/
setup.go
96 lines (89 loc) · 1.95 KB
/
setup.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
package not_found
import (
super "github.com/caibirdme/durian/server"
"github.com/mholt/caddy"
"github.com/valyala/fasthttp"
"os"
"strconv"
"strings"
)
func init() {
caddy.RegisterPlugin(super.DirectiveNotFound, caddy.Plugin{
ServerType: super.FastHTTPServerType,
Action: setup,
})
}
const (
defaultStatusCode = fasthttp.StatusNotFound
defaultBody = "not found"
defaultContentType = "text/html; charset=utf-8"
)
type NotFoundConfig struct {
StatusCode int
File string
ContentType string
Body string
}
func setup(c *caddy.Controller) error {
cfg, err := parseConfig(c)
if err != nil {
return err
}
if cfg.File != "" {
fileInfo, err := os.Lstat(cfg.File)
if err != nil {
return c.Errf("%s path err: %s", cfg.File, err)
}
if fileInfo.IsDir() {
return c.Errf("%s is a directory", cfg.File)
}
}
srvCfg := super.GetConfig(c)
if srvCfg == nil {
panic("[BUG] server config can't be nil")
}
srvCfg.NotFound = super.NotFoundConfig{
StatusCode: cfg.StatusCode,
File: cfg.File,
ContentType: cfg.ContentType,
Body: cfg.Body,
}
return nil
}
func parseConfig(c *caddy.Controller) (*NotFoundConfig, error) {
c.Next()
cfg := NotFoundConfig{StatusCode: defaultStatusCode, Body: defaultBody, ContentType: defaultContentType}
for c.NextBlock() {
kind := c.Val()
switch strings.ToLower(kind) {
case "code":
if !c.NextArg() {
return nil, c.ArgErr()
}
code, err := strconv.Atoi(c.Val())
if err != nil {
return nil, c.Err(err.Error())
}
cfg.StatusCode = code
case "content_type":
if !c.NextArg() {
return nil, c.ArgErr()
}
cfg.ContentType = c.Val()
case "body":
if !c.NextArg() {
return nil, c.ArgErr()
}
cfg.Body = c.Val()
case "file":
if !c.NextArg() {
return nil, c.ArgErr()
}
cfg.File = c.Val()
}
}
if cfg.File != "" && cfg.Body != "" {
return nil, c.Err("cannot specify file and body at the same time")
}
return &cfg, nil
}