-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
112 lines (91 loc) · 2.76 KB
/
router.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
105
106
107
108
109
110
111
112
package essen
import (
"net/http"
"github.com/zemirco/uid"
)
type router struct {
get handlerStorage
post handlerStorage
put handlerStorage
}
//Get function is used to add a reqeust handler in a router group to repond against GET HTTP request method.
func (r *router) Get(route string, f func(Response, Request)) {
ff := func(res http.ResponseWriter, req *http.Request) {
//Custom Response Fields
eres := Response{Res: res, ReqMethod: "GET"}
//Custom Request Fields
ereq := Request{Req: req, Uid: uid.New(7)}
ereq.requestBody()
//Call Registered Middleware
f(eres, ereq)
}
r.get[route] = ff
}
//Post function is used to add a request handler in a router group to respond against POST HTTP request method.
func (r *router) Post(route string, f func(Response, Request)) {
ff := func(res http.ResponseWriter, req *http.Request) {
//Custom Response Fields
eres := Response{Res: res, ReqMethod: "POST"}
//Custom Request Fields
ereq := Request{Req: req, Uid: uid.New(7)}
ereq.requestBody()
//Call Registered Middleware
f(eres, ereq)
}
r.post[route] = ff
}
//Put function is used to add a request handler in a router group to respond against PUT HTTP request method.
func (r *router) Put(route string, f func(Response, Request)) {
ff := func(res http.ResponseWriter, req *http.Request) {
//Custom Response Fields
eres := Response{Res: res, ReqMethod: "PUT"}
//Csutom Request Fields
ereq := Request{Req: req, Uid: uid.New(7)}
ereq.requestBody()
//Call Registered Middleware
f(eres, ereq)
}
r.put[route] = ff
}
//UseRouter function is called to register router against a parent route.
//
//Look at Router() method for an example.
func (e Essen) UseRouter(route string, router router) {
//Register Get Routes
for k, v := range router.get {
froute := route + k
paths.get[froute] = v
}
//Register Post Routes
for k, v := range router.post {
froute := route + k
paths.post[froute] = v
}
//Register Put Routes
for k, v := range router.put {
froute := route + k
paths.put[froute] = v
}
}
//Done function is used to clear all router related data once it is passed in UseRouter function.
func (r *router) Done() {
*r = router{}
}
//Router function is used to get router instance which can then be used to group different request handlers.
//
// app := essen.App()
// router := essen.Router()
//
// router.Get("/index", func(res essen.Response, req essen.Request){
// //do something
// })
//
// router.Post("/form", func(res essen.Response, req, essen.Request){
// //do something
// })
//
// app.UseRouter("/user", router)
// router.Done() //Call Done after using a router, Used to achieve memory efficiency.
func (e Essen) Router() router {
return router{get: make(handlerStorage), post: make(handlerStorage), put: make(handlerStorage)}
}