-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconfig.go
More file actions
124 lines (107 loc) · 2.36 KB
/
Copy pathconfig.go
File metadata and controls
124 lines (107 loc) · 2.36 KB
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
package txtdirect
import (
"io/ioutil"
"log"
"os"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"gopkg.in/natefinch/lumberjack.v2"
)
var allOptions = []string{"host", "path", "gometa", "www"}
// Config contains the middleware's configuration
type Config struct {
Enable []string `json:"enable"`
Redirect string `json:"redirect,omitempty"`
Resolver string `json:"resolver,omitempty"`
LogOutput string `json:"logfile,omitempty"`
Qr Qr
}
func ParseCaddy(d *caddyfile.Dispenser) (*Config, error) {
var enable []string
var redirect string
var resolver string
var logfile string
for d.Next() {
for nesting := d.Nesting(); d.NextBlock(nesting); {
switch d.Val() {
case "disable":
if enable != nil {
return nil, d.ArgErr()
}
toDisable := d.RemainingArgs()
if len(toDisable) == 0 {
return nil, d.ArgErr()
}
enable = removeArrayFromArray(allOptions, toDisable)
case "enable":
if enable != nil {
return nil, d.ArgErr()
}
enable = d.RemainingArgs()
if len(enable) == 0 {
return nil, d.ArgErr()
}
case "redirect":
toRedirect := d.RemainingArgs()
if len(toRedirect) != 1 {
return nil, d.ArgErr()
}
redirect = toRedirect[0]
case "resolver":
resolverAddr := d.RemainingArgs()
if len(resolverAddr) != 1 {
return nil, d.ArgErr()
}
resolver = resolverAddr[0]
case "logfile":
logfile = "stdout"
// Set stdout as the default value
if d.NextArg() {
logfile = d.Val()
}
}
}
}
// If nothing is specified, enable everything
if enable == nil {
enable = allOptions
}
conf := Config{
Enable: enable,
Redirect: redirect,
Resolver: resolver,
LogOutput: logfile,
}
parseLogfile(logfile)
return &conf, nil
}
func removeArrayFromArray(array, toBeRemoved []string) []string {
t := make([]string, len(array))
copy(t, array)
for _, toRemove := range toBeRemoved {
for i, option := range t {
if option == toRemove {
t[i] = t[len(t)-1]
t = t[:len(t)-1]
break
}
}
}
return t
}
func parseLogfile(logfile string) {
switch logfile {
case "stdout":
log.SetOutput(os.Stdout)
case "stderr":
log.SetOutput(os.Stderr)
case "":
log.SetOutput(ioutil.Discard)
default:
log.SetOutput(&lumberjack.Logger{
Filename: logfile,
MaxSize: 100,
MaxAge: 14,
MaxBackups: 10,
})
}
}