From 6ac673056bd2bfbba8971e02a533ae238897c220 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Fri, 9 Feb 2024 08:42:08 +0100 Subject: [PATCH] Server: Add "GET /health" handler so clients can perform health checks Signed-off-by: Michael Mayer --- internal/server/start.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/internal/server/start.go b/internal/server/start.go index f0af51d96..72b7394c9 100644 --- a/internal/server/start.go +++ b/internal/server/start.go @@ -15,6 +15,7 @@ import ( "github.com/gin-gonic/gin" "github.com/photoprism/photoprism/internal/config" + "github.com/photoprism/photoprism/pkg/header" ) // Start the REST API server using the configuration provided @@ -27,14 +28,14 @@ func Start(ctx context.Context, conf *config.Config) { start := time.Now() - // Set HTTP server mode. + // Set web server mode. if conf.HttpMode() != "" { gin.SetMode(conf.HttpMode()) } else if conf.Debug() == false { gin.SetMode(gin.ReleaseMode) } - // Create new HTTP router engine without standard middleware. + // Create new router engine without standard middleware. router := gin.New() // Set proxy addresses from which headers related to the client and protocol can be trusted @@ -56,6 +57,7 @@ func Start(ctx context.Context, conf *config.Config) { ".png", ".gif", ".jpeg", ".jpg", ".webp", ".mp3", ".mp4", ".zip", ".gz", }), gzip.WithExcludedPaths([]string{ + conf.BaseUri("/health"), conf.BaseUri(config.ApiUri + "/t"), conf.BaseUri(config.ApiUri + "/folders/t"), conf.BaseUri(config.ApiUri + "/zip"), @@ -79,14 +81,21 @@ func Start(ctx context.Context, conf *config.Config) { // Find and load templates. router.LoadHTMLFiles(conf.TemplateFiles()...) - // Register HTTP route handlers. + // Register application routes. registerRoutes(router, conf) + // Register "GET /health" route so clients can perform health checks. + router.GET(conf.BaseUri("/health"), func(c *gin.Context) { + c.Header(header.CacheControl, header.CacheControlNoStore) + c.Header(header.AccessControlAllowOrigin, header.Any) + c.String(http.StatusOK, "OK") + }) + + // Start web server. var tlsErr error var tlsManager *autocert.Manager var server *http.Server - // Start HTTP server. if unixSocket := conf.HttpSocket(); unixSocket != "" { var listener net.Listener var unixAddr *net.UnixAddr @@ -144,7 +153,7 @@ func Start(ctx context.Context, conf *config.Config) { } } - // Graceful HTTP server shutdown. + // Graceful web server shutdown. <-ctx.Done() log.Info("server: shutting down") err := server.Close()