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:
Maxime Delporte
2025-10-10 16:15:30 +02:00
parent 4342a8df0d
commit e0931223e4
6 changed files with 77 additions and 8 deletions

24
cmd/api/routes.go Normal file
View 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
}