diff options
Diffstat (limited to 'pkg/ext/router.go')
-rw-r--r-- | pkg/ext/router.go | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/pkg/ext/router.go b/pkg/ext/router.go new file mode 100644 index 0000000..5d22814 --- /dev/null +++ b/pkg/ext/router.go @@ -0,0 +1,72 @@ +package ext + +import ( + "errors" + "fmt" + "net/http" + + "git.gabrielgio.me/cerrado/pkg/service" + "git.gabrielgio.me/cerrado/templates" +) + +type ( + Router struct { + middlewares []Middleware + router *http.ServeMux + } + Middleware func(next http.HandlerFunc) http.HandlerFunc + ErrorRequestHandler func(w http.ResponseWriter, r *http.Request) error +) + +func NewRouter() *Router { + return &Router{ + router: http.NewServeMux(), + } +} +func (r *Router) Handler() http.Handler { + return r.router +} + +func (r *Router) AddMiddleware(middleware Middleware) { + r.middlewares = append(r.middlewares, middleware) +} + +func wrapError(next ErrorRequestHandler) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if err := next(w, r); err != nil { + if errors.Is(err, service.RepositoryNotFoundErr) { + NotFound(w) + } else { + InternalServerError(w, err) + } + } + } +} + +func (r *Router) run(next ErrorRequestHandler) http.HandlerFunc { + return func(w http.ResponseWriter, re *http.Request) { + req := wrapError(next) + for _, r := range r.middlewares { + req = r(req) + } + req(w, re) + } +} + +func (r *Router) HandleFunc(path string, handler ErrorRequestHandler) { + r.router.HandleFunc(path, r.run(handler)) +} + +func NotFound(w http.ResponseWriter) { + w.WriteHeader(http.StatusNotFound) + templates.WritePageTemplate(w, &templates.ErrorPage{ + Message: "Not Found", + }) +} + +func InternalServerError(w http.ResponseWriter, err error) { + w.WriteHeader(http.StatusInternalServerError) + templates.WritePageTemplate(w, &templates.ErrorPage{ + Message: fmt.Sprintf("Internal Server Error:\n%s", err.Error()), + }) +} |