没有路由器Gorilla Mux的Google Cloud Go处理程序?

10 google-app-engine router go

https://cloud.google.com/appengine/docs/go/users/

我在这里看到他们没有指定使用任何路由器...:https://cloud.google.com/appengine/docs/go/config/appconfig

在谷歌云中,当与Golang一起使用时,它表示要指定每个处理程序app.yaml.这是否意味着我们不应该使用第三方路由器以获得更好的性能?我想Gorilla Mux用于路由器...如果我使用其他路由器用于Google App Engine Golang App,它会如何工作?

请告诉我.谢谢!

小智 14

您可以将Gorilla Mux与App Engine一起使用.这是如何做:

app.yaml的处理程序部分的末尾,添加一个脚本处理程序,将所有路径路由到Go应用程序:

application: myapp
version: 1
runtime: go
api_version: go1

handlers:

- url: /(.*\.(gif|png|jpg))$
  static_files: static/\1
  upload: static/.*\.(gif|png|jpg)$

- url: /.*
  script: _go_app
Run Code Online (Sandbox Code Playgroud)

_go_app脚本是App Engine编译的Go程序.该模式/.*匹配所有路径.

App Engine生成的主要功能将所有请求分派给DefaultServeMux.

在init()函数中,创建并配置Gorilla 路由器.使用DefaultServeMux注册Gorilla路由器以处理所有路径:

func init() {
    r := mux.NewRouter()
    r.HandleFunc("/", homeHandler)

    // The path "/" matches everything not matched by some other path.
    http.Handle("/", r)
}
Run Code Online (Sandbox Code Playgroud)