Skip to content

Commit 9dbc796

Browse files
authored
feat: now minima middlewares build to standard ones (#49)
Co-authored-by: ToTu-Dev <75479355+ToTu-Dev@users.noreply.github.com>
1 parent f6f4d91 commit 9dbc796

5 files changed

Lines changed: 32 additions & 32 deletions

File tree

_examples/start.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package main
22

3-
43
/**
54
* Minima is a free and open source software under Mit license
65
@@ -26,7 +25,7 @@ SOFTWARE.
2625
2726
* Authors @apoorvcodes @megatank58
2827
* Maintainers @Panquesito7 @savioxavier @Shubhaankar-Sharma @apoorvcodes @megatank58
29-
* Thank you for showing interest in minima and for this beautiful community
28+
* Thank you for showing interest in minima and for this beautiful community
3029
*/
3130

3231
import (

build.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package minima
2+
3+
import "net/http"
4+
5+
func Build(h Handler) func(http.Handler) http.Handler {
6+
return func(next http.Handler) http.Handler {
7+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
8+
resp := response(w, req)
9+
reqs := request(req)
10+
h(resp, reqs)
11+
next.ServeHTTP(w, req)
12+
})
13+
}
14+
}

minima.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,22 +271,24 @@ func (m *Minima) GetProp(key string) interface{} {
271271
return m.properties[key]
272272
}
273273

274+
275+
274276
/**
275-
* * @info Injects Minima middleware to the stack
276-
* * @param {...Handler} [handler] The handler stack to append
277-
* * @returns {}
277+
* @info Injects net/http middleware to the stack
278+
* @param {...http.HandlerFunc} [handler] The handler stack to append
279+
* @returns {}
278280
*/
279-
func (m *Minima) Use(handler ...Handler) *Minima {
281+
func (m *Minima) UseRaw(handler ...func(http.Handler) http.Handler) *Minima {
280282
m.router.use(handler...)
281283
return m
282284
}
283285

284286
/**
285-
* * @info Injects net/http middleware to the stack
286-
* * @param {...http.HandlerFunc} [handler] The handler stack to append
287-
* * @returns {}
287+
* @info Injects minima middleware to the stack
288+
* @param {Handler} [handler] The handler stack to append
289+
* @returns {}
288290
*/
289-
func (m *Minima) UseRaw(handler ...func(http.Handler) http.Handler) *Minima {
290-
m.router.useRaw(handler...)
291+
func (m *Minima) Use(handler Handler) *Minima {
292+
m.router.use(Build(handler))
291293
return m
292294
}

request.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ func request(httpRequest *http.Request) *Request {
7070
header: &IncomingHeader{},
7171
fileReader: nil,
7272
method: httpRequest.Proto,
73-
rawQuery: httpRequest.URL.Query(),
74-
Query: make(map[string][]string),
75-
73+
rawQuery: httpRequest.URL.Query(),
74+
Query: make(map[string][]string),
7675
}
7776

7877
for i, v := range httpRequest.Header {
@@ -91,7 +90,7 @@ func request(httpRequest *http.Request) *Request {
9190
req.body[key] = value
9291
}
9392
}
94-
for i,v := range req.rawQuery {
93+
for i, v := range req.rawQuery {
9594
req.Query[i] = v
9695
}
9796

router.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ type Handler func(res *Response, req *Request)
4646
type Router struct {
4747
notfound Handler
4848
handler http.Handler
49-
minmiddleware []Handler
5049
middlewares []func(http.Handler) http.Handler
5150
routes map[string]*Routes
5251
}
@@ -209,35 +208,22 @@ func (r *Router) Mount(path string, Router *Router) *Router {
209208
return r
210209
}
211210

212-
/**
213-
* @info Injects Minima middleware to the stack
214-
* @param {...Handler} [handler] The handler stack to append
215-
* @returns {}
216-
*/
217-
func (r *Router) use(handler ...Handler) {
218-
r.minmiddleware = append(r.minmiddleware, handler...)
219-
}
211+
220212

221213
/**
222214
* @info Injects net/http middleware to the stack
223215
* @param {...func(http.Handler)http.Handler} [handler] The handler stack to append
224216
* @returns {}
225217
*/
226-
func (r *Router) useRaw(handler ...func(http.Handler) http.Handler) {
218+
func (r *Router) use(handler ...func(http.Handler) http.Handler) {
227219
if r.handler != nil {
228220
panic("Minima: Middlewares can't go after the routes are mounted")
229221
}
230222
r.middlewares = append(r.middlewares, handler...)
231223
}
232224

233225
//A dummy function that runs at the end of the middleware stack
234-
func (r *Router) middlewareHTTP(w http.ResponseWriter, rq *http.Request) {
235-
resp := response(w, rq)
236-
req := request(rq)
237-
for _, fn := range r.minmiddleware {
238-
fn(resp, req)
239-
}
240-
}
226+
func (r *Router) middlewareHTTP(w http.ResponseWriter, rq *http.Request) {}
241227

242228
/**
243229
* @info Builds whole middleware stack chain into single handler

0 commit comments

Comments
 (0)