zrok/util/proxy.go

33 lines
609 B
Go
Raw Normal View History

2022-07-21 22:01:39 +02:00
package util
import (
"net/http"
"net/http/httputil"
2022-08-09 19:57:45 +02:00
"sync/atomic"
2022-07-21 22:01:39 +02:00
)
type proxyHandler struct {
2022-08-09 19:57:45 +02:00
proxy *httputil.ReverseProxy
requests int32
2022-07-21 22:01:39 +02:00
}
func NewProxyHandler(proxy *httputil.ReverseProxy) *proxyHandler {
2022-08-09 19:57:45 +02:00
handler := &proxyHandler{proxy: proxy}
director := proxy.Director
proxy.Director = func(req *http.Request) {
atomic.AddInt32(&handler.requests, 1)
director(req)
}
return handler
}
func (self *proxyHandler) Requests() int32 {
return atomic.LoadInt32(&self.requests)
2022-07-21 22:01:39 +02:00
}
func (self *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
self.proxy.ServeHTTP(w, r)
}