diff --git a/summercms/resources/application.conf b/summercms/resources/application.conf index 6627315..f68409a 100644 --- a/summercms/resources/application.conf +++ b/summercms/resources/application.conf @@ -5,7 +5,7 @@ server { port = ${?SERVER_PORT} } -# Database configuration for ZIO config (used by AppConfig) +# Database configuration for ZIO config (used by AppConfig.DatabaseConfig) database { host = "localhost" host = ${?DB_HOST} @@ -20,8 +20,8 @@ database { } # HikariCP configuration for Quill DataSource -# Reads from "database" prefix via Quill.DataSource.fromPrefix -database { +# Uses separate prefix to avoid conflict with ZIO config "database" property +hikari { dataSourceClassName = "org.postgresql.ds.PGSimpleDataSource" dataSource { serverName = "localhost" diff --git a/summercms/src/Main.scala b/summercms/src/Main.scala index 5006997..9b7afa1 100644 --- a/summercms/src/Main.scala +++ b/summercms/src/Main.scala @@ -4,6 +4,7 @@ import zio.config.typesafe.TypesafeConfigProvider import api.Routes import _root_.config.{AppConfig as SummerConfig} +import db.QuillContext object Main extends ZIOAppDefault { @@ -29,7 +30,11 @@ object Main extends ZIOAppDefault { _ <- Console.printLine(banner) _ <- Console.printLine(s" Starting on port ${cfg.server.port}...") _ <- Console.printLine("") - _ <- Server.serve(Routes.routes).provide(Server.defaultWithPort(cfg.server.port)) + // Note: Migrations are NOT auto-run. Use CLI to run migrations (Phase 5). + _ <- Server.serve(Routes.routes).provide( + Server.defaultWithPort(cfg.server.port), + QuillContext.dataSourceLayer + ) } yield () } diff --git a/summercms/src/api/HealthRoutes.scala b/summercms/src/api/HealthRoutes.scala index 85f8beb..d812a3e 100644 --- a/summercms/src/api/HealthRoutes.scala +++ b/summercms/src/api/HealthRoutes.scala @@ -1,20 +1,33 @@ package api +import zio.* import zio.http.* +import javax.sql.DataSource /** Health check endpoints * - * Provides basic health check endpoint for load balancers and monitoring systems. The /health - * endpoint returns 200 OK when the server is running. + * Provides health check endpoints for load balancers and monitoring systems. * - * Future endpoints: - * - /ready will verify database connectivity + * Endpoints: + * - GET /health - Returns 200 OK when server is running (liveness probe) + * - GET /ready - Returns 200 when database is connected, 503 when not (readiness probe) */ object HealthRoutes { - val routes: Routes[Any, Response] = + val routes: Routes[DataSource, Response] = Routes( - Method.GET / "health" -> Handler.text("ok") + // Liveness probe - always 200 if server is running + Method.GET / "health" -> Handler.text("ok"), + // Readiness probe - checks database connectivity + Method.GET / "ready" -> handler { + ZIO.serviceWithZIO[DataSource] { ds => + ZIO.attempt { + val conn = ds.getConnection + conn.close() + }.as(Response.text("ready")) + .catchAll(_ => ZIO.succeed(Response.status(Status.ServiceUnavailable))) + } + } ) } diff --git a/summercms/src/api/Routes.scala b/summercms/src/api/Routes.scala index 4c232ae..bb15855 100644 --- a/summercms/src/api/Routes.scala +++ b/summercms/src/api/Routes.scala @@ -1,6 +1,7 @@ package api import zio.http.* +import javax.sql.DataSource /** Route composition point * @@ -8,7 +9,7 @@ import zio.http.* */ object Routes { - val routes: Routes[Any, Response] = + val routes: Routes[DataSource, Response] = HealthRoutes.routes } diff --git a/summercms/src/db/QuillContext.scala b/summercms/src/db/QuillContext.scala index d7a224b..e60d85b 100644 --- a/summercms/src/db/QuillContext.scala +++ b/summercms/src/db/QuillContext.scala @@ -27,11 +27,12 @@ object QuillContext { /** DataSource layer from HikariCP configuration * - * Reads configuration from "database" prefix in application.conf. + * Reads configuration from "hikari" prefix in application.conf. + * Uses separate prefix from "database" to avoid conflict with ZIO config. * HikariCP manages the connection pool. */ val dataSourceLayer: ZLayer[Any, Throwable, DataSource] = - Quill.DataSource.fromPrefix("database") + Quill.DataSource.fromPrefix("hikari") /** Quill PostgreSQL context with snake_case naming *