Server: Add "GET /health" handler so clients can perform health checks

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2024-02-09 08:42:08 +01:00
parent 554ba35f2f
commit 6ac673056b

View file

@ -15,6 +15,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/pkg/header"
) )
// Start the REST API server using the configuration provided // Start the REST API server using the configuration provided
@ -27,14 +28,14 @@ func Start(ctx context.Context, conf *config.Config) {
start := time.Now() start := time.Now()
// Set HTTP server mode. // Set web server mode.
if conf.HttpMode() != "" { if conf.HttpMode() != "" {
gin.SetMode(conf.HttpMode()) gin.SetMode(conf.HttpMode())
} else if conf.Debug() == false { } else if conf.Debug() == false {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
} }
// Create new HTTP router engine without standard middleware. // Create new router engine without standard middleware.
router := gin.New() router := gin.New()
// Set proxy addresses from which headers related to the client and protocol can be trusted // 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", ".png", ".gif", ".jpeg", ".jpg", ".webp", ".mp3", ".mp4", ".zip", ".gz",
}), }),
gzip.WithExcludedPaths([]string{ gzip.WithExcludedPaths([]string{
conf.BaseUri("/health"),
conf.BaseUri(config.ApiUri + "/t"), conf.BaseUri(config.ApiUri + "/t"),
conf.BaseUri(config.ApiUri + "/folders/t"), conf.BaseUri(config.ApiUri + "/folders/t"),
conf.BaseUri(config.ApiUri + "/zip"), conf.BaseUri(config.ApiUri + "/zip"),
@ -79,14 +81,21 @@ func Start(ctx context.Context, conf *config.Config) {
// Find and load templates. // Find and load templates.
router.LoadHTMLFiles(conf.TemplateFiles()...) router.LoadHTMLFiles(conf.TemplateFiles()...)
// Register HTTP route handlers. // Register application routes.
registerRoutes(router, conf) 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 tlsErr error
var tlsManager *autocert.Manager var tlsManager *autocert.Manager
var server *http.Server var server *http.Server
// Start HTTP server.
if unixSocket := conf.HttpSocket(); unixSocket != "" { if unixSocket := conf.HttpSocket(); unixSocket != "" {
var listener net.Listener var listener net.Listener
var unixAddr *net.UnixAddr 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() <-ctx.Done()
log.Info("server: shutting down") log.Info("server: shutting down")
err := server.Close() err := server.Close()