英文:
Heroku is not reading Firestore credentials
问题
我正在进行一个项目,其中我使用Gin
框架进行API开发,使用Firestore
作为数据库。在本地一切正常工作,我能够执行CRUD操作。当我将API部署到Heroku
时,日志显示构建成功并部署应用程序。但是,当我点击打开应用程序时,出现错误“应用程序错误”。这是因为Heroku无法从google-credentials.json
文件中读取我的firestore
凭据。而且,我不应该上传google-credentials.json
文件,因为它包含我的凭据。在本地,我通过提供本地文件路径来读取它。
控制器/controllers/users.go
var firestoreCredentialsLocation = "A:/Go/API_Gin/google-credentials.json" //本地文件路径
func GetUsers(c *gin.Context) {
// 使用服务账号
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()
// 其他操作...
}
c.IndentedJSON(http.StatusOK, gin.H{
"message": "用户成功返回!",
})
}
点击URL后的错误:
日志:
你能告诉我如何解决这个问题吗?我应该怎么做才能让Heroku读取我的数据库凭据并成功部署应用程序?另外,请告诉我给出.json文件路径的方法是否可行?谢谢。
英文:
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.
答案1
得分: 1
问题解决了。我以错误的方式读取了一个端口值,导致应用程序崩溃。Heroku的Go应用程序文档帮助了我。
https://devcenter.heroku.com/articles/getting-started-with-go
英文:
Problem solved. I was reading a port value in an incorrect manner and because of that, the application crashed. Heroku's documentation for Go apps helped me.
https://devcenter.heroku.com/articles/getting-started-with-go
答案2
得分: 0
尝试从环境变量中获取凭据,而不是从文件中获取。你可以查看Heroku文档,了解如何在Heroku中设置环境变量。https://devcenter.heroku.com/articles/config-vars
英文:
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
答案3
得分: 0
你可以使用Heroku的google-credentials构建包(在此处)来从环境变量中读取google-credentials,并在Heroku上创建一个credentials.json文件。
英文:
You can use Heroku google-credentials build pack here to read google-credentials from environment variables and create a credentials.json in heroku
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论