-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2.go
272 lines (248 loc) · 5.64 KB
/
v2.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package v2
import (
"context"
"fmt"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
"time"
)
type Json struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
}
type ApiRes struct {
ctx context.Context
json *Json
}
func Success(ctx context.Context) *ApiRes {
json := Json{
Code: 1,
}
var a = ApiRes{
ctx: ctx,
json: &json,
}
return &a
}
func Error(ctx context.Context) *ApiRes {
json := Json{
Code: 0,
}
var a = ApiRes{
ctx: ctx,
json: &json,
}
return &a
}
func (a *ApiRes) SetCode(code int) *ApiRes {
a.json.Code = code
return a
}
func (a *ApiRes) SetData(data interface{}) *ApiRes {
a.json.Data = data
return a
}
func (a *ApiRes) SetMsg(msg string) *ApiRes {
a.json.Msg = msg
return a
}
func (a *ApiRes) End() {
from := g.RequestFromCtx(a.ctx)
from.Header.Set("Access-Control-Expose-Headers", "Set-Cookie")
from.Response.Status = 200
from.Response.WriteJson(a.json)
return
}
func (a *ApiRes) FileDownload(path, name string) {
from := g.RequestFromCtx(a.ctx)
from.Response.ServeFileDownload(path)
return
}
func (a *ApiRes) FileSelect(path string) {
from := g.RequestFromCtx(a.ctx)
from.Response.ServeFile(path)
return
}
func LoginJson(r *ghttp.Request, msg string, data ...interface{}) {
var info interface{}
if len(data) > 0 {
info = data[0]
} else {
info = nil
}
r.Response.WriteJsonExit(Json{
Code: 1,
Data: info,
Msg: msg,
})
}
func ResponseJson(ctx context.Context, data interface{}) {
g.RequestFromCtx(ctx).Response.WriteJson(data)
return
}
type PageSize struct {
CurrentPage int `json:"current_page"`
Data interface{} `json:"data"`
LastPage int `json:"last_page"`
PerPage int `json:"per_page"`
Total int `json:"total"`
}
// SetPage 设置分页
func SetPage(page, limit, total int, data interface{}) *PageSize {
var size = new(PageSize)
if page == 1 {
size.LastPage = 1
} else {
size.LastPage = page - 1
}
size.PerPage = limit
size.Total = total
size.CurrentPage = page
size.Data = data
return size
}
// MiddlewareError 异常处理中间件
func MiddlewareError(r *ghttp.Request) {
//r.Response.CORSDefault()
r.Middleware.Next()
if err := r.GetError(); err != nil {
bo := gstr.Contains(err.Error(), ": ")
msg := ""
if bo {
msg = gstr.SubStrFromEx(err.Error(), ": ")
} else {
msg = err.Error()
}
r.Response.ClearBuffer()
json := Json{
Code: 0,
Msg: msg,
}
r.Response.Status = 200
r.Response.WriteJson(json)
}
}
// AuthBase 鉴权中间件,只有前端或者后端登录成功之后才能通过
func AuthBase(r *ghttp.Request, name string) {
info, err := r.Session.Get(name, nil)
if err != nil {
panic(err.Error())
}
if !info.IsEmpty() {
r.Middleware.Next()
} else {
NoLogin(r)
}
}
func AuthAdmin(r *ghttp.Request) {
AuthBase(r, "admin")
}
func AuthIndex(r *ghttp.Request) {
AuthBase(r, "user")
}
func NoLogin(r *ghttp.Request) {
r.Response.Status = 401
r.Response.WriteJsonExit(Json{
Code: 401,
Data: nil,
Msg: "请登录后操作",
})
}
func CreateFileDir() error {
path := gfile.Pwd() + "/resource"
if !gfile.IsDir(path) {
if err := gfile.Mkdir(path); err != nil {
return err
}
if err := gfile.Mkdir(path + "/public/upload"); err != nil {
return err
}
}
return nil
}
func CreateDB(ctx context.Context, sqlHost, sqlPort, sqlRoot, sqlPass, baseName string, debug bool) {
cfg := gcfg.Instance()
for {
cfgBase, _ := cfg.Get(ctx, "database")
if cfgBase == nil {
gdb.SetConfig(gdb.Config{
"default": gdb.ConfigGroup{
gdb.ConfigNode{
Host: sqlHost,
Port: sqlPort,
User: sqlRoot,
Pass: sqlPass,
Name: baseName,
Timezone: "Local",
Type: "mysql",
Role: "master",
CreatedAt: "create_time",
UpdatedAt: "update_time",
Debug: debug,
},
}})
}
time.Sleep(time.Minute * 10)
}
}
func Start(address, agent string, maxSessionTime time.Duration, isApi bool, skipUrl string, maxBody ...int64) *ghttp.Server {
s := g.Server()
s.SetAddr(address)
s.SetDumpRouterMap(false)
path := gfile.Pwd() + "/resource/public/upload"
if !gfile.IsDir(path) {
err := gfile.Mkdir(path)
if err != nil {
panic(err.Error())
}
_ = gfile.Mkdir(gfile.Pwd() + "/resource/template")
_ = gfile.Mkdir(gfile.Pwd() + "/resource/scripts")
_ = gfile.Mkdir(gfile.Pwd() + "/resource/public/html")
_ = gfile.Mkdir(gfile.Pwd() + "/resource/public/resource/css")
_ = gfile.Mkdir(gfile.Pwd() + "/resource/public/resource/image")
_ = gfile.Mkdir(gfile.Pwd() + "/resource/public/resource/js")
}
s.SetServerRoot(gfile.Pwd() + "/resource")
s.AddSearchPath(path)
s.AddStaticPath("/upload", path)
err := s.SetLogPath(gfile.Pwd() + "/resource/log")
if err != nil {
fmt.Println(err)
}
s.SetLogLevel("all")
s.SetLogStdout(false)
if len(maxBody) > 0 {
s.SetClientMaxBodySize(maxBody[0])
} else {
s.SetClientMaxBodySize(200 * 1024 * 1024)
}
s.SetFormParsingMemory(50 * 1024 * 1024)
if isApi {
s.SetOpenApiPath("/api.json")
s.SetSwaggerPath("/swagger")
}
s.SetMaxHeaderBytes(1024 * 20)
s.SetErrorStack(true)
s.SetSessionIdName("zrSession")
s.SetAccessLogEnabled(true)
s.SetSessionMaxAge(maxSessionTime)
err = s.SetConfigWithMap(g.Map{
"sessionPath": gfile.Pwd() + "/resource/session",
"serverAgent": agent,
})
if err != nil {
fmt.Println(err)
}
s.Use(MiddlewareError)
if skipUrl != "" {
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.RedirectTo(skipUrl)
})
}
return s
}