import Hummingbird import NIOCore /// Serves the website's health-check route. /// /// The controller exposes its routes as a `RouteCollection` so they can be added to a router /// (or a sub-group) by the application that composes it: /// /// ```swift /// router.addRoutes(HealthController().routes) /// ``` /// /// - Note: `Context` is the request context the routes are resolved against, and must match the /// context of the router the routes are added to. public struct HealthController: Sendable { // MARK: Properties /// The JSON payload returned for every health check. private let payload: String // MARK: Initializers /// Creates a health controller. public init() { self.payload = #"{"status":"ok"}"# } // MARK: Computed /// The routes served by the controller. /// /// Serves a `GET` request for the health path (`/health`) with a static JSON status payload. public var routes: RouteCollection { let routes = RouteCollection(context: Context.self) routes.get( .Health.check, use: check ) return routes } } // MARK: - Helpers private extension HealthController { // MARK: Methods /// Handles a request for the health check. /// /// Returns a constant JSON body built directly per request — the payload is a tiny literal with no /// rendering step, so there is nothing to pre-render or cache. /// - Parameters: /// - request: the incoming request. /// - context: the context the request is resolved against. /// - Returns: a `200 OK` response carrying the static JSON status payload. @Sendable func check( request: Request, context: some RequestContext ) -> Response { Response( status: .ok, headers: [.contentType: "application/json"], body: .init(byteBuffer: .init(string: payload)) ) } } // MARK: - Constants private extension RouterPath { /// A namespace for the ``HealthController`` route paths. enum Health { /// The path of the health-check endpoint. static let check: RouterPath = "/health" } }