mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-21 04:52:35 +02:00

* cleanup code for URL type * fix makefile for trace mode * refactor, merge Entry, RawEntry and Route into one. * Implement fileserver. * refactor: rename HTTPRoute to ReverseProxyRoute to avoid confusion * refactor: move metrics logger to middleware package - fix prometheus metrics for load balanced routes - route will now fail when health monitor fail to start * fix extra output of ls-* commands by defer initializaing stuff, speed up start time * add test for path traversal attack, small fix on FileServer.Start method * rename rule.on.bypass to pass * refactor and fixed map-to-map deserialization * updated route loading logic * schemas: add "add_prefix" option to modify_request middleware * updated route JSONMarshalling --------- Co-authored-by: yusing <yusing@6uo.me>
44 lines
1 KiB
Go
44 lines
1 KiB
Go
package metricslogger
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
|
|
"github.com/yusing/go-proxy/internal/metrics"
|
|
)
|
|
|
|
type MetricsLogger struct {
|
|
ServiceName string `json:"service_name"`
|
|
}
|
|
|
|
func NewMetricsLogger(serviceName string) *MetricsLogger {
|
|
return &MetricsLogger{serviceName}
|
|
}
|
|
|
|
func (m *MetricsLogger) GetHandler(next http.Handler) http.HandlerFunc {
|
|
return func(rw http.ResponseWriter, req *http.Request) {
|
|
m.ServeHTTP(rw, req, next.ServeHTTP)
|
|
}
|
|
}
|
|
|
|
func (m *MetricsLogger) ServeHTTP(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
|
|
visitorIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
|
if err != nil {
|
|
visitorIP = req.RemoteAddr
|
|
}
|
|
|
|
// req.RemoteAddr had been modified by middleware (if any)
|
|
lbls := &metrics.HTTPRouteMetricLabels{
|
|
Service: m.ServiceName,
|
|
Method: req.Method,
|
|
Host: req.Host,
|
|
Visitor: visitorIP,
|
|
Path: req.URL.Path,
|
|
}
|
|
|
|
next.ServeHTTP(newHTTPMetricLogger(rw, lbls), req)
|
|
}
|
|
|
|
func (m *MetricsLogger) ResetMetrics() {
|
|
metrics.GetRouteMetrics().UnregisterService(m.ServiceName)
|
|
}
|