在Go的app.yaml文件中如何传递运行参数?

huangapple go评论94阅读模式
英文:

How to pass run arguments in app.yaml for Go?

问题

根据文档

entrypoint(入口点):可选项。通过在应用启动时执行入口点命令来覆盖默认的启动行为。为了使应用能够接收HTTP请求,入口点元素应包含一个启动监听端口8080的Web服务器的命令。

我该如何配置这个呢?任何地方都没有找到详细信息。
我可以这样做吗?

entrypoint: go run main.go fooArg --bar-flag=1

我没有云构建文件,只有app.yaml。那么entrypoint到底是做什么的?
当应用引擎到达entrypoint部分时,程序是否已经编译完成?

谢谢

英文:

According to Docs:

> entrypoint
Optional. Overrides the default startup behavior by executing the entrypoint command when your app starts. For your app to receive HTTP requests, the entrypoint element should contain a command which starts a web server that listens on port 8080.

How would I configure this? there are no details found anywhere.
Can I do this?

entrypoint: go run main.go fooArg --bar-flag=1

I don't have cloud build file, only app.yaml. so what does entrypoint really do?
when app engine reaches entrypoint part is the program already compiled?

Thank you

答案1

得分: 3

我刚刚尝试了一下,使用我的自己的GCP AppEngine项目,并且在使用entrypoint(例如entrypoint: go run ./cmd/web prod)时对我没有起作用。当我尝试时,我得到了这个晦涩的错误信息:

错误类型:UNKNOWN
错误信息:/layers/google.go.appengine_gomod/srv 中没有Go文件

我正在使用Google Cloud SDK 344.0.0

不过,我和你的情况类似,我只是想将args传递给我的Golang主函数。根据文档,我改用了env_variables,这样就可以工作了。

我的app.yaml文件如下:

runtime: go115
main: ./cmd/web
env_variables:
  APP_ENV: "prod"

然后在我的代码中,我只需使用os.Getenv("APP_ENV")来访问它。

英文:

I tried this just now with my own GCP AppEngine project and using entrypoint (eg. entrypoint: go run ./cmd/web prod) did not work for me. When I tried it, I am getting this cryptic error message:

Error type: UNKNOWN
Error message: no Go files in /layers/google.go.appengine_gomod/srv

I'm using Google Cloud SDK 344.0.0.

I'm in a similar situation like you though where I'm simply trying to pass in args into my golang main. Following the docs, I changed over to using env_variables instead which worked.

My app.yaml looks like:

runtime: go115
main: ./cmd/web
env_variables:
  APP_ENV: "prod"

and then in my code, I simply use os.Getenv("APP_ENV") anywhere to access.

huangapple
  • 本文由 发表于 2021年6月5日 04:48:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/67843806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定