forked from admpub/nging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
136 lines (119 loc) · 3.92 KB
/
util.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
127
128
129
130
131
132
133
134
135
136
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package handler
import (
"io"
"os"
"strings"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/subdomains"
"github.com/admpub/log"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/library/common"
"github.com/admpub/nging/application/library/notice"
"github.com/admpub/nging/application/registry/route"
)
var (
NewLister = common.NewLister
Paging = common.Paging
PagingWithPagination = common.PagingWithPagination
PagingWithLister = common.PagingWithLister
PagingWithListerCond = common.PagingWithListerCond
PagingWithSelectList = common.PagingWithSelectList
Ok = common.Ok
Err = common.Err
SendErr = common.SendErr
SendFail = common.SendFail
SendOk = common.SendOk
)
var (
WebSocketLogger = log.GetLogger(`websocket`)
OfficialSQL string
GlobalPrefix string //路由前缀(全局)
FrontendPrefix string //路由前缀(前台)
BackendPrefix string //路由前缀(后台)
//=============================
// 后台路由注册函数
//=============================
Echo = route.Echo
Apply = route.Apply
SetRootGroup = route.SetRootGroup
Register = func(fn func(echo.RouteRegister)) {
route.RegisterToGroup(`@`, fn)
}
// Use “@”符号代表后台网址前缀
Use = func(groupName string, middlewares ...interface{}) {
if groupName != `*` {
groupName = `@` + groupName
}
route.Use(groupName, middlewares...)
}
RegisterToGroup = func(groupName string, fn func(echo.RouteRegister), middlewares ...interface{}) {
route.RegisterToGroup(`@`+groupName, fn, middlewares...)
}
Host = route.Host
)
func init() {
WebSocketLogger.SetLevel(`Info`)
route.AddGroupNamer(func(group string) string {
if len(group) == 0 {
return group
}
if group == `@` {
return BackendPrefix
}
if strings.HasPrefix(group, `@`) {
return BackendPrefix + group[1:]
}
return group
})
}
func User(ctx echo.Context) *dbschema.NgingUser {
user, _ := ctx.Get(`user`).(*dbschema.NgingUser)
return user
}
func RoleList(ctx echo.Context) []*dbschema.NgingUserRole {
roleList, _ := ctx.Get(`roleList`).([]*dbschema.NgingUserRole)
return roleList
}
func Prefix() string {
return Echo().Prefix() + BackendPrefix
}
func NoticeWriter(ctx echo.Context, noticeType string) (wOut io.Writer, wErr io.Writer, err error) {
user := User(ctx)
if user == nil {
return nil, nil, ctx.Redirect(URLFor(`/login`))
}
typ := `service:` + noticeType
notice.OpenMessage(user.Username, typ)
wOut = &com.CmdResultCapturer{Do: func(b []byte) error {
os.Stdout.Write(b)
notice.Send(user.Username, notice.NewMessageWithValue(typ, noticeType, string(b), notice.Succeed))
return nil
}}
wErr = &com.CmdResultCapturer{Do: func(b []byte) error {
os.Stderr.Write(b)
notice.Send(user.Username, notice.NewMessageWithValue(typ, noticeType, string(b), notice.Failed))
return nil
}}
return
}
func URLFor(purl string) string {
return subdomains.Default.URL(BackendPrefix+purl, `backend`)
}
func FrontendURLFor(purl string) string {
return subdomains.Default.URL(FrontendPrefix+purl, `frontend`)
}