-
Notifications
You must be signed in to change notification settings - Fork 1
/
routepage.go
88 lines (83 loc) · 2.18 KB
/
routepage.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
package routepage
import (
"bytes"
"fmt"
"html/template"
"github.com/admpub/log"
"github.com/admpub/webx/application/dbschema"
xMW "github.com/admpub/webx/application/middleware"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/defaults"
)
func Apply(router *echo.Echo, funcMap map[string]interface{}) error {
cond := db.NewCompounds()
cond.AddKV(`disabled`, `N`)
f := dbschema.NewOfficialCommonRoutePage(defaults.NewMockContext())
_, err := f.ListByOffset(nil, func(r db.Result) db.Result {
return r.OrderBy(`id`)
}, 0, -1, cond.And())
for _, v := range f.Objects() {
Register(v, router, funcMap)
}
return err
}
func Register(v *dbschema.OfficialCommonRoutePage, router *echo.Echo, funcMap map[string]interface{}) echo.IRouter {
handler, err := buildHandler(v, funcMap)
if err != nil {
log.Error(err)
return nil
}
log.Info(`Register custom routing page: `, v.Name, ` (`, v.Method, ` `, v.Route, `)`)
return router.Route(v.Method, v.Route, handler)
}
func buildHandler(v *dbschema.OfficialCommonRoutePage, funcMap map[string]interface{}) (func(ctx echo.Context) error, error) {
var (
t *template.Template
data = echo.H{}
err error
)
if len(v.PageVars) > 0 {
err = com.JSONDecode(com.Str2bytes(v.PageVars), &data)
if err != nil {
v.PageContent = `[buildHandler]parse page vars: ` + err.Error()
} else {
name := com.Md5(v.PageContent)
t = template.New(name)
t.Funcs(funcMap)
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf(`%v`, e)
}
}()
_, err = t.Parse(v.PageContent)
}
}
return func(ctx echo.Context) error {
var content string
if t != nil {
buf := bytes.NewBuffer(nil)
err := t.Execute(buf, xMW.DefaultRenderDataWrapper(ctx, data))
if err != nil {
content = err.Error()
} else {
content = buf.String()
}
} else {
content = v.PageContent
}
switch v.PageType {
case `redirect`:
return ctx.Redirect(content)
case `html`:
return ctx.HTML(content)
case `xml`:
return ctx.XMLBlob(com.Str2bytes(content))
case `json`:
return ctx.JSONBlob(com.Str2bytes(content))
default: //text
return ctx.String(content)
}
}, nil
}