/
routes.go
44 lines (33 loc) · 1.98 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"expvar"
"net/http"
"github.com/julienschmidt/httprouter"
)
// routes is our main application's router.
func (app *application) routes() http.Handler {
router := httprouter.New()
// Convert the app.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)
// Convert app.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)
// healthcheck
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
// application metrics handler
router.Handler(http.MethodGet, "/debug/vars", expvar.Handler())
// Movies handlers. Note, that these movie endpoints use the `requireActivatedUser` middleware.
router.HandlerFunc(http.MethodGet, "/v1/movies", app.requirePermissions("movies:read", app.listMoviesHandler))
router.HandlerFunc(http.MethodPost, "/v1/movies", app.requirePermissions("movies:write", app.createMovieHandler))
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.requirePermissions("movies:read", app.showMovieHandler))
router.HandlerFunc(http.MethodPatch, "/v1/movies/:id", app.requirePermissions("movies:write", app.updateMovieHandler))
router.HandlerFunc(http.MethodDelete, "/v1/movies/:id", app.requirePermissions("movies:write", app.deleteMovieHandler))
// Users handlers
router.HandlerFunc(http.MethodPost, "/v1/users", app.registerUserHandler)
router.HandlerFunc(http.MethodPut, "/v1/users/activated", app.activateUserHandler)
// Tokens handlers
router.HandlerFunc(http.MethodPost, "/v1/tokens/authentication", app.createAuthenticationTokenHandler)
// Wrap the router with the panic recovery middleware and rate limit middleware.
return app.metrics(app.recoverPanic(app.enableCORS(app.rateLimit(app.authenticate(router)))))
}