Don't log healthcheck and favicon requests

This commit is contained in:
DarthSim
2024-01-19 18:41:37 +03:00
parent 30f4fea1a1
commit 2026de092b
2 changed files with 40 additions and 16 deletions

View File

@@ -8,6 +8,8 @@ import (
nanoid "github.com/matoous/go-nanoid/v2"
log "github.com/sirupsen/logrus"
"github.com/imgproxy/imgproxy/v3/config"
)
const (
@@ -29,7 +31,11 @@ type route struct {
type Router struct {
prefix string
healthRoutes []string
faviconRoute string
Routes []*route
HealthHandler RouteHandler
}
func (r *route) isMatch(req *http.Request) bool {
@@ -45,8 +51,15 @@ 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,
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 {

View File

@@ -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)
}
// 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)
}