A single handler is awkward when an HTTP service needs several endpoints. `http.ServeMux` routes requests by URL to registered handlers. Its `ServeHTTP` method makes it an `http.Handler`, so it can be passed to `http.ListenAndServe`. section 8.2.1
Listing 8.4 internal/handlers/router.go: Handler to URL association
package handlers
import (
"net/http"
"learngo-pockets/httpgordle/internal/api"
"learngo-pockets/httpgordle/internal/newgame"
)
// Mux creates a multiplexer with all the endpoints for our service.
func Mux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc(api.NewGameRoute, newgame.Handle) #1
return mux
}
Create a mux in one function, register `/games` with `HandleFunc`, and return it for use by the server. section 8.2.1
A path-only registration leaves the `/games` handler to reject non-POST requests; the example returns `405 Method Not Allowed` for GET. A later router registers a method-and-path pattern instead. Keep route setup together as endpoints are added. section 8.2.1
