-
Notifications
You must be signed in to change notification settings - Fork 52
/
web.go
268 lines (235 loc) · 5.55 KB
/
web.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
package znet
import (
"context"
"net/http"
"sync"
// "strconv"
// "sync"
"time"
"github.com/sohaha/zlsgo/zlog"
// "github.com/sohaha/zlsgo/zvar"
)
const (
defaultMultipartMemory = 32 << 20 // 32 MB
// DebugMode debug
DebugMode = "debug"
// ReleaseMode release
ReleaseMode = "release"
// TestMode test
TestMode = "test"
)
const (
defaultServerName = "Z"
defaultAddr = ":3788"
defaultLocation = "Local" // "Asia/Shanghai"
releaseCode = iota
debugCode
testCode
)
type (
// Context context
Context struct {
Writer http.ResponseWriter
Request *http.Request
Code int
Engine *Engine
Info *info
Log *zlog.Logger
next HandlerFunc
}
// Engine is a simple HTTP route multiplexer that parses a request path
Engine struct {
// Log Log
Log *zlog.Logger
readTimeout time.Duration
writeTimeout time.Duration
webModeName string
webMode int
timeLocation *time.Location
ShowFavicon bool
MaxMultipartMemory int64
customMethodType string
addr []string
router *router
}
router struct {
prefix string
middleware []HandlerFunc
trees map[string]*Tree
parameters Parameters
notFound HandlerFunc
panic PanicFunc
}
info struct {
StartTime time.Time
StopHandle bool
handlerLen int
middleware []HandlerFunc
Data map[string]interface{}
}
// HandlerFunc HandlerFunc
HandlerFunc func(*Context)
// MiddlewareFunc MiddlewareFunc
MiddlewareFunc func(*Context, HandlerFunc)
// PanicFunc PanicFunc
PanicFunc func(c *Context, err error)
// MiddlewareType is a public type that is used for middleware
MiddlewareType HandlerFunc
// Parameters records some parameters
Parameters struct {
routeName string
}
serverMap struct {
engine *Engine
srv *http.Server
}
)
var (
// Log Log
Log = zlog.New(zlog.ColorTextWrap(zlog.ColorGreen, "[Z] "))
zservers = map[string]*Engine{}
)
func init() {
Log.ResetFlags(zlog.BitTime | zlog.BitLevel)
}
// New returns a newly initialized Engine object that implements the Engine
func New(serverName ...string) *Engine {
name := defaultServerName
if len(serverName) > 0 {
name = serverName[0]
}
log := zlog.New("[" + name + "] ")
log.ResetFlags(zlog.BitTime | zlog.BitLevel)
location, _ := time.LoadLocation(defaultLocation)
route := &router{
trees: make(map[string]*Tree),
}
r := &Engine{
Log: log,
router: route,
readTimeout: 0 * time.Second,
writeTimeout: 0 * time.Second,
webModeName: ReleaseMode,
webMode: releaseCode,
timeLocation: location,
addr: []string{defaultAddr},
MaxMultipartMemory: defaultMultipartMemory,
}
if _, ok := zservers[name]; ok {
Log.Fatalf("serverName: %s is has", name)
}
zservers[name] = r
r.Use(withRequestLog)
return r
}
// Server Server
func Server(serverName ...string) (engine *Engine, ok bool) {
name := defaultServerName
if len(serverName) > 0 {
name = serverName[0]
}
if engine, ok = zservers[name]; !ok {
Log.Warnf("serverName: %s is not", name)
engine = New(name)
}
return
}
// SetAddr SetAddr
func (e *Engine) SetAddr(addr ...string) {
e.addr = addr
}
// SetTimeLocation timezone
func (e *Engine) SetTimeLocation(location string) {
e.timeLocation, _ = time.LoadLocation(location)
}
// SetCustomMethodField SetCustomMethodField
func (e *Engine) SetCustomMethodField(field string) {
e.customMethodType = field
}
// SetMode Setting Server Mode
func (e *Engine) SetMode(value string) {
var level int
switch value {
case ReleaseMode, "":
level = zlog.LogWarn
e.webMode = releaseCode
case DebugMode:
level = zlog.LogDebug
e.webMode = debugCode
case TestMode:
level = zlog.LogSuccess
e.webMode = testCode
default:
e.Log.Panic("web mode unknown: " + value)
}
if value == "" {
value = ReleaseMode
}
e.webModeName = value
e.Log.SetLogLevel(level)
}
// SetTimeout setTimeout
func (e *Engine) SetTimeout(Timeout time.Duration, WriteTimeout ...time.Duration) {
if len(WriteTimeout) > 0 {
e.writeTimeout = WriteTimeout[0]
e.readTimeout = Timeout
} else {
e.writeTimeout = Timeout
e.readTimeout = Timeout
}
}
// Run Run
func Run() {
var (
srvMap sync.Map
m sync.WaitGroup
)
for _, e := range zservers {
for _, addr := range e.addr {
go func(addr string, e *Engine) {
m.Add(1)
srv := &http.Server{
Addr: addr,
Handler: e,
ReadTimeout: e.readTimeout,
WriteTimeout: e.writeTimeout,
// MaxHeaderBytes: 1 << 20,
}
srvMap.Store(addr, &serverMap{e, srv})
e.Log.Success(e.Log.ColorBackgroundWrap(zlog.ColorLightGreen, zlog.ColorDefault, e.Log.OpTextWrap(zlog.OpBold, "Listen "+addr)))
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
e.Log.Fatalf("Listen: %s\n", err)
} else if err != http.ErrServerClosed {
e.Log.Info(err)
}
}(addr, e)
}
}
iskill := isKill()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
srvMap.Range(func(key, value interface{}) bool {
go func(value interface{}) {
if s, ok := value.(*serverMap); ok {
r := s.engine
r.Log.Warn("Shutdown server ...")
if err := s.srv.Shutdown(ctx); err != nil {
r.Log.Error(err)
} else {
r.Log.Success("Shutdown server done")
}
if iskill {
m.Done()
} else {
runNewProcess(s.srv)
m.Done()
}
}
}(value)
return true
})
m.Wait()
}
func runNewProcess(srv *http.Server) {
Log.Warn("In development...")
}