Spaces:
Configuration error
Configuration error
File size: 861 Bytes
2a72108 74975e2 2a72108 74975e2 2a72108 74975e2 |
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 |
package proxy
import (
"net/http"
"net/http/httputil"
"net/url"
"time"
)
type Proxy struct {
target *url.URL
proxy *httputil.ReverseProxy
timeoutMS int
}
func NewProxy(targetURL string, timeoutMS int) (*Proxy, error) {
target, err := url.Parse(targetURL)
if err != nil {
return nil, err
}
proxy := &Proxy{
target: target,
timeoutMS: timeoutMS,
}
proxy.proxy = &httputil.ReverseProxy{
Director: proxy.director,
Transport: &http.Transport{
Proxy: http.ProxyURL(target),
ResponseHeaderTimeout: time.Duration(timeoutMS) * time.Millisecond,
},
}
return proxy, nil
}
func (p *Proxy) director(req *http.Request) {
req.URL.Scheme = p.target.Scheme
req.URL.Host = p.target.Host
req.Host = p.target.Host
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.proxy.ServeHTTP(w, r)
}
|