-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
61 lines (52 loc) · 1.25 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
// go-rs/rest-api-framework
// Copyright(c) 2019-2022 Roshan Gade. All rights reserved.
// MIT Licensed
package rest
import "log"
type middleware struct {
pattern *pattern
task Handler
}
type route struct {
method string
pattern *pattern
task Handler
}
type exception struct {
code string
task ErrorHandler
}
type router struct {
middlewares []middleware
routes []route
exceptions []exception
uncaughtException ErrorHandler
}
func (r *router) middleware(str string, task Handler) {
p := &pattern{
value: trim(str) + "/^",
}
if err := p.compile(); err != nil {
log.Fatalf("Failed to compile `%s` due to %v", p.value, err)
}
r.middlewares = append(r.middlewares, middleware{pattern: p, task: task})
}
func (r *router) route(method string, str string, task Handler) {
p := &pattern{
value: trim(str),
}
if err := p.compile(); err != nil {
log.Fatalf("Failed to compile `%s` due to %v", p.value, err)
}
r.routes = append(r.routes, route{
method: method,
pattern: p,
task: task,
})
}
func (r *router) exception(code string, task ErrorHandler) {
r.exceptions = append(r.exceptions, exception{code: code, task: task})
}
func (r *router) unhandledException(task ErrorHandler) {
r.uncaughtException = task
}