Reputation:
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!
Upvotes: 11
Views: 2283
Reputation:
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/\1
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)
}
Upvotes: 14