-
Notifications
You must be signed in to change notification settings - Fork 5
/
caddyfile.go
199 lines (158 loc) · 4.08 KB
/
caddyfile.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
package gbox
import (
"errors"
"fmt"
"net/url"
"strconv"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
)
func init() { // nolint:gochecknoinits
httpcaddyfile.RegisterHandlerDirective("gbox", parseCaddyfile)
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { // nolint:ireturn
m := new(Handler).CaddyModule().New().(*Handler)
if err := m.UnmarshalCaddyfile(h.Dispenser); err != nil {
return nil, err
}
if m.Upstream == "" {
return nil, errors.New("upstream url must be set")
}
return m, nil
}
// nolint:funlen,gocyclo
func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) (err error) {
for d.Next() {
for d.NextBlock(0) {
switch d.Val() {
case "upstream":
if h.Upstream != "" {
return d.Err("upstream already specified")
}
if !d.NextArg() {
return d.ArgErr()
}
val := d.Val()
var u *url.URL
var tokens []caddyfile.Token
u, err = url.Parse(val)
if err != nil {
return err
}
r := &rewrite.Rewrite{URI: u.RequestURI()}
rp := &reverseproxy.Handler{}
rpPattern := `
reverse_proxy {
to %s://%s
header_up Host {upstream_hostport}
}
`
rpConfig := fmt.Sprintf(rpPattern, u.Scheme, u.Host)
tokens, err = caddyfile.Tokenize([]byte(rpConfig), "")
if err != nil {
return err
}
err = rp.UnmarshalCaddyfile(caddyfile.NewDispenser(tokens))
if err != nil {
return err
}
// unmarshal again to add extra reverse proxy config
err = rp.UnmarshalCaddyfile(d.NewFromNextSegment())
if err != nil {
return err
}
h.Upstream = val
h.RewriteRaw = caddyconfig.JSONModuleObject(r, "rewrite", "rewrite", nil)
h.ReverseProxyRaw = caddyconfig.JSONModuleObject(rp, "reverse_proxy", "reverse_proxy", nil)
case "disabled_introspection":
if !d.NextArg() {
return d.ArgErr()
}
var disabled bool
disabled, err = strconv.ParseBool(d.Val())
if err != nil {
return err
}
h.DisabledIntrospection = disabled
case "fetch_schema_interval":
if !d.NextArg() {
return d.ArgErr()
}
var dt time.Duration
dt, err = caddy.ParseDuration(d.Val())
if err != nil {
return err
}
h.FetchSchemaInterval = caddy.Duration(dt)
case "fetch_schema_timeout":
if !d.NextArg() {
return d.ArgErr()
}
var dt time.Duration
dt, err = caddy.ParseDuration(d.Val())
if err != nil {
return err
}
h.FetchSchemaTimeout = caddy.Duration(dt)
case "fetch_schema_header":
if !d.NextArg() {
return d.ArgErr()
}
name := d.Val()
if !d.NextArg() {
return d.ArgErr()
}
h.FetchSchemaHeader.Add(name, d.Val())
case "complexity":
if h.Complexity != nil {
return d.Err("complexity already specified")
}
if err = h.unmarshalCaddyfileComplexity(d.NewFromNextSegment()); err != nil {
return err
}
case "caching":
if h.Caching != nil {
return d.Err("caching already specified")
}
if err = h.unmarshalCaddyfileCaching(d.NewFromNextSegment()); err != nil {
return err
}
case "disabled_playgrounds":
if !d.NextArg() {
return d.ArgErr()
}
var disabled bool
disabled, err = strconv.ParseBool(d.Val())
if err != nil {
return err
}
h.DisabledPlaygrounds = disabled
case "cors_origins":
origins := d.RemainingArgs()
if len(origins) == 0 {
return d.ArgErr()
}
h.CORSOrigins = origins
case "cors_allowed_headers":
headers := d.RemainingArgs()
if len(headers) == 0 {
return d.ArgErr()
}
h.CORSAllowedHeaders = headers
default:
return d.Errf("unrecognized subdirective %s", d.Val())
}
}
}
return err
}
// Interface guards.
var (
_ caddyfile.Unmarshaler = (*Handler)(nil)
)