Skip to content

Commit

Permalink
feat: now minima middlewares build to standard ones (#49)
Browse files Browse the repository at this point in the history
Co-authored-by: ToTu-Dev <[email protected]>
  • Loading branch information
apoorvcodes and apoorvcodes authored Mar 3, 2022
1 parent f6f4d91 commit 9dbc796
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
3 changes: 1 addition & 2 deletions _examples/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package main


/**
* Minima is a free and open source software under Mit license
Expand All @@ -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 (
Expand Down
14 changes: 14 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
20 changes: 11 additions & 9 deletions minima.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 3 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
20 changes: 3 additions & 17 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -209,35 +208,22 @@ 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")
}
r.middlewares = append(r.middlewares, 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
Expand Down

0 comments on commit 9dbc796

Please sign in to comment.