-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.go
More file actions
100 lines (80 loc) · 2.64 KB
/
extension.go
File metadata and controls
100 lines (80 loc) · 2.64 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
package zeaburcaddyextension
import (
"fmt"
"io/fs"
"log/slog"
"net/http"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
func init() {
caddy.RegisterModule(ZeaburExtension{})
httpcaddyfile.RegisterHandlerDirective("zeaburextension", func(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
return &ZeaburExtension{}, nil
})
httpcaddyfile.RegisterDirectiveOrder("zeaburextension", httpcaddyfile.After, "header")
}
type ZeaburExtension struct {
headerConfig map[string]HeaderConfig
redirectRules map[string]RedirectRule
}
// Provision implements caddy.Provisioner.
func (z *ZeaburExtension) Provision(ctx caddy.Context) error {
fsys := ctx.FileSystems().Default()
if content, err := fs.ReadFile(fsys, "_headers"); err == nil {
slog.Info("found _headers file")
hc, err := ParseHeaderConfig(string(content))
if err != nil {
return fmt.Errorf("parse headers: %w", err)
}
hcm := make(map[string]HeaderConfig, len(hc))
for _, h := range hc {
hcm[strings.TrimRight(h.Path, "/")+"/"] = h
}
z.headerConfig = hcm
}
if content, err := fs.ReadFile(fsys, "_redirects"); err == nil {
slog.Info("found _redirects file")
rr, err := ParseRedirects(string(content))
if err != nil {
return fmt.Errorf("parse redirects: %w", err)
}
rrm := make(map[string]RedirectRule, len(rr))
for _, r := range rr {
rrm[strings.TrimRight(r.SourcePath, "/")+"/"] = r
}
z.redirectRules = rrm
}
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (z *ZeaburExtension) ServeHTTP(w http.ResponseWriter, r *http.Request, h caddyhttp.Handler) error {
path := strings.TrimRight(r.URL.Path, "/") + "/"
// if the path matches the exact redirect rules, redirect
if rule, ok := z.redirectRules[path]; ok {
slog.Info("redirecting", "source", r.URL.Path, "target", rule.TargetPath, "status", rule.StatusCode)
http.Redirect(w, r, rule.TargetPath, rule.StatusCode)
return nil
}
// if the path matches the prefix redirect rules, redirect
if rule, ok := z.headerConfig[path]; ok {
slog.Info("applying headers", "path", r.URL.Path, "headers", rule.Headers)
for k, v := range rule.Headers {
w.Header().Set(k, v)
}
}
return h.ServeHTTP(w, r)
}
// CaddyModule returns the Caddy module information.
func (ZeaburExtension) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.zeaburextension",
New: func() caddy.Module { return new(ZeaburExtension) },
}
}
var (
_ caddy.Provisioner = (*ZeaburExtension)(nil)
_ caddyhttp.MiddlewareHandler = (*ZeaburExtension)(nil)
)