-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
483 lines (436 loc) · 11.6 KB
/
server.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package goblet
import (
"crypto/sha1"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"time"
"github.com/pkg/errors"
"xorm.io/xorm/caches"
"github.com/sirupsen/logrus"
"github.com/extrame/goblet/config"
ge "github.com/extrame/goblet/error"
"github.com/extrame/goblet/render"
yaml "gopkg.in/yaml.v3"
)
var NotImplemented = fmt.Errorf("this method is not implemented")
type Fn struct {
Name string
Fn interface{}
}
type ControllerNeedInit interface {
Init(*Server)
}
type ControllerNeedInitAndReturnError interface {
Init(*Server) error
}
// Server 服务器类型
type Server struct {
ConfigFile string
Basic config.Basic
Cache config.Cache
Log config.Log
Db config.Db
router router
Renders map[string]render.Render
Name string
plugins map[string]NewPlugin
funcs []Fn
initCtrl []ControllerNeedInit
initCtrlNew []ControllerNeedInitAndReturnError
pres map[string][]reflect.Value
nrPlugin onNewRequestPlugin
saver Saver
filler map[string]FormFillFn
multiFiller map[string]MultiFormFillFn
kv KvDriver
okFunc func(*Context)
errFunc func(*Context, error, ...string)
defaultRender string
cfg *yaml.Node
cfgFileSuffix string
silenceUrls map[string]bool
loginSaver LoginInfoStorer
configer Configer
delims []string
}
var defaultErrFunc = func(c *Context, err error, context ...string) {
c.responseMap = nil
msg := err.Error()
if len(context) > 0 {
msg = "[" + strings.Join(context, "|") + "]" + msg
}
c.RespondWithStatus(msg, http.StatusBadRequest)
}
func (s *Server) SetDefaultOk(fn func(*Context)) {
s.okFunc = fn
}
func (s *Server) SetDefaultError(fn func(*Context, error, ...string)) {
s.errFunc = fn
}
// type Handler interface {
// Path() string
// Dir() string
// }
// type RestNewHander interface {
// New() (int, interface{})
// }
// type PageHandler interface {
// Page() (int, interface{})
// }
// Organize 进行服务器环境的初始化配置,初始化所有plugin,对于plugin的所有操作,在Organize之后都可以视为已经初始化
func (s *Server) Organize(name string, plugins []interface{}) {
var err error
var dbPwdPlugin DbPwdPlugin
var dbUserPlugin dbUserNamePlugin
s.Name = name
s.Renders = make(map[string]render.Render)
for _, plugin := range plugins {
if tp, ok := plugin.(NewPlugin); ok {
typ := reflect.ValueOf(plugin).Type()
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
key := strings.ToLower(typ.Name())
if s.plugins == nil {
s.plugins = make(map[string]NewPlugin)
}
s.plugins[key] = tp
}
//bind the specials plugins
if tp, ok := plugin.(DbPwdPlugin); ok {
dbPwdPlugin = tp
}
if tp, ok := plugin.(ChangeSuffixOfConfig); ok {
s.cfgFileSuffix = tp.GetConfigSuffix()
}
if tp, ok := plugin.(dbUserNamePlugin); ok {
dbUserPlugin = tp
}
if tp, ok := plugin.(onNewRequestPlugin); ok {
s.nrPlugin = tp
}
if tp, ok := plugin.(Saver); ok {
s.saver = tp
}
if kv, ok := plugin.(KvDriver); ok {
s.kv = kv
}
if ov, ok := plugin.(OkFuncSetter); ok {
s.okFunc = ov.RespendOk
}
if ev, ok := plugin.(ErrFuncSetter); ok {
s.errFunc = ev.RespondError
}
if rv, ok := plugin.(DefaultRenderSetter); ok {
s.defaultRender = rv.DefaultRender()
}
if sv, ok := plugin.(SilenceUrlSetter); ok {
s.silenceUrls = sv.SetSilenceUrls()
}
if lv, ok := plugin.(LoginInfoStorer); ok {
s.loginSaver = lv
}
if lv, ok := plugin.(Configer); ok {
s.configer = lv
}
if dv, ok := plugin.(DelimSetter); ok {
var delimis = dv.SetDelim()
s.delims = delimis[:]
}
if rv, ok := plugin.(render.Render); ok {
s.Renders[rv.Type()] = rv
}
}
if s.saver == nil {
s.saver = new(LocalSaver)
}
if s.configer == nil {
s.configer = new(YamlConfiger)
}
if s.loginSaver == nil {
s.loginSaver = new(CookieLoginInfoStorer)
}
s.pres = make(map[string][]reflect.Value)
s.filler = make(map[string]FormFillFn)
s.multiFiller = make(map[string]MultiFormFillFn)
if err = s.parseConfig(); err == nil {
s.router.init()
s.funcs = make([]Fn, 0)
if dbPwdPlugin != nil {
s.Db.Pwd = dbPwdPlugin.SetPwd(s.Db.Pwd)
}
if dbUserPlugin != nil {
s.Db.User = dbUserPlugin.SetName(s.Db.User)
}
err = s.connectDB()
if err == nil {
if s.Basic.Env == config.DevelopEnv {
DB.ShowSQL(true)
}
} else if err != config.NoDbDriver {
log.Fatalln("connect error:", err)
}
} else {
logrus.WithError(err).Fatalln("read config file error")
}
s.enableDbCache()
if s.errFunc == nil {
s.errFunc = defaultErrFunc
}
if s.defaultRender == "" {
s.defaultRender = "html"
}
}
func (s *Server) isSilence(u string) bool {
si, ok := s.silenceUrls[u]
return ok && si
}
func (s *Server) connectDB() (err error) {
DB, err = s.Db.New(s.Basic.DbEngine)
return err
}
// ControlBy
// Use Member of struct of type goblet.Router to redefine the path
func (s *Server) ControlBy(block interface{}) {
cfg := s.prepareOption(block)
if bc, ok := block.(ControllerNeedInit); ok {
s.initCtrl = append(s.initCtrl, bc)
}
if bc, ok := block.(ControllerNeedInitAndReturnError); ok {
s.initCtrlNew = append(s.initCtrlNew, bc)
}
s.router.add(cfg)
}
func (s *Server) caller() (string, string, error) {
pc := make([]uintptr, 2) // at least 1 entry needed
runtime.Callers(2, pc)
f := runtime.FuncForPC(pc[1])
var caller_valid = regexp.MustCompile(`[\w]*\.\(\*([\w]+)\)\.([\w]+)`)
matched := caller_valid.FindStringSubmatch(f.Name())
if len(matched) == 3 {
return matched[1], matched[2], nil
}
return "", "", errors.New("no matched caller")
}
func (s *Server) Pre(fn interface{}, conds ...string) {
if c, _, err := s.caller(); err == nil {
for _, m := range conds {
key := strings.ToLower(c + "-" + m)
if arr, ok := s.pres[key]; ok {
s.pres[key] = append(arr, reflect.ValueOf(fn))
} else {
s.pres[key] = []reflect.Value{reflect.ValueOf(fn)}
}
}
}
}
func (s *Server) AddModel(models interface{}, syncs ...bool) {
var err error
var sync = true
if len(syncs) > 0 {
sync = syncs[0]
}
if sync {
err = DB.Sync2(models)
if err != nil {
logrus.Fatalln("sync error:", err)
}
}
}
func (s *Server) Env() string {
return s.Basic.Env
}
// Debug 当服务器环境为调试环境时,执行相应的匿名函数,用于编写调试环境专用的代码块
func (s *Server) Debug(fn func()) {
if s.Env() == config.DevelopEnv {
fn()
}
}
func (s *Server) WwwRoot() string {
if abs, err := filepath.Abs(s.Basic.WwwRoot); err == nil {
return abs
}
return s.Basic.WwwRoot
}
func (s *Server) GetServerPathByCtrl(ctrl interface{}) []string {
root := s.WwwRoot()
cfg := s.prepareOption(ctrl)
var paths = make([]string, len(cfg.GetRouting()))
for i, r := range cfg.GetRouting() {
paths[i] = filepath.Join(root, r)
}
return paths
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
s.wrapError(w, err, true)
}
}()
err := s.router.route(s, w, r)
err = errors.Cause(err)
if geE, ok := err.(*ge.Error); ok && geE.Code == ge.ERROR_NOSUCHROUTER {
var path string
if geE.Method != "" {
//dynamic return a method which should used as static render
logrus.Debugln("use static file name return by dynamic", geE.Method)
file := filepath.Join(s.Basic.WwwRoot, s.PublicDir(), geE.Method)
if _, err := os.Stat(file); !os.IsNotExist(err) {
s.ServeFile(w, r, filepath.Join(s.Basic.WwwRoot, s.PublicDir(), geE.Method))
return
}
}
if strings.HasSuffix(r.URL.Path, "/") {
path = r.URL.Path + "index.html"
} else {
path = r.URL.Path
}
s.ServeFile(w, r, filepath.Join(s.Basic.WwwRoot, s.PublicDir(), path))
} else if err != nil {
s.wrapError(w, err, false)
}
}
func (s *Server) ServeFile(w http.ResponseWriter, r *http.Request, file string) {
//if not index.html, set cache-control to 1 year
if filepath.Base(file) != "index.html" {
w.Header().Del("Pragma")
w.Header().Set("Cache-Control", "max-age=31536000")
}
http.ServeFile(w, r, file)
}
// GetPlugin 获得对应名称的插件
func (s *Server) GetPlugin(key string) NewPlugin {
return s.plugins[key]
}
func (s *Server) SetConfigSuffix(suffix string) {
s.cfgFileSuffix = suffix
}
func (s *Server) parseConfig() (err error) {
reader, err := s.configer.GetConfigSource(s)
if err == nil {
s.initLog()
s.cfg = new(yaml.Node)
err = yaml.NewDecoder(reader).Decode(s.cfg)
if err == nil {
if err = s.getCfg("basic").Decode(&s.Basic); err == nil {
s.Db.Name = s.Name
if err = s.getCfg(s.Basic.DbEngine).Decode(&s.Db); err == nil {
if s.Db.Host == "" {
s.Db.Host = s.Name
}
if err = s.getCfg("cache").Decode(&s.Cache); err == nil {
s.getCfg("log").Decode(&s.Log)
}
}
}
}
}
if err != nil {
return err
}
if s.Basic.Env == "" {
s.Basic.Env = config.DevelopEnv
logrus.Info("environment not set, default set as development")
}
if s.Basic.DbEngine == "" {
s.Basic.DbEngine = "none"
}
if s.Basic.Port == 0 {
s.Basic.Port = 8080
}
if s.Basic.Env != config.DevelopEnv && s.Basic.Env != config.ProductEnv && s.Basic.Env != config.OldProductEnv {
logrus.Fatalln("environment must be development or production, config env: development or env: production")
} else if s.Basic.Env == config.OldProductEnv {
s.Basic.Env = config.ProductEnv
fmt.Println("[Deprecatd]production environment must be set as 'production' instead of 'product'")
}
if s.Basic.Env == config.DevelopEnv {
logrus.SetLevel(logrus.DebugLevel)
}
for _, plugin := range s.plugins {
if err = plugin.AddCfgAndInit(s); err != nil {
logrus.Fatalf("add plugin config error in (%T) with error (%s)", plugin, err)
}
}
return
}
// Hash 获得一个字符串的加密版本
func (s *Server) Hash(str string) string {
hash := sha1.New()
hash.Write([]byte(str))
hash.Write([]byte(s.Basic.HashSecret))
return fmt.Sprintf("%x", hash.Sum(nil))
}
// PublicDir 获得服务器对应的公共文件夹的地址
func (s *Server) PublicDir() string {
return s.Basic.PublicDir
}
func (s *Server) enableDbCache() {
if s.Cache.Enable {
cacher := caches.NewLRUCacher(caches.NewMemoryStore(), s.Cache.Amount)
DB.SetDefaultCacher(cacher)
}
}
func (s *Server) GetDelims() []string {
return s.delims
}
// Run 运营一个服务器
func (s *Server) Run() error {
if s.Basic.Version == "datetime" {
s.Basic.Version = fmt.Sprintf("%d", time.Now().Unix())
}
// s.Renders = make(map[string]render.Render)
if s.Renders["html"] == nil {
s.Renders["html"] = new(render.HtmlRender)
var tempFuncMap = make(template.FuncMap)
for _, bc := range s.initCtrl {
bc.Init(s)
}
for _, bc := range s.initCtrlNew {
err := bc.Init(s)
if err != nil {
return err
}
}
for _, v := range s.funcs {
tempFunc := func() int {
return 0
}
tempFuncMap[v.Name] = tempFunc
}
s.Renders["html"].Init(s, tempFuncMap)
}
if s.Renders["json"] == nil {
s.Renders["json"] = new(render.JsonRender)
}
if s.Renders["raw"] == nil {
s.Renders["raw"] = new(render.RawRender)
}
if s.Renders["xml"] == nil {
s.Renders["xml"] = new(render.XmlRender)
}
logrus.WithField("port", s.Basic.Port).Infoln("Listening")
srv := &http.Server{
Addr: fmt.Sprintf(":%d", s.Basic.Port),
Handler: s,
WriteTimeout: time.Second * time.Duration(s.Basic.WriteT0),
ReadTimeout: time.Second * time.Duration(s.Basic.ReadT0),
}
srv.SetKeepAlivesEnabled(s.Basic.EnableKeepAlive)
var err error
if s.Basic.HttpsEnable {
err = srv.ListenAndServeTLS(s.Basic.HttpsCertFile, s.Basic.HttpsKey)
} else {
err = srv.ListenAndServe()
}
logrus.Println(err)
return err
}