rest-api-prototype/internal/app/router.go

36 lines
1.2 KiB
Go
Raw Normal View History

package app
import (
"net/http"
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/handler"
2022-03-11 18:08:03 +01:00
"git.pbiernat.dev/golang/rest-api-prototype/internal/app/service"
"github.com/gorilla/mux"
)
func SetupRouter(env *handler.Env) *mux.Router {
r := mux.NewRouter()
r.NotFoundHandler = &handler.NotFoundHandler{}
r.MethodNotAllowedHandler = &handler.MethodNotAllowedHandler{}
r.Use(PrepareHeadersMiddleware)
r.Use(ValidateJsonBodyMiddleware) // probably not needed
r.Use(LoggingMiddleware)
hc := r.PathPrefix("/health").Subrouter()
hc.Handle("", handler.New(env, handler.HealthCheckHandler)).Methods(http.MethodGet)
2022-03-11 18:08:03 +01:00
auth := r.PathPrefix("/auth").Subrouter()
auth.Handle("/login", handler.New(env, handler.AuthLoginHandler)).Methods(http.MethodPost)
api := r.PathPrefix("/api").Subrouter()
2022-03-11 18:08:03 +01:00
api.Use(service.AuthService.ValidateUserTokenMiddleware) // only /api/** endpoints use this middleware
api.Handle("/article", handler.New(env, handler.CreateArticleHandler)).Methods(http.MethodPost)
api.Handle("/category", handler.New(env, handler.CreateCategoryHandler)).Methods(http.MethodPost)
api.Handle("/category/{id:[0-9]+}", handler.New(env, handler.DeleteCategoryHandler)).Methods(http.MethodDelete)
return r
}