From 9dbc796b7ac28d126e407b622cb37b2c53458f27 Mon Sep 17 00:00:00 2001 From: Apoorv Date: Thu, 3 Mar 2022 19:30:31 +0530 Subject: [PATCH] feat: now minima middlewares build to standard ones (#49) Co-authored-by: ToTu-Dev <75479355+ToTu-Dev@users.noreply.github.com> --- _examples/start.go | 3 +-- build.go | 14 ++++++++++++++ minima.go | 20 +++++++++++--------- request.go | 7 +++---- router.go | 20 +++----------------- 5 files changed, 32 insertions(+), 32 deletions(-) create mode 100644 build.go diff --git a/_examples/start.go b/_examples/start.go index df36c43..f5bbbcb 100644 --- a/_examples/start.go +++ b/_examples/start.go @@ -1,6 +1,5 @@ package main - /** * Minima is a free and open source software under Mit license @@ -26,7 +25,7 @@ SOFTWARE. * Authors @apoorvcodes @megatank58 * Maintainers @Panquesito7 @savioxavier @Shubhaankar-Sharma @apoorvcodes @megatank58 -* Thank you for showing interest in minima and for this beautiful community +* Thank you for showing interest in minima and for this beautiful community */ import ( diff --git a/build.go b/build.go new file mode 100644 index 0000000..1ed6955 --- /dev/null +++ b/build.go @@ -0,0 +1,14 @@ +package minima + +import "net/http" + +func Build(h Handler) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + resp := response(w, req) + reqs := request(req) + h(resp, reqs) + next.ServeHTTP(w, req) + }) + } +} diff --git a/minima.go b/minima.go index 945cdd6..250a1f1 100644 --- a/minima.go +++ b/minima.go @@ -271,22 +271,24 @@ func (m *Minima) GetProp(key string) interface{} { return m.properties[key] } + + /** - * * @info Injects Minima middleware to the stack - * * @param {...Handler} [handler] The handler stack to append - * * @returns {} + * @info Injects net/http middleware to the stack + * @param {...http.HandlerFunc} [handler] The handler stack to append + * @returns {} */ -func (m *Minima) Use(handler ...Handler) *Minima { +func (m *Minima) UseRaw(handler ...func(http.Handler) http.Handler) *Minima { m.router.use(handler...) return m } /** - * * @info Injects net/http middleware to the stack - * * @param {...http.HandlerFunc} [handler] The handler stack to append - * * @returns {} + * @info Injects minima middleware to the stack + * @param {Handler} [handler] The handler stack to append + * @returns {} */ -func (m *Minima) UseRaw(handler ...func(http.Handler) http.Handler) *Minima { - m.router.useRaw(handler...) + func (m *Minima) Use(handler Handler) *Minima { + m.router.use(Build(handler)) return m } diff --git a/request.go b/request.go index 5d2a460..97b0e1e 100644 --- a/request.go +++ b/request.go @@ -70,9 +70,8 @@ func request(httpRequest *http.Request) *Request { header: &IncomingHeader{}, fileReader: nil, method: httpRequest.Proto, - rawQuery: httpRequest.URL.Query(), - Query: make(map[string][]string), - + rawQuery: httpRequest.URL.Query(), + Query: make(map[string][]string), } for i, v := range httpRequest.Header { @@ -91,7 +90,7 @@ func request(httpRequest *http.Request) *Request { req.body[key] = value } } - for i,v := range req.rawQuery { + for i, v := range req.rawQuery { req.Query[i] = v } diff --git a/router.go b/router.go index 457f23e..291ff56 100644 --- a/router.go +++ b/router.go @@ -46,7 +46,6 @@ type Handler func(res *Response, req *Request) type Router struct { notfound Handler handler http.Handler - minmiddleware []Handler middlewares []func(http.Handler) http.Handler routes map[string]*Routes } @@ -209,21 +208,14 @@ func (r *Router) Mount(path string, Router *Router) *Router { return r } -/** - * @info Injects Minima middleware to the stack - * @param {...Handler} [handler] The handler stack to append - * @returns {} - */ -func (r *Router) use(handler ...Handler) { - r.minmiddleware = append(r.minmiddleware, handler...) -} + /** * @info Injects net/http middleware to the stack * @param {...func(http.Handler)http.Handler} [handler] The handler stack to append * @returns {} */ -func (r *Router) useRaw(handler ...func(http.Handler) http.Handler) { +func (r *Router) use(handler ...func(http.Handler) http.Handler) { if r.handler != nil { panic("Minima: Middlewares can't go after the routes are mounted") } @@ -231,13 +223,7 @@ func (r *Router) useRaw(handler ...func(http.Handler) http.Handler) { } //A dummy function that runs at the end of the middleware stack -func (r *Router) middlewareHTTP(w http.ResponseWriter, rq *http.Request) { - resp := response(w, rq) - req := request(rq) - for _, fn := range r.minmiddleware { - fn(resp, req) - } -} +func (r *Router) middlewareHTTP(w http.ResponseWriter, rq *http.Request) {} /** * @info Builds whole middleware stack chain into single handler