-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.go
171 lines (141 loc) · 3.96 KB
/
routes.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package essen
import (
"log"
"net/http"
"path/filepath"
"github.com/zemirco/uid"
)
type handlerStorage map[string]func(http.ResponseWriter, *http.Request)
type staticHandlerStorage map[string]http.Handler
type pathCache struct {
post handlerStorage
put handlerStorage
get handlerStorage
head handlerStorage
use handlerStorage
static staticHandlerStorage
}
var paths = pathCache{
get: make(handlerStorage),
post: make(handlerStorage),
put: make(handlerStorage),
head: make(handlerStorage),
use: make(handlerStorage),
static: make(staticHandlerStorage),
}
//Head is a function to register a route against HTTP HEAD request method
//
// app := essen.App()
//
// app.Head("/", func(res app.Response, req app.Request){
// //do something
// })
func (e Essen) Head(route string, f func(Response, Request)) {
ff := func(res http.ResponseWriter, req *http.Request) {
//Custom Response Fields
eres := Response{Res: res, ReqMethod: "HEAD"}
//Custom Request Fields
ereq := Request{Req: req, Uid: uid.New(7)}
ereq.requestBody()
//Call Registered Middleware
f(eres, ereq)
}
paths.head[route] = ff
}
//Get is a function to register a route against HTTP GET request method
//
// app := essen.App()
//
// app.Get("/", func(res app.Response, req app.Request){
// //do something
// })
func (e Essen) 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)
}
paths.get[route] = ff
}
//Post is a function to register a route against HTTP POST request method
//
// app := essen.App()
//
// app.Post("/", func(res app.Response, req app.Request){
// //do something
// })
func (e Essen) 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)
}
paths.post[route] = ff
}
//Put is a function to register a route against HTTP PUT request method
//
// app := essen.App()
//
// app.Put("/", func(res app.Response, req app.Request){
// //do something
// })
func (e Essen) Put(route string, f func(Response, Request)) {
ff := func(res http.ResponseWriter, req *http.Request) {
//Custom Response Fields
eres := Response{Res: res, ReqMethod: "PUT"}
//Custom Request Fields
ereq := Request{Req: req, Uid: uid.New(7)}
ereq.requestBody()
//Call Registered Middleware
f(eres, ereq)
}
paths.put[route] = ff
}
//Use is a function to register a route against any HTTP request method.
//
// app := essen.App()
//
// app.Use("/", func(res app.Response, req app.Request){
// //do something
// })
func (e Essen) Use(route string, f func(Response, Request)) {
ff := func(res http.ResponseWriter, req *http.Request) {
//Custom Response Field
eres := Response{Res: res, ReqMethod: req.Method}
//Custom Request Fields
ereq := Request{Req: req, Uid: uid.New(7)}
ereq.requestBody()
//Call Registered Middleware
f(eres, ereq)
}
paths.use[route] = ff
}
//Static is function to serve static files on reciept HTTP request.
//
//`path` parameter can either be a directory path or a file path.
//
// app := essen.App()
// app.Static("/static", "./static_dir")
func (e Essen) Static(route string, path string) {
//Create error instance
ee := EssenError{nilval: true}
//Generate absolute path
path, err := filepath.Abs(path)
//Check for absolute path generation error
if err != nil {
ee.nilval = false
ee.errortype = "PathError"
ee.message = "String provided cannot be used for absolute path conversion"
log.Panic(ee.Error())
}
//Create file serving handler
paths.static[route] = http.FileServer(http.Dir(path))
}