dongnius85154 2019-09-06 03:09
浏览 56
已采纳

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

I have created a small test app in golang and am trying to deploy it to Google AppEngine and although the "gcloud app deploy" command seems to work and reports no errors, when I visit the endpoint -> https://XXX.appspot.com/cards it just sits there and eventually gives me a 500 response, and displays the error (in the browser)

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

I am fairly new go golang and AppEngine but I have made several working apps recently and I am completely stumped as to why it's not working.

When I type "gcloud app logs tail -s default" I see some log entries like:

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...

Here is the simple single file go app "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 )
}

The app.yaml

runtime: go111

It should return text: "cards api here"

  • 写回答

1条回答 默认 最新

  • douliu3831 2019-09-06 05:18
    关注

    I figured it out, basically, there were 2 things wrong here:

    1) I needed to type "dep init" and "dep ensure -update" in the root of my project. (that downloads all the dependencies into your project folder so that they can be uploaded with your app.)

    2) I don't think we are supposed to specify a port in our code we are supposed to make the listen port taken from os.GetEnv("PORT) on an AppEngine server

    instead of:

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

    It sould be

    err := http.ListenAndServe(":"+os.getEnv("PORT"), router)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划