-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblueprint.go
More file actions
256 lines (222 loc) · 5.71 KB
/
Copy pathblueprint.go
File metadata and controls
256 lines (222 loc) · 5.71 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package gadm
import (
"bytes"
"encoding/json"
"fmt"
"gadm/isdebug"
"log"
"net/http"
"slices"
"strings"
"github.com/samber/lo"
)
// like flask.Blueprint
//
// | Name | Endpoint | Path |
// |-------|-----------|------------|
// | Foo | foo | /foo/ |
// | | .index | / |
// | | .action | /action |
// | | foo.index | /foo/ |
// | Admin | admin | /admin/ |
// | | .index | / |
//
// A blueprint is A model and dependent pages
type Blueprint struct {
Endpoint string // {foo}.index
Path string // /foo
Name string // Foo
Children map[string]*Blueprint
Parent *Blueprint
Handler http.HandlerFunc
// Custom register into http.ServerMux
RegisterFunc func(*http.ServeMux, string, *Blueprint)
StaticFolder string
TemplateFolder string
// StaticUrlPath
// ErrorHandler
}
// like flask `Blueprint.Register`
func (b *Blueprint) AddChild(child *Blueprint) (err error) {
if child.Path != "" && !strings.HasPrefix(child.Path, "/") {
fmt.Printf("Blueprint(%s) Path: %s not valid", child.Endpoint, child.Path)
}
if b.Children == nil {
b.Children = map[string]*Blueprint{}
}
if _, ok := b.Children[child.Endpoint]; ok {
// allow replace?
err = fmt.Errorf("parent %s duplicated child %s", b.Endpoint, child.Endpoint)
} else {
b.Children[child.Endpoint] = child
child.Parent = b
// fix all children's Parent
fixPointer(b)
}
return err
}
func fixPointer(b *Blueprint) {
for _, c := range b.Children {
if c.Parent == nil {
c.Parent = b
}
fixPointer(c)
}
}
// Register all Blueprint to `http.ServeMux`
func (b *Blueprint) registerTo(mux *http.ServeMux, parent string) {
if b.RegisterFunc != nil {
b.RegisterFunc(mux, parent, b)
} else if b.Handler != nil {
if !strings.HasPrefix(b.Path, "/") {
log.Printf("warning: Blueprint(%s path: %s) not start with /", b.Name, b.Path)
}
if isdebug.On {
log.Printf("%s handle %s", b.Name, parent+b.Path)
}
if strings.HasSuffix(b.Path, "/") {
mux.HandleFunc(parent+b.Path+"{$}", b.Handler)
} else {
mux.HandleFunc(parent+b.Path, b.Handler)
}
} else if b.Endpoint == "static" && b.StaticFolder != "" {
// log.Printf("%s handle %s fs: %s", b.Name, parent+b.Path, b.StaticFolder)
if !strings.HasSuffix(b.Path, "/") {
panic("Blueprint(Name='static').Path should end with /")
}
fs := http.FileServer(http.Dir(b.StaticFolder))
mux.Handle(parent+b.Path, // minified.Middleware(
http.StripPrefix(parent+b.Path, fs))
}
// Avoid `ServerMux` duplicated `Path`
// eg: `index` `index_view` have same `path`
up := map[string]bool{}
for _, child := range b.Children {
if unique := up[child.Path]; !unique {
child.registerTo(mux, parent+b.Path)
up[child.Path] = true
}
}
}
func (b *Blueprint) prefixOf(tail string) string {
arr := []string{}
for c := b.Parent; c != nil; c = c.Parent {
arr = append(arr, c.Path)
}
slices.Reverse(arr)
arr = append(arr, tail)
return strings.Join(arr, "")
}
// endpoint arg like:
// {ep}
// {ep}.index
// {ep}.child.index
// child.index
func (b *Blueprint) GetUrl(endpoint string, qs ...any) (string, error) {
eps := strings.Split(endpoint, ".")
if eps[0] == "" || eps[0] == b.Endpoint || inmap(b.Children, eps[0]) {
i := 1
if inmap(b.Children, eps[0]) {
i = 0
}
pa := []string{b.prefixOf(b.Path)}
match := true
curb := b
for ; i < len(eps); i++ {
child, ok := curb.Children[eps[i]]
if !ok {
match = false
break
}
pa = append(pa, child.Path)
curb = child
}
if match {
res := strings.Join(pa, "")
if len(qs) > 0 {
res += "?" + pairsToQuery(qs...).Encode()
}
return res, nil
}
}
if b.Parent != nil && !strings.HasPrefix(endpoint, ".") {
return b.Parent.GetUrl(endpoint, qs...)
}
return "", fmt.Errorf(`endpoint miss for '%s'`, endpoint)
}
func (b *Blueprint) MarshalJSON() ([]byte, error) {
w := bytes.NewBuffer(nil)
err := json.NewEncoder(w).Encode(map[string]any{
"endpoint": b.Endpoint,
"path": b.Path,
"name": b.Name,
"children": b.Children,
"static_folder": b.StaticFolder,
"template_folder": b.TemplateFolder,
})
return w.Bytes(), err
}
// Tree liked structure
type Menu struct {
Name string
Path string
Icon string
Class string
Roles []string
Children []*Menu
}
func (M *Menu) AddMenu(i *Menu, category ...string) {
parent := M.find(firstOr(category, M.Name))
if parent == nil {
parent = &Menu{Name: firstOr(category)}
M.Children = append(M.Children, parent)
}
parent.Children = append(parent.Children, i)
}
func (M *Menu) find(name string) *Menu {
if M.Name == name {
return M
}
c, _ := lo.Find(M.Children, func(m *Menu) bool {
return m.Name == name
})
return c
}
func (M *Menu) dict(current_path string, user_roles []string) map[string]any {
return map[string]any{
"Name": M.Name,
"Path": M.Path,
"Icon ": M.Icon,
"LoginRequired": len(M.Roles) > 0,
"Class": M.Class,
"IsActive": M.Path != "" && (current_path == M.Path || strings.HasPrefix(current_path, M.Path)),
"IsVisible": M.hasAccess(user_roles),
"IsAccessible": M.hasAccess(user_roles),
"Children": lo.Map(M.Children, func(child *Menu, _ int) map[string]any {
return child.dict(current_path, user_roles)
}),
}
}
func (M *Menu) hasAccess(user_roles []string) bool {
if len(user_roles) == 0 && len(M.Roles) > 0 {
return false
}
if len(M.Roles) == 0 && len(M.Children) == 0 {
return true
}
for _, role := range user_roles {
if role == "admin" {
return true
}
if slices.Contains(M.Roles, role) {
return true
}
}
// check children
for _, child := range M.Children {
if child.hasAccess(user_roles) {
return true
}
}
return false
}