I am working on a project where I am using Gin
framework for API development and Firestore
as a database. In local everything is working fine. I am able to perform CRUD operations. When I deployed the API to Heroku
, I can see in logs that the build is successful and the application gets deployed. When I clicked on it to open it is giving the error "Application error"
. It is because Heroku is not reading my firestore
credentials from the google-credentials.json
file. And I am not supposed to upload the google-credentials.json
file as it has my credentials. For local I am reading it by giving a local file path.
controllers/users.go
var firestoreCredentialsLocation = "A:/Go/API_Gin/google-credentials.json" //local path to file
func GetUses(c *gin.Context) {
// Use a service account
ctx := context.Background()
sa := option.WithCredentialsFile(firestoreCredentialsLocation)
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
blah blah..........
blah blah..........
blah blah..........
}
c.IndentedJSON(http.StatusOK, gin.H{
"message": "Users returned successfully!",
})
}
Error after clicking on URL:
logs:
Can you guys tell me how can I solve this problem? What should I do so that Heroku will read my database credentials and deploy the app successfully? Also, tell me is it a good method to give the path of a .json file? Thank you.
CodePudding user response:
Instead of getting the credentials from the file, try to get them from the environment variables. You can check this Heroku documentation to find out how to set environment variables in Heroku https://devcenter.heroku.com/articles/config-vars
CodePudding user response:
You can use Heroku google-credentials build pack here to read google-credentials from environment variables and create a credentials.json in heroku