-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
80 lines (68 loc) · 1.5 KB
/
route.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
package api
import (
"net/http"
"110yards.ca/libs/go/core/logger"
"github.com/go-chi/chi/v5"
)
type Method int
const (
Method_Get Method = iota
Method_Put
Method_Post
Method_Delete
)
type Route struct {
Method Method
Path string
Handler http.HandlerFunc
AllowAnonymous bool
}
func AddRoutes(router *chi.Mux, routes []Route) {
for _, route := range routes {
AddRoute(router, route)
}
}
func AddRoute(router *chi.Mux, route Route) {
switch route.Method {
case Method_Get:
router.Get(route.Path, route.Handler)
logger.Infof("GET %s", route.Path)
case Method_Post:
router.Post(route.Path, route.Handler)
logger.Infof("POST %s", route.Path)
case Method_Put:
router.Put(route.Path, route.Handler)
logger.Infof("PUT %s", route.Path)
case Method_Delete:
router.Delete(route.Path, route.Handler)
logger.Infof("DELETE %s", route.Path)
}
}
func Get(path string, handler http.HandlerFunc) Route {
return Route{
Method: Method_Get,
Path: path,
Handler: handler,
}
}
func Put(path string, handler http.HandlerFunc) Route {
return Route{
Method: Method_Put,
Path: path,
Handler: handler,
}
}
func Post(path string, handler http.HandlerFunc) Route {
return Route{
Method: Method_Post,
Path: path,
Handler: handler,
}
}
func Delete(path string, handler http.HandlerFunc) Route {
return Route{
Method: Method_Delete,
Path: path,
Handler: handler,
}
}