-
Notifications
You must be signed in to change notification settings - Fork 3
/
caddyfile.go
62 lines (46 loc) · 1.11 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
package naddy
import (
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/tj/go-redirects"
)
const ModuleName = "netlify_redirects"
func init() {
httpcaddyfile.RegisterHandlerDirective(ModuleName, parseCaddyfile)
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m Middleware
d := h.Dispenser
for d.Next() {
for nesting := d.Nesting(); d.NextBlock(nesting); {
allRedirects := ""
for {
val := d.Val()
args := d.RemainingArgs()
if val == "}" || val == "" {
break
}
allArgs := append([]string{val}, args...)
for _, arg := range allArgs {
allRedirects = fmt.Sprintf("%s %s", allRedirects, arg)
}
allRedirects = fmt.Sprintf("%s\n", allRedirects)
if d.Next() == false {
break
}
}
redir, err := redirects.ParseString(allRedirects)
if err != nil {
m.Logger.Error(err.Error())
} else {
m.Redirects = redir
}
}
}
return m, nil
}
var (
_ caddy.Provisioner = (*Middleware)(nil)
)