-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
38 lines (33 loc) · 917 Bytes
/
utils.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
package broker
import (
"crypto/tls"
"net/http"
"net/http/httputil"
"net/url"
"github.com/spf13/viper"
)
// ParseBroker convert broker to http handler
func ParseBroker(b IBroker) func(http.ResponseWriter, *http.Request) {
wrapProcessRequest := func(req *http.Request) {
upstreamURI := viper.GetString("target")
upstream, _ := url.Parse(upstreamURI)
req.URL.Host = upstream.Host
req.URL.Scheme = upstream.Scheme
req.Host = upstream.Host
req.Header.Set("Accept", "application/json")
b.ProcessRequest(req)
}
wrapProcessResponse := func(res *http.Response) error {
b.LogResponse(res)
err := b.ProcessResponse(res)
b.LogResponse(res)
return err
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
proxy := httputil.ReverseProxy{Transport: tr}
proxy.Director = wrapProcessRequest
proxy.ModifyResponse = wrapProcessResponse
return proxy.ServeHTTP
}