英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论