-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapplicationBuilder.go
277 lines (244 loc) · 8.7 KB
/
applicationBuilder.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
273
274
275
276
277
package webapi
import (
"fmt"
"net/http"
"net/http/pprof"
"strconv"
"strings"
"github.com/farseer-go/collections"
"github.com/farseer-go/fs/configure"
"github.com/farseer-go/fs/container"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/core/eumLogLevel"
"github.com/farseer-go/fs/exception"
"github.com/farseer-go/fs/flog"
"github.com/farseer-go/fs/modules"
"github.com/farseer-go/webapi/context"
"github.com/farseer-go/webapi/controller"
"github.com/farseer-go/webapi/middleware"
"github.com/farseer-go/webapi/minimal"
"github.com/farseer-go/webapi/websocket"
)
type applicationBuilder struct {
area string
mux *serveMux
certFile string // https证书
keyFile string // https证书 key
tls bool // 是否使用https
MiddlewareList collections.List[context.IMiddleware] // 注册的中间件
printRoute bool // 打印所有路由信息到控制台
useApiResponse bool // 是否使用了ApiResponse中间件
addr string // 地址
hostAddress string // 完整地址
}
func NewApplicationBuilder() *applicationBuilder {
return &applicationBuilder{
area: "/",
mux: new(serveMux),
MiddlewareList: collections.NewList[context.IMiddleware](),
}
}
func (r *applicationBuilder) RegisterMiddleware(m context.IMiddleware) {
r.MiddlewareList.Add(m)
}
// RegisterPOST 注册单个Api(支持占位符,例如:/{cateId}/{Id})
func (r *applicationBuilder) RegisterPOST(route string, actionFunc any, params ...string) {
r.registerAction(Route{Url: route, Method: "POST", Action: actionFunc, Params: params})
}
// RegisterGET 注册单个Api(支持占位符,例如:/{cateId}/{Id})
func (r *applicationBuilder) RegisterGET(route string, actionFunc any, params ...string) {
r.registerAction(Route{Url: route, Method: "GET", Action: actionFunc, Params: params})
}
// RegisterPUT 注册单个Api(支持占位符,例如:/{cateId}/{Id})
func (r *applicationBuilder) RegisterPUT(route string, actionFunc any, params ...string) {
r.registerAction(Route{Url: route, Method: "PUT", Action: actionFunc, Params: params})
}
// RegisterDELETE 注册单个Api(支持占位符,例如:/{cateId}/{Id})
func (r *applicationBuilder) RegisterDELETE(route string, actionFunc any, params ...string) {
r.registerAction(Route{Url: route, Method: "DELETE", Action: actionFunc, Params: params})
}
// RegisterWS 注册WebSocket(支持占位符,例如:/{cateId}/{Id})
func (r *applicationBuilder) RegisterWS(route string, actionFunc any) {
r.registerAction(Route{Url: route, Method: "WS", Action: actionFunc})
}
// RegisterRoutes 批量注册路由
func (r *applicationBuilder) RegisterRoutes(routes ...Route) {
for i := 0; i < len(routes); i++ {
if strings.ToUpper(routes[i].Method) == "WS" {
r.registerWS(routes[i])
} else {
r.registerAction(routes[i])
}
}
}
// RegisterController 自动注册控制器下的所有Action方法
func (r *applicationBuilder) RegisterController(c controller.IController) {
lst := controller.Register(r.area, c)
for i := 0; i < lst.Count(); i++ {
r.mux.HandleRoute(lst.Index(i))
}
}
// registerAction 注册单个Api
func (r *applicationBuilder) registerAction(route Route) {
// 需要先依赖模块
modules.ThrowIfNotLoad(Module{})
route.Url = strings.Trim(route.Url, " ")
route.Url = strings.TrimLeft(route.Url, "/")
if route.Url == "" {
flog.Panicf("注册路由失败:%s必须设置值", flog.Colors[eumLogLevel.Error]("routing"))
}
r.mux.HandleRoute(minimal.Register(r.area, route.Method, route.Url, route.Action, route.Filters, route.Params...))
}
// registerWS 注册websocket
func (r *applicationBuilder) registerWS(route Route) {
// 需要先依赖模块
modules.ThrowIfNotLoad(Module{})
route.Url = strings.Trim(route.Url, " ")
route.Url = strings.TrimLeft(route.Url, "/")
if route.Url == "" {
flog.Panicf("注册websocket路由失败:%s必须设置值", flog.Colors[eumLogLevel.Error]("routing"))
}
r.mux.HandleRoute(websocket.Register(r.area, route.Method, route.Url, route.Action, route.Filters, route.Params...))
}
// Area 设置区域
func (r *applicationBuilder) Area(area string, f func()) {
if !strings.HasPrefix(area, "/") {
area = "/" + area
}
if !strings.HasSuffix(area, "/") {
area += "/"
}
r.area = area
// 执行注册
f()
// 执行完后,恢复区域为"/"
r.area = "/"
}
// UseCors 使用CORS中间件
func (r *applicationBuilder) UseCors() {
r.MiddlewareList.Insert(0, &middleware.Cors{})
}
// UseStaticFiles 支持静态目录,在根目录./wwwroot中的文件,直接以静态文件提供服务
func (r *applicationBuilder) UseStaticFiles() {
// 默认wwwroot为静态目录
r.mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./wwwroot"))))
}
// UsePprof 是否同时开启pprof
func (r *applicationBuilder) UsePprof() {
r.mux.HandleFunc("/debug/pprof/", pprof.Index)
r.mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
// UseSession 开启Session
func (r *applicationBuilder) UseSession() {
if !container.IsRegister[ISessionMiddlewareCreat]() {
panic("要使用Session,请加载模块:session-redis")
}
r.RegisterMiddleware(container.Resolve[ISessionMiddlewareCreat]().Create())
}
func (r *applicationBuilder) UseWebApi() {
r.RegisterMiddleware(&middleware.UrlRewriting{})
}
// UseApiResponse 支持ApiResponse结构
func (r *applicationBuilder) UseApiResponse() {
r.useApiResponse = true
r.RegisterMiddleware(&middleware.ApiResponse{})
}
// UseValidate 使用字段验证器
func (r *applicationBuilder) UseValidate() {
middleware.InitValidate()
r.RegisterMiddleware(&middleware.Validate{})
}
// UseTLS 使用https
func (r *applicationBuilder) UseTLS(certFile, keyFile string) {
r.certFile = certFile
r.keyFile = keyFile
r.tls = true
}
// Run 运行Web服务(默认8888端口)
func (r *applicationBuilder) Run(params ...string) {
// 设置监听地址
if len(params) > 0 {
r.addr = params[0]
}
if r.addr == "" {
r.addr = configure.GetString("WebApi.Url")
if r.addr == "" {
r.addr = ":8888"
}
}
r.addr = strings.TrimSuffix(r.addr, "/")
// 初始化中间件
r.mux.initMiddleware(r.MiddlewareList)
if r.tls {
r.hostAddress = fmt.Sprintf("https://127.0.0.1%s", r.addr)
} else {
r.hostAddress = fmt.Sprintf("http://127.0.0.1%s", r.addr)
}
// 打印路由地址
r.print()
if strings.HasPrefix(r.addr, ":") {
flog.Infof("Web service is started:%s/", r.hostAddress)
}
if apiDoc, exists := r.mux.m["/doc/api"]; exists {
flog.Infof("Api Document is:%s%s", r.hostAddress, apiDoc.RouteUrl)
}
if r.tls {
flog.Info(http.ListenAndServeTLS(r.addr, r.certFile, r.keyFile, r.mux))
} else {
flog.Info(http.ListenAndServe(r.addr, r.mux))
}
}
// 打印路由地址
func (r *applicationBuilder) print() {
if r.printRoute {
lstRoute := collections.NewList[context.HttpRoute]()
for _, httpRoute := range r.mux.m {
if httpRoute.Controller == nil && httpRoute.Action == nil && httpRoute.RouteUrl == "/" && httpRoute.Method.Count() == 0 {
continue
}
lstRoute.Add(*httpRoute)
}
lstRoute.OrderBy(func(item context.HttpRoute) any {
return item.RouteUrl
}).Where(func(item context.HttpRoute) bool {
return item.RouteUrl != "/doc/api"
}).For(func(index int, httpRoute *context.HttpRoute) {
method := strings.Join(httpRoute.Method.ToArray(), "|")
if method == "" {
method = "GET"
}
fmt.Printf("%s:%s %s%s\n", flog.Blue(strconv.Itoa(index+1)), flog.Red(method), r.hostAddress, httpRoute.RouteUrl)
})
fmt.Println("---------------------------------------")
}
}
// PrintRoute 打印所有路由信息到控制台
func (r *applicationBuilder) PrintRoute() {
r.printRoute = true
}
// UseHealthCheck 【GET】开启健康检查(默认route = "/healthCheck")
func (r *applicationBuilder) UseHealthCheck(routes ...string) {
if len(routes) == 0 {
routes = []string{"/healthCheck"}
}
for _, route := range routes {
r.RegisterGET(route, func() []string {
healthChecks := container.ResolveAll[core.IHealthCheck]()
lstError := collections.NewList[string]()
lstSuccess := collections.NewList[string]()
for _, healthCheck := range healthChecks {
item, err := healthCheck.Check()
if err == nil {
lstSuccess.Add(item)
} else {
lstError.Add(err.Error())
}
}
exception.ThrowWebExceptionBool(lstError.Count() > 0, 403, lstError.ToString(","))
return lstSuccess.ToArray()
})
}
}