Created
February 4, 2020 10:20
-
-
Save aquiseb/f5e061f8dc2e86e7e3355d48d515f03e to your computer and use it in GitHub Desktop.
Multiple routes in a google cloud function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package function | |
| import ( | |
| "net/http" | |
| ) | |
| var mux = newMux() | |
| //F represents cloud function entry point | |
| func F(w http.ResponseWriter, r *http.Request) { | |
| mux.ServeHTTP(w, r) | |
| } | |
| func newMux() *http.ServeMux { | |
| mux := http.NewServeMux() | |
| mux.HandleFunc("/one", one) | |
| mux.HandleFunc("/two", two) | |
| mux.HandleFunc("/subroute/three", three) | |
| return mux | |
| } | |
| func one(w http.ResponseWriter, r *http.Request) { | |
| w.Write([]byte("hello from one")) | |
| } | |
| func two(w http.ResponseWriter, r *http.Request) { | |
| w.Write([]byte("hello from two")) | |
| } | |
| func three(w http.ResponseWriter, r *http.Request) { | |
| w.Write([]byte("hello from three")) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment