/
group.go
126 lines (108 loc) · 2.79 KB
/
group.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
package api
import (
"fmt"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"github.com/eqto/api-server/websocket"
)
var (
normalizeRegex *regexp.Regexp
)
// Group ..
type Group struct {
s *Server
prefixPath string
name string
}
func (g *Group) formatPath(path string) string {
path = strings.TrimPrefix(path, `/`)
return fmt.Sprintf(`%s/%s`, g.prefixPath, path)
}
func (g *Group) SetPrefixPath(path string) {
path = strings.TrimPrefix(path, `/`)
path = strings.TrimSuffix(path, `/`)
g.prefixPath = `/` + path
}
func (g *Group) Get(path string) *Route {
return g.getRoute(MethodGet, g.formatPath(path))
}
func (g *Group) GetAction(f func(Context) error) *Route {
return g.action(MethodGet, f)
}
func (g *Group) GetSecureAction(f func(Context) error) *Route {
return g.action(MethodGet, f).Secure()
}
func (g *Group) Post(path string) *Route {
return g.getRoute(MethodPost, g.formatPath(path))
}
func (g *Group) HandleWebsocket(path string) *Websocket {
route := g.getRoute(MethodGet, g.formatPath(path))
route.isWs = true
if g.s.wsServ == nil {
g.s.wsServ = websocket.NewServer()
}
return &Websocket{wsServ: g.s.wsServ}
}
func (g *Group) PostAction(f func(Context) error) *Route {
return g.action(MethodPost, f)
}
func (g *Group) PostSecureAction(f func(Context) error) *Route {
return g.action(MethodPost, f).Secure()
}
func (g *Group) AddMiddleware(f func(Context) error) Middleware {
m := &middlewareContainer{f: f, group: g.name}
g.s.middlewares = append(g.s.middlewares, m)
return m
}
func (g *Group) action(method string, f func(Context) error) *Route {
ptr := reflect.ValueOf(f).Pointer()
name := runtime.FuncForPC(ptr).Name()
name = filepath.Base(name)
if strings.Count(name, `.`) > 1 {
g.s.logger.E(`unsupported add inline function`, name)
return &Route{logger: g.s.logger}
}
path := strings.ReplaceAll(name, `.`, `/`)
route := g.getRoute(method, g.formatPath(path))
route.AddAction(f).AssignTo(`data`)
return route
}
func (g *Group) getRoute(method, path string) *Route {
switch method {
case MethodGet, MethodPost:
default:
return nil
}
if g.s.normalize {
path = normalizePath(path)
}
route, ok := g.s.routeMap[method][path]
if !ok {
route = &Route{logger: g.s.logger}
g.s.routeMap[method][path] = route
g.s.logger.D(fmt.Sprintf(`Register route: %s %s`, method, path))
}
route.UseGroup(g.name)
return route
}
func normalizePath(path string) string {
if normalizeRegex == nil {
normalizeRegex = regexp.MustCompile(`([A-Z]+)`)
}
path = normalizeRegex.ReplaceAllString(path, `_$1`)
path = strings.ToLower(path)
validPath := false
if strings.HasPrefix(path, `/`) {
validPath = true
path = path[1:]
}
path = strings.TrimPrefix(path, `_`)
if validPath {
path = `/` + path
}
path = strings.ReplaceAll(path, `/_`, `/`)
return path
}