Breaking HTTP handlers into groups
This recipe gives advice on how the HTTP handlers could be separated into modules.
How to do it...
- Open the console and create the folder
chapter11/recipe08
. - Navigate to the directory.
- Create the file
handlegroups.go
with the following content:
package main import ( "fmt" "log" "net/http" ) func main() { log.Println("Staring server...") // Adding to mani Mux mainMux := http.NewServeMux() mainMux.Handle("/api/", http.StripPrefix("/api", restModule())) mainMux.Handle("/ui/", http.StripPrefix("/ui", uiModule())) if err := http.ListenAndServe(":8080", mainMux); err != nil { panic(err) } } func restModule() http.Handler { // Separate Mux for all REST restApi := http.NewServeMux() restApi.HandleFunc("/users", func(w http.ResponseWriter...