Files
greenlight/cmd/api/routes.go

33 lines
1.3 KiB
Go

package main
import (
"github.com/julienschmidt/httprouter"
"net/http"
)
func (app *application) routes() http.Handler {
// Initialize a new httprouter router instance
router := httprouter.New()
// Convert the notFoundResponse() helper to a http.Handler using the http.HandlerFunc() adapter, and then set it as the custom error handler for 404 Not Found responses.
router.NotFound = http.HandlerFunc(app.notFoundResponse)
// Likewise, convert the methodNotAllowedResponse() helper to a http.Handler and set it as the custom error handler for 405 Method Not Allowed responses
router.MethodNotAllowed = http.HandlerFunc(app.methodNotAllowedResponse)
/*
Register the relevant methods, URL patterns and handler functions for our
endpoints using the HandlerFunc() method. Note that http.MethodGet and
http.MethodPost are constants which equate to the strings "GET" and "POST"
respectively.
*/
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)
router.HandlerFunc(http.MethodPut, "/v1/movies/:id", app.updateMovieHandler)
// Return the httprouter instance.
// Wrap the router with the panic recovery middleware
return app.recoverPanic(router)
}