-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.go
149 lines (134 loc) · 3.25 KB
/
middleware.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
package redirecter
import (
"fmt"
"net/http"
"strconv"
"sync"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
func init() {
caddy.RegisterModule(Middleware{})
httpcaddyfile.RegisterHandlerDirective("redirecter", parseCaddyfile)
}
var mutex = &sync.Mutex{}
var redirecter *Redirecter
type Middleware struct {
Pgds
logger *zap.Logger
}
func (Middleware) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.redirecter",
New: func() caddy.Module { return new(Middleware) },
}
}
func (h *Middleware) Provision(ctx caddy.Context) error {
h.logger = ctx.Logger(h)
if h.Host == "" || h.Port == 0 || h.User == "" || h.Password == "" || h.DbName == "" {
return fmt.Errorf("Some values are missing")
}
mutex.Lock()
defer mutex.Unlock()
if redirecter == nil {
h.logger.Info("Initializing redirecter singleton")
redirecter = initRedirecter(h.Pgds, h.logger)
err := redirecter.Reload()
if err != nil {
h.logger.Error("Failed to load redirecter", zap.Error(err))
}
return err
}
return nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
handler := &Middleware{}
err := handler.UnmarshalCaddyfile(h.Dispenser)
return handler, err
}
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
//
// redirecter {
// host "127.0.0.1"
// port 5432
// user "patates"
// password "bullides"
// db_name "vinissimus"
// }
//
func (h *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
args := d.RemainingArgs()
if len(args) > 0 {
return d.ArgErr()
}
for d.NextBlock(0) {
switch d.Val() {
case "host":
args := d.RemainingArgs()
if len(args) != 1 {
return d.ArgErr()
}
h.Host = args[0]
case "port":
args := d.RemainingArgs()
if len(args) != 1 {
return d.ArgErr()
}
port, err := strconv.Atoi(args[0])
if err != nil {
return err
}
h.Port = port
case "user":
args := d.RemainingArgs()
if len(args) != 1 {
return d.ArgErr()
}
h.User = args[0]
case "password":
args := d.RemainingArgs()
if len(args) != 1 {
return d.ArgErr()
}
h.Password = args[0]
case "db_name":
args := d.RemainingArgs()
if len(args) != 1 {
return d.ArgErr()
}
h.DbName = args[0]
default:
return d.Errf("unrecognized subdirective %q", d.Val())
}
}
}
return nil
}
func (h *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
newPath, ok := redirecter.FindRedirect(buildUrlWithoutQuery(r))
if ok {
http.Redirect(w, r, newPath, http.StatusMovedPermanently)
return nil
} else {
return next.ServeHTTP(w, r)
}
}
func buildUrlWithoutQuery(r *http.Request) string {
newUrl := *r.URL
newUrl.Host = r.Host
newUrl.Scheme = "http"
if r.TLS != nil {
newUrl.Scheme = "https"
}
newUrl.RawQuery = ""
return newUrl.String()
}
var (
_ caddy.Provisioner = (*Middleware)(nil)
_ caddyfile.Unmarshaler = (*Middleware)(nil)
_ caddyhttp.MiddlewareHandler = (*Middleware)(nil)
)