瀏覽代碼

Don't log healthcheck and favicon requests

DarthSim 1 年之前
父節點
當前提交
2026de092b
共有 2 個文件被更改,包括 40 次插入16 次删除
  1. 34 4
      router/router.go
  2. 6 12
      server.go

+ 34 - 4
router/router.go

@@ -8,6 +8,8 @@ import (
 
 	nanoid "github.com/matoous/go-nanoid/v2"
 	log "github.com/sirupsen/logrus"
+
+	"github.com/imgproxy/imgproxy/v3/config"
 )
 
 const (
@@ -28,8 +30,12 @@ type route struct {
 }
 
 type Router struct {
-	prefix string
-	Routes []*route
+	prefix       string
+	healthRoutes []string
+	faviconRoute string
+
+	Routes        []*route
+	HealthHandler RouteHandler
 }
 
 func (r *route) isMatch(req *http.Request) bool {
@@ -45,9 +51,16 @@ func (r *route) isMatch(req *http.Request) bool {
 }
 
 func New(prefix string) *Router {
+	healthRoutes := []string{prefix + "/health"}
+	if len(config.HealthCheckPath) > 0 {
+		healthRoutes = append(healthRoutes, prefix+config.HealthCheckPath)
+	}
+
 	return &Router{
-		prefix: prefix,
-		Routes: make([]*route, 0),
+		prefix:       prefix,
+		healthRoutes: healthRoutes,
+		faviconRoute: prefix + "/favicon.ico",
+		Routes:       make([]*route, 0),
 	}
 }
 
@@ -83,6 +96,23 @@ func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
 	rw.Header().Set("Server", "imgproxy")
 	rw.Header().Set(xRequestIDHeader, reqID)
 
+	if req.Method == http.MethodGet {
+		if r.HealthHandler != nil {
+			for _, healthRoute := range r.healthRoutes {
+				if req.URL.Path == healthRoute {
+					r.HealthHandler(reqID, rw, req)
+					return
+				}
+			}
+		}
+
+		if req.URL.Path == r.faviconRoute {
+			// TODO: Add a real favicon maybe?
+			rw.WriteHeader(200)
+			return
+		}
+	}
+
 	if ip := req.Header.Get("CF-Connecting-IP"); len(ip) != 0 {
 		replaceRemoteAddr(req, ip)
 	} else if ip := req.Header.Get("X-Forwarded-For"); len(ip) != 0 {

+ 6 - 12
server.go

@@ -30,15 +30,12 @@ func buildRouter() *router.Router {
 	r := router.New(config.PathPrefix)
 
 	r.GET("/", handleLanding, true)
-	r.GET("/health", handleHealth, true)
-	if len(config.HealthCheckPath) > 0 {
-		r.GET(config.HealthCheckPath, handleHealth, true)
-	}
-	r.GET("/favicon.ico", handleFavicon, true)
 	r.GET("/", withMetrics(withPanicHandler(withCORS(withSecret(handleProcessing)))), false)
 	r.HEAD("/", withCORS(handleHead), false)
 	r.OPTIONS("/", withCORS(handleHead), false)
 
+	r.HealthHandler = handleHealth
+
 	return r
 }
 
@@ -181,7 +178,10 @@ func handleHealth(reqID string, rw http.ResponseWriter, r *http.Request) {
 		ierr = ierrors.Wrap(err, 1)
 	}
 
-	router.LogResponse(reqID, r, status, ierr)
+	// Log response only if something went wrong
+	if ierr != nil {
+		router.LogResponse(reqID, r, status, ierr)
+	}
 
 	rw.Header().Set("Cache-Control", "no-cache")
 	rw.WriteHeader(status)
@@ -192,9 +192,3 @@ func handleHead(reqID string, rw http.ResponseWriter, r *http.Request) {
 	router.LogResponse(reqID, r, 200, nil)
 	rw.WriteHeader(200)
 }
-
-func handleFavicon(reqID string, rw http.ResponseWriter, r *http.Request) {
-	router.LogResponse(reqID, r, 200, nil)
-	// TODO: Add a real favicon maybe?
-	rw.WriteHeader(200)
-}