Go应用在本地运行正常,但在运行goapp serve时出现404错误。

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

Go app runs fine locally, produces 404 when running goapp serve

问题

我使用Gorilla mux包编写了一个REST API,并且所有的数据都存储在MySQL数据库中。我使用go-sql-driver/mysql包来访问它。

当我直接编译和运行二进制文件时,它按预期工作。

这是我第一次尝试部署到Google云服务,所以我不熟悉需要进行任何特殊设置以使其工作。

所有的代码都可以在cobraclamp/hotswapper-api找到。

注意:我知道main函数中的InitDB有样板代码的凭据,在本地和生产项目中已经正确设置。

英文:

I have a written a REST API using the Gorilla mux package and all data is stored in a MySQL database. I'm using the go-sql-driver/mysql package to access it.

When I compile and run the bin directly, it works as expected.

This is my first attepmt at deploying about to google cloud services, so I'm not familair with any special setup thatr needs to occur to make this work.

All code can be found at cobraclamp/hotswapper-api

NOTE: I'm aware that the InitDB in main has boilerplate credentials, they are properly set in the local and production projects

答案1

得分: 2

我还没有完全浏览你的代码,但我猜问题出在你在main中初始化了你的路由器。

根据App Engine go SDK文档Gorilla mux文档,你需要在一个init()函数中进行这样的操作:

或者,对于Google App Engine,将其注册在init()函数中:

func init() {
    http.Handle("/", router)
}

如果你不这样做,我猜你的应用程序将对任何路由返回404错误。

英文:

I haven't trawled through all your code, but I guess the problem is you initialise your router in main.

As per the App Engine go SDK docs and the Gorilla mux docs, you need to do this in an init() function:

> Or, for Google App Engine, register it in a init() function:

func init() {
    http.Handle("/", router)
}

If you don't do this I guess your app will get a 404 for any route.

huangapple
  • 本文由 发表于 2016年3月26日 04:33:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/36227600.html
匿名

发表评论

匿名网友

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

确定