/
spring-echo.go
189 lines (153 loc) · 5.11 KB
/
spring-echo.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
/*
* Copyright 2012-2019 the original author or authors.
*
* 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
*
* https://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 SpringEcho
import (
"context"
"github.com/go-spring/go-spring-parent/spring-logger"
"github.com/go-spring/go-spring-parent/spring-utils"
"github.com/go-spring/go-spring-web"
"github.com/labstack/echo"
)
type route struct {
fn SpringWeb.Handler // Web 处理函数
wildCardName string // 通配符的名称
}
// Container 适配 echo 的 Web 容器
type Container struct {
*SpringWeb.BaseWebContainer
echoServer *echo.Echo
routes map[string]route // 记录所有通过 spring echo 注册的路由
}
// NewContainer Container 的构造函数
func NewContainer(config SpringWeb.ContainerConfig) *Container {
c := &Container{
BaseWebContainer: SpringWeb.NewBaseWebContainer(config),
routes: make(map[string]route),
}
return c
}
// Deprecated: Filter 机制可完美替代中间件机制,不再需要定制化
func (c *Container) SetEchoServer(e *echo.Echo) {
c.echoServer = e
}
// Start 启动 Web 容器,非阻塞
func (c *Container) Start() {
c.PreStart()
// 使用默认的 echo 容器
if c.echoServer == nil {
e := echo.New()
e.HideBanner = true
c.echoServer = e
}
var cFilters []SpringWeb.Filter
if f := c.GetLoggerFilter(); f != nil {
cFilters = append(cFilters, f)
}
if f := c.GetRecoveryFilter(); f != nil {
cFilters = append(cFilters, f)
}
cFilters = append(cFilters, c.GetFilters()...)
// 添加容器级别的过滤器,这样在路由不存在时也会调用这些过滤器
c.echoServer.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(echoCtx echo.Context) error {
// 如果 method+path 是 spring echo 注册过的,那么可以保证 WebContext
// 的 Handler 是准确的,否则是不准确的,请优先使用 spring echo 注册路由。
key := echoCtx.Request().Method + echoCtx.Path()
if r, ok := c.routes[key]; ok {
NewContext(r.fn, r.wildCardName, echoCtx)
} else {
NewContext(nil, echoCtx.Path(), echoCtx)
}
filters := append(cFilters, SpringWeb.HandlerFilter(Echo(next)))
chain := SpringWeb.NewDefaultFilterChain(filters)
chain.Next(WebContext(echoCtx))
return nil
}
})
// 映射 Web 处理函数
for _, mapper := range c.Mappers() {
c.PrintMapper(mapper)
path, wildCardName := SpringWeb.ToPathStyle(mapper.Path(), SpringWeb.EchoPathStyle)
fn := HandlerWrapper(mapper.Handler(), wildCardName, mapper.Filters())
for _, method := range SpringWeb.GetMethod(mapper.Method()) {
c.echoServer.Add(method, path, fn)
c.routes[method+path] = route{
fn: mapper.Handler(),
wildCardName: wildCardName,
}
}
}
// 设置参数校验器
c.echoServer.Validator = SpringWeb.Validator
// 启动 echo 容器
go func() {
// TODO 应用 ReadTimeout 和 WriteTimeout。
var err error
if cfg := c.Config(); cfg.EnableSSL {
err = c.echoServer.StartTLS(c.Address(), cfg.CertFile, cfg.KeyFile)
} else {
err = c.echoServer.Start(c.Address())
}
SpringLogger.Infof("exit echo server on %s return %s", c.Address(), SpringUtils.ErrorToString(err))
}()
}
// Stop 停止 Web 容器,阻塞
func (c *Container) Stop(ctx context.Context) {
err := c.echoServer.Shutdown(ctx)
SpringLogger.Infof("shutdown echo server on %s return %s", c.Address(), SpringUtils.ErrorToString(err))
}
// HandlerWrapper Web 处理函数包装器
func HandlerWrapper(fn SpringWeb.Handler, wildCardName string, filters []SpringWeb.Filter) echo.HandlerFunc {
return func(echoCtx echo.Context) error {
webCtx := WebContext(echoCtx)
if webCtx == nil {
webCtx = NewContext(fn, wildCardName, echoCtx)
}
SpringWeb.InvokeHandler(webCtx, fn, filters)
return nil
}
}
/////////////////// handler //////////////////////
// echoHandler 封装 Echo 处理函数
type echoHandler echo.HandlerFunc
func (e echoHandler) Invoke(ctx SpringWeb.WebContext) {
if err := e(EchoContext(ctx)); err != nil {
panic(err)
}
}
func (e echoHandler) FileLine() (file string, line int, fnName string) {
return SpringUtils.FileLine(e)
}
// Echo Web Echo 适配函数
func Echo(fn echo.HandlerFunc) SpringWeb.Handler {
return echoHandler(fn)
}
/////////////////// filter //////////////////////
// echoFilter 封装 Echo 中间件
type echoFilter echo.MiddlewareFunc
func (filter echoFilter) Invoke(ctx SpringWeb.WebContext, chain SpringWeb.FilterChain) {
h := filter(func(echoCtx echo.Context) error {
chain.Next(ctx)
return nil
})
if err := h(EchoContext(ctx)); err != nil {
panic(err)
}
}
// Filter Web Echo 中间件适配器
func Filter(fn echo.MiddlewareFunc) SpringWeb.Filter {
return echoFilter(fn)
}