Adding httprouter dependency to manage endpoints. Adding our routes to routes.go file facilitating endpoints management and adding POST /v1/movies and GET /v1/movies/:id endpoints. Updating main.go file removing ServeMux and add httprouter instead in app structure. Creating movies.go file to manage create and show movie endpoint.
This commit is contained in:
24
cmd/api/routes.go
Normal file
24
cmd/api/routes.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (app *application) routes() http.Handler {
|
||||
// Initialize a new httprouter router instance
|
||||
router := httprouter.New()
|
||||
|
||||
/*
|
||||
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)
|
||||
|
||||
// Return the httprouter instance.
|
||||
return router
|
||||
}
|
||||
Reference in New Issue
Block a user