没有使用Gorilla Mux的Google Cloud Go处理程序?

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

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)
}

huangapple
  • 本文由 发表于 2014年10月27日 12:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/26581231.html
匿名

发表评论

匿名网友

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

确定