-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
59 lines (47 loc) · 1.31 KB
/
proxy.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
package middleware
import (
"regexp"
"github.com/go-zoox/proxy"
"github.com/go-zoox/zoox"
)
// ProxyConfig is the config of proxy middlewares
type ProxyConfig struct {
Rewrites map[string]ProxyRewrite
}
// ProxyRewrite ...
type ProxyRewrite struct {
Target string
Rewrites map[string]string
}
// Proxy is a middleware that authenticates via Basic Auth.
func Proxy(cfg *ProxyConfig) zoox.Middleware {
return func(ctx *zoox.Context) {
for key, value := range cfg.Rewrites {
if matched, err := regexp.MatchString(key, ctx.Path); err == nil && matched {
// @BUG: this is not working
p := proxy.NewSingleTarget(value.Target, &proxy.SingleTargetConfig{
Rewrites: value.Rewrites,
})
p.ServeHTTP(ctx.Writer, ctx.Request)
return
// rewriters := rewriter.Rewriters{}
// for k, v := range value.Rewrites {
// rewriters = append(rewriters, &rewriter.Rewriter{
// From: k,
// To: v,
// })
// }
// ctx.Request.URL.Path = rewriters.Rewrite(ctx.Path)
// ctx.Path = ctx.Request.URL.Path
// u, err := url.Parse(value.Target)
// if err != nil {
// panic(fmt.Errorf("invalid proxy target: %s", value.Target))
// }
// p := httputil.NewSingleHostReverseProxy(u)
// p.ServeHTTP(ctx.Writer, ctx.Request)
// return
}
}
ctx.Next()
}
}