-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
68 lines (52 loc) · 1.26 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
67
68
package goadmin
import "github.com/labstack/echo/v4"
type AdminHandler func(ctx *AppContext) error
func WrapHandler(handleFunc AdminHandler) echo.HandlerFunc {
return func(ctx echo.Context) error {
ac, ok := ctx.(*AppContext)
if !ok {
return ErrContextNotConfigured
}
return handleFunc(ac)
}
}
func AuthByCookie(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
ac, ok := ctx.(*AppContext)
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 withAppContext(app *App) echo.MiddlewareFunc {
return func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
ac := &AppContext{
Context: ctx,
app: app,
}
return handlerFunc(ac)
}
}
}
func withViewData(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
ac := ctx.(*AppContext)
data := &Data{}
user, ok := ac.Get(UserContextKey).(*User)
if ok {
data.User = user
}
sortOrder := -100
data.Breadcrumbs.Add("Dashboard", ac.URL("/"), &sortOrder)
ac.Set(DataContextKey, data)
return handlerFunc(ctx)
}
}