R
Railway9mo ago
Nuke

Need help with deployment

Subject: Seeking Guidance on Loading Environment Variables in Railway for My Go API Deployment Hi everyone, I hope you're all doing well. I'm currently working on deploying my Go API on Railway, and I've run into a bit of a challenge. Specifically, I need to figure out how to load environment variables from a .env file in Railway. Locally, I've been able to load these variables successfully. Here's a snippet of my .env file for reference:
# Firebase project ID
FIREBASE_PROJECT_ID=jobs-scraxxxxx-xxxx

# Path to your Firebase service account credentials JSON file
FIREBASE_CREDENTIALS_FILE=cred.json

# Port for your application (optional, you can use a default port if not specified)
PORT=8080
# Firebase project ID
FIREBASE_PROJECT_ID=jobs-scraxxxxx-xxxx

# Path to your Firebase service account credentials JSON file
FIREBASE_CREDENTIALS_FILE=cred.json

# Port for your application (optional, you can use a default port if not specified)
PORT=8080
In my main.go file, I'm loading these environment variables like this:
projectID := os.Getenv("FIREBASE_PROJECT_ID")
credentialsFile := os.Getenv("FIREBASE_CREDENTIALS_FILE")

// Initialize Firestore client using environment variables.
opt := option.WithCredentialsFile(credentialsFile)
firestoreClient, err := firestore.NewClient(context.Background(), projectID, opt)
if err != nil {
log.Fatalf("Error initializing Firestore client: %v", err)
}

r.GET("/syncFirestore", func(c *gin.Context) {
handlers.SyncWithFireBase(c, firestoreClient)
})

r.GET("/getJobsFromFirestore", func(c *gin.Context) {
handlers.GetJobsFromFirestore(c, firestoreClient)
})
projectID := os.Getenv("FIREBASE_PROJECT_ID")
credentialsFile := os.Getenv("FIREBASE_CREDENTIALS_FILE")

// Initialize Firestore client using environment variables.
opt := option.WithCredentialsFile(credentialsFile)
firestoreClient, err := firestore.NewClient(context.Background(), projectID, opt)
if err != nil {
log.Fatalf("Error initializing Firestore client: %v", err)
}

r.GET("/syncFirestore", func(c *gin.Context) {
handlers.SyncWithFireBase(c, firestoreClient)
})

r.GET("/getJobsFromFirestore", func(c *gin.Context) {
handlers.GetJobsFromFirestore(c, firestoreClient)
})
Now, the challenge I'm facing is how to load the cred.json file in Railway, as it primarily supports key-value pairs for environment variables. You can find my GitHub repository with the code here: Jobs-Scraper on GitHub I would greatly appreciate any guidance or suggestions on how to tackle this issue and successfully deploy my Go API on Railway. Thank you in advance for your assistance! Project ID: 455cfb5c-e03f-4d7c-81b7-1e2a12858525 Best regards, Mabud
GitHub
GitHub - Pavel401/Jobs-Scraper
Contribute to Pavel401/Jobs-Scraper development by creating an account on GitHub.
No description
3 Replies
Percy
Percy9mo ago
Project ID: N/A
Nuke
Nuke9mo ago
Project ID : sweltering-air Project ID: 455cfb5c-e03f-4d7c-81b7-1e2a12858525
Brody
Brody9mo ago
id recommend storing that credentials file on a volume https://docs.railway.app/reference/volumes but you can deploy this template https://railway.app/template/Nan7Bs it will come with a volume, then upload that json credentials file to it, unmount the volume from the template service and then mount it to your go service