google-app-engine - 将简单的 golang 应用程序部署到 appengine 已停止工作

标签 google-app-engine go gcloud

我在 golang 中创建了一个小型测试应用程序,并尝试将其部署到 Google AppEngine,尽管“gcloud app deploy”命令似乎有效并且没有报告任何错误,但当我访问端点时 -> https://XXX.appspot.com/cards它只是坐在那里,最终给我一个 500 响应,并显示错误(在浏览器中)

Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.

我对 go golang 和 AppEngine 相当陌生,但我最近制作了几个可用的应用程序,但我完全不知道为什么它不起作用。

当我输入“gcloud app logs tail -s default”时,我看到一些日志条目,例如:

2019-09-06 02:17:31 default[20190905t204717]  "GET / HTTP/1.1" 500
2019-09-06 02:17:32 default[20190905t204717]  Starting the application...
2019-09-06 02:22:08 default[20190905t212008]  "GET / HTTP/1.1" 500
2019-09-06 02:22:08 default[20190905t212008]  Starting the application...
2019-09-06 02:22:14 default[20190905t212008]  "GET /cards HTTP/1.1" 500
2019-09-06 02:22:17 default[20190905t212008]  "GET /cards HTTP/1.1" 500
2019-09-06 02:22:17 default[20190905t212008]  Starting the application...

这是简单的单文件 go 应用“main.go”

package main
import (
    "encoding/json"
    "fmt"
    "github.com/gorilla/mux"
    "net/http"
)
func main() {
    fmt.Println("Starting the application...")
    router := mux.NewRouter()
    router.HandleFunc("/cards", GetCardsEndpoint).Methods("GET")
    err := http.ListenAndServe(":80", router)
    fmt.Println("ERR:", err)
}
func GetCardsEndpoint(response http.ResponseWriter, request *http.Request) {
    fmt.Println("API: GetCardsEndpoint() ")
    err := json.NewEncoder(response).Encode("cards api here")
    fmt.Println("API: GetCardsEndpoint() err:", err )
}

app.yaml

runtime: go111

它应该返回文本:“cards api here”

最佳答案

我想通了,基本上,这里有两处错误:

1) 我需要在项目的根目录中键入“dep init”和“dep ensure -update”。 (这会将所有依赖项下载到您的项目文件夹中,以便它们可以与您的应用程序一起上传。)

2) 我不认为我们应该在我们的代码中指定一个端口我们应该从 AppEngine 服务器上的 os.GetEnv("PORT) 获取监听端口

代替:

err := http.ListenAndServe(":80", router)

应该是

err := http.ListenAndServe(":"+os.getEnv("PORT"), router)

关于google-app-engine - 将简单的 golang 应用程序部署到 appengine 已停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57815210/

相关文章:

MySql server “Error No. 2003 Can' t connect to Mysql server”,我连接不上,我已经改了my.cnf并创建了远程用户

sql - 阻止 BigQuery 截断输出

google-cloud-platform - 谷歌云平台: How to fetch permissions assigned for particular Identity across all GCP resources via REST call

google-app-engine - Google App Engine - 数据存储失败异常 : internal error

python - 无法在 Google 应用引擎中使用 TfidfVectorizer

google-app-engine - 谷歌应用引擎 : Java version of the python deferred library?

go - 在 go 中实现 io.WriterAt 的缓冲区

c# - Go map 与 C# 字典

azure - 可以通过Azure go sdk接收用户信息吗?

python - 在 GqlQuery 的 where 子句中使用本地时间