···11+namespace PDSharp.Handlers
22+33+open System.Text.Json
44+open Microsoft.AspNetCore.Http
55+open Giraffe
66+open PDSharp.Core.Health
77+open PDSharp.Core.Config
88+99+module HealthHandler =
1010+ /// PDS version (could be read from assembly info)
1111+ let private version = "0.1.0"
1212+1313+ /// JSON serialization options with camelCase naming
1414+ let private jsonOptions =
1515+ JsonSerializerOptions(PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true)
1616+1717+ /// Health check handler for /xrpc/_health endpoint
1818+ let healthHandler : HttpHandler =
1919+ fun next ctx -> task {
2020+ let config = ctx.GetService<AppConfig>()
2121+ let healthState = ctx.GetService<HealthState>()
2222+ let status = buildHealthStatus version healthState config.SqliteConnectionString "." // Check disk of current working directory
2323+2424+ if status.DatabaseStatus.IsHealthy then
2525+ ctx.SetStatusCode 200
2626+ else
2727+ ctx.SetStatusCode 503
2828+2929+ let json = JsonSerializer.Serialize(status, jsonOptions)
3030+ ctx.SetContentType "application/json"
3131+ return! text json next ctx
3232+ }