Split up the NewRouter function to ease the creation of versioned routers
This commit is contained in:
16
api/api.go
16
api/api.go
@ -7,17 +7,23 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewRouter returns an HTTP handler (http.Handler) which can then
|
// NewRouter returns an HTTP handler (http.Handler) which can be
|
||||||
// be used by the net/http package to serve the api of our API.
|
// used by the net/http package to serve the routes of our API. It
|
||||||
// We use gorilla/mux because it is more convenient than net/http,
|
// always returns a router for the newest version of our API. We
|
||||||
// e.g. when extracting path parameters.
|
// use gorilla/mux because it is more convenient than net/http, e.g.
|
||||||
|
// when extracting path parameters.
|
||||||
func NewRouter() *mux.Router {
|
func NewRouter() *mux.Router {
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
// this can later be restricted to a specific host with
|
// this can later be restricted to a specific host with
|
||||||
// `router.Host(...)` and to HTTPS with `router.Schemes("https")`
|
// `router.Host(...)` and to HTTPS with `router.Schemes("https")`
|
||||||
|
return newRouterV1(router)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newRouterV1 returns a subrouter containing the routes of version
|
||||||
|
// 1 of our API.
|
||||||
|
func newRouterV1(router *mux.Router) *mux.Router {
|
||||||
v1 := router.PathPrefix("/api/v1").Subrouter()
|
v1 := router.PathPrefix("/api/v1").Subrouter()
|
||||||
v1.HandleFunc("/health", Health).Methods(http.MethodGet)
|
v1.HandleFunc("/health", Health).Methods(http.MethodGet)
|
||||||
|
|
||||||
return v1
|
return v1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user