-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
167 lines (139 loc) · 4.46 KB
/
middleware.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
// Copyright 2022 anoweb Author. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package anoweb
import (
"embed"
"github.com/go-the-way/anoweb/context"
"github.com/go-the-way/anoweb/middleware"
"github.com/go-the-way/anoweb/session"
)
type defaultMWState struct {
header bool
logger bool
recovery bool
recoveryHandler func(ctx *context.Context)
recoveryCodeName string
recoveryCodeVal int
recoveryMsgName string
session bool
sessionProvider session.Provider
sessionConfig *session.Config
sessionListener *session.Listener
static bool
staticCache bool
staticRoot string
staticPrefix string
favicon bool
faviconRoute string
faviconFile string
faviconFS *embed.FS
}
// Use Middlewares
func (a *App) Use(middlewares ...middleware.Middleware) *App {
a.middlewares = append(a.middlewares, middlewares...)
return a
}
// UseSession Use Session Middleware
func (a *App) UseSession(provider session.Provider, config *session.Config, listener *session.Listener) *App {
a.defaultMWState.session = true
a.defaultMWState.sessionProvider = provider
a.defaultMWState.sessionConfig = config
a.defaultMWState.sessionListener = listener
return a
}
// UseRecovery Use Recovery Middleware
func (a *App) UseRecovery() *App {
a.defaultMWState.recovery = true
return a
}
// UseLogger Use Logger Middleware
func (a *App) UseLogger() *App {
a.defaultMWState.logger = true
return a
}
// RecoveryConfig Sets Recovery config
func (a *App) RecoveryConfig(codeName string, codeVal int, msgName string) *App {
a.defaultMWState.recoveryCodeName = codeName
a.defaultMWState.recoveryCodeVal = codeVal
a.defaultMWState.recoveryMsgName = msgName
return a
}
// RecoveryHandler Sets Recovery handler
func (a *App) RecoveryHandler(handlers ...func(ctx *context.Context)) *App {
if len(handlers) > 0 {
a.defaultMWState.recoveryHandler = handlers[0]
}
return a
}
// UseStatic Use Static Resources Middleware
func (a *App) UseStatic(cache bool, root, prefix string) *App {
a.defaultMWState.static = true
a.defaultMWState.staticCache = cache
a.defaultMWState.staticRoot = root
a.defaultMWState.staticPrefix = prefix
return a
}
// UseFavicon Use Favicon Middleware
func (a *App) UseFavicon() *App {
a.defaultMWState.favicon = true
return a
}
// FaviconFile Use Favicon File
func (a *App) FaviconFile(file string) *App {
a.defaultMWState.faviconFile = file
return a
}
// FaviconRoute Use Favicon File
func (a *App) FaviconRoute(route string) *App {
a.defaultMWState.faviconRoute = route
return a
}
// FaviconFS Use Favicon FS
func (a *App) FaviconFS(fs *embed.FS) *App {
a.defaultMWState.faviconFS = fs
return a
}
func (a *App) useDefaultMWs() *App {
if a.defaultMWState.session {
a.middlewares[0] = middleware.Session(a.defaultMWState.sessionProvider, a.defaultMWState.sessionConfig, a.defaultMWState.sessionListener)
}
if a.defaultMWState.header {
a.middlewares[1] = middleware.Header()
}
if a.defaultMWState.logger {
a.middlewares[2] = middleware.Logger()
}
if a.defaultMWState.recovery {
if a.defaultMWState.recoveryMsgName != "" {
a.middlewares[3] = middleware.RecoveryWithConfig(a.defaultMWState.recoveryCodeName, a.defaultMWState.recoveryCodeVal, a.defaultMWState.recoveryMsgName, a.defaultMWState.recoveryHandler)
} else {
a.middlewares[3] = middleware.Recovery(a.defaultMWState.recoveryHandler)
}
}
if a.defaultMWState.static {
a.middlewares = append(a.middlewares, middleware.Static(a.defaultMWState.staticCache, a.defaultMWState.staticRoot, a.defaultMWState.staticPrefix))
}
if a.defaultMWState.favicon {
favicon := middleware.Favicon(a.defaultMWState.faviconFile, a.defaultMWState.faviconFS)
a.Get(a.defaultMWState.faviconRoute, favicon.Handler())
}
return a
}
// Middlewares Filter Middlewares
func (a *App) Middlewares() []middleware.Middleware {
middlewares := make([]middleware.Middleware, 0)
for _, m := range a.middlewares {
if m != nil {
middlewares = append(middlewares, m)
}
}
return middlewares
}