-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
129 lines (115 loc) · 3.32 KB
/
app.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
package plover
import (
"net/http"
"strings"
)
type App struct {
*Container
Route *Route
NotFound NotFound
}
func (app *App) SetNotFound(NotFound NotFound) {
app.NotFound = NotFound
}
func (app *App) dispatch(PATH string, methods []Controllers, middlewares []Middleware) bool {
for _, control := range methods {
if strings.EqualFold(control.PATH, PATH) {
initInvoke(control.Handle, app)
defer destructInvoke(control.Handle)
action := control.Action
var intercept bool
if len(middlewares) > 0 && !control.WithoutMiddleware {
for _, middleware := range middlewares {
action, intercept = middleware(app, action)
if intercept { // 是否拦截
action()
return true
}
}
}
action()
return true
}
}
return false
}
func (app *App) Crossdomain(allowed bool) {
app.Set("Crossdomain", allowed)
}
func (app *App) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
app.Init(responseWriter, request)
if Crossdomain, ok := app.Get("Crossdomain").(bool); Crossdomain && ok {
app.Response.Header().Set("Access-Control-Allow-Origin", request.Header.Get("Origin"))
app.Response.Header().Set("Access-Control-Allow-Headers", "authorization, language, cache-control, content-type, if-match, if-modified-since, if-none-match, if-unmodified-since, x-requested-with")
app.Response.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE")
app.Response.Header().Set("Access-Control-Max-Age", "1728000")
app.Response.Header().Set("Access-Control-Allow-Credentials", "true")
app.Response.Header().Set("Access-Control-Expose-Headers", "token")
if request.Method == http.MethodOptions {
app.Response.JsonData(nil)
}
}
methods := app.Route.GetMethod(strings.ToUpper(request.Method))
middlewares := app.Route.GetMiddleware()
PATH := app.Assets.AnalysisUri(request.URL.Path)
if ok := app.dispatch(PATH, methods, middlewares); ok {
return
}
// Any 路由匹配
if ok := app.dispatch(PATH, app.Route.GetMethod("*"), middlewares); ok {
return
}
// 分组调用
for _, group := range app.Route.Groups {
if group.HasBasic(PATH) {
methods = group.GetMethod(strings.ToUpper(request.Method))
middlewares = group.GetMiddleware()
path := app.Assets.AnalysisUri(strings.TrimPrefix(PATH, group.basic))
if ok := app.dispatch(path, methods, middlewares); ok {
return
}
// Any 路由匹配
if ok := app.dispatch(path, group.GetMethod("*"), middlewares); ok {
return
}
}
}
if hasAssets := app.Assets.Handle(responseWriter, request); hasAssets {
return
}
if app.NotFound != nil {
app.NotFound(app)
return
}
http.NotFound(responseWriter, request)
return
}
func (app *App) Run(addr ...string) {
var monitor string
if len(addr) > 0 {
monitor = addr[0]
} else {
var host string
if strings.EqualFold(app.Config.GetString("app.host"), "") {
host = "0.0.0.0"
} else {
host = app.Config.GetString("app.host")
}
var port string
if strings.EqualFold(app.Config.GetString("app.port"), "") {
port = "8888"
} else {
port = app.Config.GetString("app.port")
}
monitor = host + ":" + port
}
http.ListenAndServe(monitor, app)
}
func NewApp() *App {
container := &Container{}
container.Assets = new(Assets)
container.Config = NewConfig()
route := new(Route)
app := &App{container, route, nil}
return app
}