-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.go
104 lines (91 loc) · 3.24 KB
/
express.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package express
import (
"net/http"
"strings"
rtr "github.com/nikosEfthias/httpRouter"
)
//__ROUTER
func iterateMiddleWare(middleWares *[]MiddleWare, handler Handle, res http.ResponseWriter, req *http.Request, prm *Params) Handle {
if len(*middleWares) == 0 {
return handler
}
for _, val := range *middleWares {
// TODO:[X] Prevent Double execution
if next := val(res, req, prm); !next {
return func(http.ResponseWriter, *http.Request, Params) {
}
}
}
return handler
}
//New creates the actual router instance
//New must be called before mount which will receive the output of New function
//Most of the times mounter function is required to be called once only
func New() *ServerMux {
return &ServerMux{rtr.New()}
}
//NewRoutes creates the mounter which can be Exported from the package
func NewRoutes(base string) (mounter *RouterMux) {
mounter = &RouterMux{
BasePath: base,
}
return
}
//Mount mounts the divided routes to main router
func (mounter *RouterMux) Mount(router *ServerMux) {
mounter.BasePath = strings.TrimSuffix(mounter.BasePath, "/")
for _, route := range mounter.SubRoutes {
fnc := route.Handler
middleWares := route.MiddleWares
path := route.Path
if !strings.HasSuffix(path, "/") {
path += "/"
}
path = mounter.BasePath + path
switch strings.ToLower(route.Method) {
case "get":
router.GET(path, func(res http.ResponseWriter, req *http.Request, params rtr.Params) {
prm := httpRouterParamsToExpressParams(params)
iterateMiddleWare(&middleWares, fnc, res, req,&prm)(res, req,prm)
})
case "post":
router.POST(path, func(res http.ResponseWriter, req *http.Request, params rtr.Params) {
prm := httpRouterParamsToExpressParams(params)
iterateMiddleWare(&middleWares, fnc, res, req,&prm)(res, req,prm)
})
case "delete":
router.DELETE(path, func(res http.ResponseWriter, req *http.Request, params rtr.Params) {
prm := httpRouterParamsToExpressParams(params)
iterateMiddleWare(&middleWares, fnc, res, req,&prm)(res, req,prm)
})
case "put":
router.PUT(path, func(res http.ResponseWriter, req *http.Request, params rtr.Params) {
prm := httpRouterParamsToExpressParams(params)
iterateMiddleWare(&middleWares, fnc, res, req,&prm)(res, req,prm)
})
}
}
}
//GET request handler
func (mounter *RouterMux) GET(path string, handler Handle, middlewares ...MiddleWare) {
mounter.SubRoutes = append(mounter.SubRoutes, &route{path, "get", middlewares, handler})
}
//POST request handler
func (mounter *RouterMux) POST(path string, handler Handle, middlewares ...MiddleWare) {
mounter.SubRoutes = append(mounter.SubRoutes, &route{path, "post", middlewares, handler})
}
//PUT request handler
func (mounter *RouterMux) PUT(path string, handler Handle, middlewares ...MiddleWare) {
mounter.SubRoutes = append(mounter.SubRoutes, &route{path, "put", middlewares, handler})
}
//DELETE request handler
func (mounter *RouterMux) DELETE(path string, handler Handle, middlewares ...MiddleWare) {
mounter.SubRoutes = append(mounter.SubRoutes, &route{path, "delete", middlewares, handler})
}
func httpRouterParamsToExpressParams(params rtr.Params) Params {
expressParams := make(map[string]string)
for _, param := range params {
expressParams[param.Key] = param.Value
}
return expressParams
}