diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-08-05 19:26:29 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-08-05 19:26:29 +0200 |
commit | 5168a9476f0e83264ecafc85bc9145e8bdcbb8dc (patch) | |
tree | 5cd04e84f8aa427d274e16dd8cf507ff48468e07 /pkg/ext/router.go | |
parent | 24c121a9ef229870f54de487de01c15a15ebbff1 (diff) | |
download | lens-5168a9476f0e83264ecafc85bc9145e8bdcbb8dc.tar.gz lens-5168a9476f0e83264ecafc85bc9145e8bdcbb8dc.tar.bz2 lens-5168a9476f0e83264ecafc85bc9145e8bdcbb8dc.zip |
ref: Move net/http
I was young and naive, fasthttp does not fit my needs and make
development slower. I'll move to net/http since it has a wider support
and will spare some time on implementation detail things (like CSRF). It
will allow me to reduce a bit of the code since there may be lib for
handling cookie encryption and auth in general.
Diffstat (limited to 'pkg/ext/router.go')
-rw-r--r-- | pkg/ext/router.go | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/pkg/ext/router.go b/pkg/ext/router.go index 74f0a95..8b9a310 100644 --- a/pkg/ext/router.go +++ b/pkg/ext/router.go @@ -1,51 +1,52 @@ package ext import ( - "github.com/fasthttp/router" - "github.com/valyala/fasthttp" + "net/http" + + "github.com/gorilla/mux" ) type ( Router struct { middlewares []Middleware - fastRouter *router.Router + router *mux.Router } - Middleware func(next fasthttp.RequestHandler) fasthttp.RequestHandler - ErrorRequestHandler func(ctx *fasthttp.RequestCtx) error + Middleware func(next http.HandlerFunc) http.HandlerFunc + ErrorRequestHandler func(w http.ResponseWriter, r *http.Request) error ) -func NewRouter(nestedRouter *router.Router) *Router { +func NewRouter(nestedRouter *mux.Router) *Router { return &Router{ - fastRouter: nestedRouter, + router: nestedRouter, } } -func (self *Router) AddMiddleware(middleware Middleware) { - self.middlewares = append(self.middlewares, middleware) +func (r *Router) AddMiddleware(middleware Middleware) { + r.middlewares = append(r.middlewares, middleware) } -func wrapError(next ErrorRequestHandler) fasthttp.RequestHandler { - return func(ctx *fasthttp.RequestCtx) { - if err := next(ctx); err != nil { - ctx.Response.SetStatusCode(500) - InternalServerError(ctx, err) +func wrapError(next ErrorRequestHandler) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if err := next(w, r); err != nil { + w.WriteHeader(http.StatusInternalServerError) + InternalServerError(w, err) } } } -func (self *Router) run(next ErrorRequestHandler) fasthttp.RequestHandler { - return func(ctx *fasthttp.RequestCtx) { +func (r *Router) run(next ErrorRequestHandler) http.HandlerFunc { + return func(w http.ResponseWriter, re *http.Request) { req := wrapError(next) - for _, r := range self.middlewares { + for _, r := range r.middlewares { req = r(req) } - req(ctx) + req(w, re) } } -func (self *Router) GET(path string, handler ErrorRequestHandler) { - self.fastRouter.GET(path, self.run(handler)) +func (r *Router) GET(path string, handler ErrorRequestHandler) { + r.router.HandleFunc(path, r.run(handler)).Methods("GET") } -func (self *Router) POST(path string, handler ErrorRequestHandler) { - self.fastRouter.POST(path, self.run(handler)) +func (r *Router) POST(path string, handler ErrorRequestHandler) { + r.router.HandleFunc(path, r.run(handler)).Methods("POSt") } |