-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
executable file
·94 lines (86 loc) · 2.15 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
package server
import (
"strings"
"github.com/zc310/fs/config"
"github.com/zc310/fs/middleware"
"github.com/dustin/go-humanize"
"github.com/valyala/fasthttp"
"github.com/zc310/alice"
"github.com/zc310/fasthttprouter"
"github.com/zc310/fs/template"
"github.com/zc310/log"
"github.com/zc310/utils/fasthttputil"
)
// Start 初始化
func Start(c *config.Config) error {
cfg := new(middleware.Config)
cfg.Router = fasthttprouter.New()
if c.Log.Path != "" {
t, err := template.NewTemplate(c.Log.Path)
if err != nil {
return err
}
log.SetPath(string(t))
}
cfg.Logger = log.NewWithPrefix("fs")
cfg.Path = "/"
cfg.Logger.Print(c)
var err error
var mw []alice.Constructor
var mp middleware.Plugin
var h alice.Chain
if len(c.Middleware) > 0 {
mw, err = c.Middleware.Load(cfg)
if err != nil {
return err
}
h = alice.New(mw...)
}
hw := make(fasthttputil.HostSwitch)
for _, h := range c.Handler {
cfg.Router = fasthttprouter.New()
mw, err = h.Middleware.Load(cfg)
if err != nil {
return err
}
for _, name := range h.Host {
hw.Add(name, alice.New(mw...).Then(cfg.Router.Handler))
}
for _, router := range h.Router {
mw, err = router.Middleware.Load(cfg)
if err != nil {
return err
}
mp, err = router.Handler.Load(cfg)
if err != nil {
return err
}
for _, path := range router.Paths {
cfg.Path = strings.TrimSuffix(path, "*filepath")
AddRouter(cfg.Router, path, alice.New(mw...).Then(mp.Handler()))
}
}
}
m, err := humanize.ParseBytes(c.MaxBodySize)
if err != nil {
cfg.Logger.Print(err)
m = fasthttp.DefaultMaxRequestBodySize
}
cfg.Logger.Print("DefaultMaxRequestBodySize", c.MaxBodySize, m)
server := &fasthttp.Server{
Name: c.Name,
Handler: h.Then(hw.Handler),
Logger: cfg.Logger,
MaxRequestBodySize: int(m),
}
return server.ListenAndServe(c.Listen)
}
func AddRouter(router *fasthttprouter.Router, path string, handle fasthttp.RequestHandler) {
router.GET(path, handle)
router.POST(path, handle)
router.HEAD(path, handle)
router.OPTIONS(path, handle)
router.DELETE(path, handle)
router.PUT(path, handle)
router.PATCH(path, handle)
}