/
proxy.go
53 lines (46 loc) · 1.13 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
package proxy
import (
"context"
"fmt"
"net/http"
"net/url"
)
// Config the plugin configuration.
type Config struct {
ProxyPath string `json:"proxypath,omitempty"`
UrlParameter string `json:"urlparameter,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// Demo a Demo plugin.
type Proxy struct {
ProxyPath string
UrlParameter string
next http.Handler
name string
}
// New created a new Demo plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.ProxyPath) == 0 {
return nil, fmt.Errorf("ProxyPath cannot be empty")
}
if len(config.UrlParameter) == 0 {
return nil, fmt.Errorf("UrlParameter cannot be empty")
}
return &Proxy{
ProxyPath:config.ProxyPath,
UrlParameter:config.UrlParameter,
next: next,
name: name,
}, nil
}
func (a *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path==a.ProxyPath{
urlStr,_ :=url.QueryUnescape(req.URL.Query().Get(a.UrlParameter))
url,_ := url.Parse(urlStr)
req.URL = url
}
a.next.ServeHTTP(rw, req)
}