30 lines
741 B
Go
30 lines
741 B
Go
package reverseproxy
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// New takes target host and creates a reverse proxy.
|
|
func New(targetHost string) (*httputil.ReverseProxy, error) {
|
|
u, err := url.Parse(targetHost)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p := httputil.NewSingleHostReverseProxy(u)
|
|
p.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) {
|
|
log.Error().Err(err).Msg("admin web proxy error")
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// ProxyRequestHandler handles the http request using proxy
|
|
func ProxyRequestHandler(proxy *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
proxy.ServeHTTP(w, r)
|
|
}
|
|
}
|