Skip to content

Instantly share code, notes, and snippets.

@krancour
Created March 11, 2019 15:04
Show Gist options
  • Save krancour/851ad4e3dbef0e09ebc1f92cc56e4c9a to your computer and use it in GitHub Desktop.
Save krancour/851ad4e3dbef0e09ebc1f92cc56e4c9a to your computer and use it in GitHub Desktop.
type HTTPReverseProxy interface {
Proxy(w http.ResponseWriter, r *http.Request)
}
type httpReverseProxy struct {
proxyHTTP2RequestFn func(http.ResponseWriter, *http.Request)
proxyHTTP1xRequestFn func(http.ResponseWriter, *http.Request)
}
func NewHTTPReverseProxy() HTTPReverseProxy {
return &httpReverseProxy{
proxyHTTP2RequestFn: defaultProxyHTTP2Request,
proxyHTTP1xRequestFn: defaultProxyHTTP1xRequest,
}
}
func (h *httpReverseProxy) Proxy(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 {
h.proxyHTTP2RequestFn(w, r)
} else {
h.proxyHTTP1xRequestFn(w, r)
}
}
func defaultProxyHTTP2Request(w http.ResponseWriter, r *http.Request) {
// Handle HTTP/2 requests
// ...
}
func defaultProxyHTTP1xRequest(w http.ResponseWriter, r *http.Request) {
// Handle HTTP/1.x requests
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment