forked from caddyserver/caddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directives.go
125 lines (107 loc) · 2.6 KB
/
directives.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
package config
import (
"os"
"os/exec"
"github.com/mholt/caddy/middleware"
)
// dirFunc is a type of parsing function which processes
// a particular directive and populates the config.
type dirFunc func(*parser) error
// validDirectives is a map of valid, built-in directive names
// to their parsing function. Built-in directives cannot be
// ordered, so they should only be used for internal server
// configuration; not directly handling requests.
var validDirectives map[string]dirFunc
func init() {
// This has to be in the init function
// to avoid an initialization loop error because
// the 'import' directive (key) in this map
// invokes a method that uses this map.
validDirectives = map[string]dirFunc{
"root": func(p *parser) error {
if !p.nextArg() {
return p.argErr()
}
p.cfg.Root = p.tkn()
return nil
},
"import": func(p *parser) error {
if !p.nextArg() {
return p.argErr()
}
filename := p.tkn()
file, err := os.Open(filename)
if err != nil {
return p.err("Parse", err.Error())
}
defer file.Close()
p2, err := newParser(file)
if err != nil {
return p.err("Parse", "Could not import "+filename+"; "+err.Error())
}
p2.cfg = p.cfg
err = p2.directives()
if err != nil {
return err
}
p.cfg = p2.cfg
return nil
},
"tls": func(p *parser) error {
tls := TLSConfig{Enabled: true}
if !p.nextArg() {
return p.argErr()
}
tls.Certificate = p.tkn()
if !p.nextArg() {
return p.argErr()
}
tls.Key = p.tkn()
p.cfg.TLS = tls
return nil
},
"startup": func(p *parser) error {
// TODO: This code is duplicated with the shutdown directive below
if !p.nextArg() {
return p.argErr()
}
command, args, err := middleware.SplitCommandAndArgs(p.tkn())
if err != nil {
return p.err("Parse", err.Error())
}
startupfn := func() error {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}
p.cfg.Startup = append(p.cfg.Startup, startupfn)
return nil
},
"shutdown": func(p *parser) error {
if !p.nextArg() {
return p.argErr()
}
command, args, err := middleware.SplitCommandAndArgs(p.tkn())
if err != nil {
return p.err("Parse", err.Error())
}
shutdownfn := func() error {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}
p.cfg.Shutdown = append(p.cfg.Shutdown, shutdownfn)
return nil
},
}
}