(options *ProxyServerOptions)
| 51 | } |
| 52 | |
| 53 | func NewProxyServer(options *ProxyServerOptions) *ProxyServer { |
| 54 | proxy := httputil.NewSingleHostReverseProxy(options.Target) |
| 55 | |
| 56 | server := &ProxyServer{ |
| 57 | options, |
| 58 | proxy, |
| 59 | } |
| 60 | |
| 61 | originalDirector := proxy.Director |
| 62 | proxy.Director = func(req *http.Request) { |
| 63 | originalDirector(req) |
| 64 | server.modifyRequest(req) |
| 65 | } |
| 66 | |
| 67 | proxy.ModifyResponse = server.modifyResponse |
| 68 | proxy.ErrorHandler = func(rw http.ResponseWriter, r *http.Request, err error) { |
| 69 | if errors.Is(err, context.Canceled) { |
| 70 | return |
| 71 | } |
| 72 | msg := fmt.Sprintf("%+v\n", err) |
| 73 | log.Println(msg) |
| 74 | rw.WriteHeader(http.StatusInternalServerError) |
| 75 | _, _ = rw.Write([]byte(msg)) |
| 76 | } |
| 77 | |
| 78 | return server |
| 79 | } |
| 80 | |
| 81 | func (p *ProxyServer) Handler() func(http.ResponseWriter, *http.Request) { |
| 82 | return func(w http.ResponseWriter, r *http.Request) { |
nothing calls this directly
no test coverage detected