英文:
Google Cloud Go Handler without Router Gorilla Mux?
问题
我看到这里他们没有指定使用任何路由器...:https://cloud.google.com/appengine/docs/go/config/appconfig
在使用Golang的Google Cloud中,它说要在app.yaml
中指定每个处理程序。这是否意味着我们不应该使用第三方路由器以获得更好的性能?我想要使用Gorilla Mux
作为路由器...如果我在Google App Engine Golang应用程序中使用其他路由器,它会如何工作?
请告诉我。谢谢!
英文:
https://cloud.google.com/appengine/docs/go/users/
I see here that they do not specify to use any router...: https://cloud.google.com/appengine/docs/go/config/appconfig
In Google Cloud when used with Golang, it says to specify every handler in app.yaml
. Does this mean we are not supposed to use 3rd party router for better performance? I would like to Gorilla Mux
for router... How would it work if I use other routers for Google App Engine Golang App?
Please let me know. Thanks!
答案1
得分: 14
你可以在App Engine中使用Gorilla Mux。以下是具体步骤:
在app.yaml的handlers部分的末尾,添加一个脚本处理程序,将所有路径路由到Go应用程序:
application: myapp
version: 1
runtime: go
api_version: go1
handlers:
- url: /(.*\.(gif|png|jpg))$
static_files: static/
upload: static/.*\.(gif|png|jpg)$
- url: /.*
script: _go_app
_go_app
脚本是由App Engine编译的Go程序。模式/.*
匹配所有路径。
由App Engine生成的main函数将所有请求分派给DefaultServeMux。
在init()函数中,创建并配置一个Gorilla Router。将Gorilla路由器注册到DefaultServeMux以处理所有路径:
func init() {
r := mux.NewRouter()
r.HandleFunc("/", homeHandler)
// 路径"/"匹配所有未被其他路径匹配的内容。
http.Handle("/", r)
}
英文:
You can use Gorilla Mux with App Engine. Here's how:
At the end of the handlers section of app.yaml, add a script handler that routes all paths to the Go application:
application: myapp
version: 1
runtime: go
api_version: go1
handlers:
- url: /(.*\.(gif|png|jpg))$
static_files: static/
upload: static/.*\.(gif|png|jpg)$
- url: /.*
script: _go_app
The _go_app
script is the Go program compiled by App Engine. The pattern /.*
matches all paths.
The main function generated by App Engine dispatches all requests to the DefaultServeMux.
In an init() function, create and configure a Gorilla Router. Register the Gorilla router with the DefaultServeMux to handle all paths:
func init() {
r := mux.NewRouter()
r.HandleFunc("/", homeHandler)
// The path "/" matches everything not matched by some other path.
http.Handle("/", r)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论