-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
66 lines (51 loc) · 1.25 KB
/
middleware.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
package goadmin
import "github.com/labstack/echo/v4"
type AdminHandler func(ctx *AdminContext) error
func WrapHandler(handleFunc AdminHandler) echo.HandlerFunc {
return func(ctx echo.Context) error {
ac, ok := ctx.(*AdminContext)
if !ok {
return ErrContextNotConfigured
}
return handleFunc(ac)
}
}
func AuthByCookie(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
ac, ok := ctx.(*AdminContext)
if !ok {
return ErrContextNotConfigured
}
u, err := authByCookie(ac)
if err != nil {
return err
}
u.Current = true
ctx.Set(UserContextKey, u)
return withViewData(handlerFunc)(ctx)
}
}
func withAdminContext(admin *Admin) echo.MiddlewareFunc {
return func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
ac := &AdminContext{
Context: ctx,
admin: admin,
}
return handlerFunc(ac)
}
}
}
func withViewData(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
ac := ctx.(*AdminContext)
data := &Data{}
user, ok := ac.Get(UserContextKey).(*User)
if ok {
data.User = user
}
data.Breadcrumbs.Add("Dashboard", ac.URL("/"), -100)
ac.Set(DataContextKey, data)
return handlerFunc(ctx)
}
}