foyer-dsi-assure-2035/api/src/lu/foyer/App.scala

80 lines
2.4 KiB
Scala
Raw Normal View History

2025-02-27 18:45:46 +01:00
package lu.foyer
import zio.*
import zio.http.*
2025-10-13 15:46:22 +02:00
import zio.http.Header.AccessControlAllowOrigin
import zio.http.Middleware.CorsConfig
import zio.http.Middleware.cors
2025-02-27 18:45:46 +01:00
import zio.http.codec.*
2025-02-28 05:40:32 +01:00
import zio.http.codec.PathCodec.path
2025-02-27 18:45:46 +01:00
import zio.http.endpoint.openapi.OpenAPIGen
import zio.http.endpoint.openapi.SwaggerUI
2025-10-22 15:30:36 +02:00
import zio.logging.ConsoleLoggerConfig
import zio.logging.LogColor
import zio.logging.LogFilter
import zio.logging.LogFormat.*
import zio.logging.consoleLogger
import zio.schema.codec.JsonCodec.ExplicitConfig
2025-02-27 18:45:46 +01:00
2025-11-04 13:42:19 +01:00
import lu.foyer.auth.AuthMiddleware.jwtAuthentication
import lu.foyer.auth.JwtScalaTokenService
2025-10-13 15:46:22 +02:00
import lu.foyer.clients.*
import lu.foyer.contracts.*
2025-02-27 18:45:46 +01:00
2025-03-03 00:24:13 +01:00
object HttpServer:
2025-10-06 18:30:22 +02:00
val corsConfig = CorsConfig(_ => Some(AccessControlAllowOrigin.All))
2025-03-03 00:24:13 +01:00
def routes =
for
2025-10-06 18:30:22 +02:00
client <- ZIO.service[ClientController]
contract <- ZIO.service[ContractController]
openAPI = OpenAPIGen.fromEndpoints(client.endpoints ++ contract.endpoints)
yield (client.routes ++ contract.routes)
2025-11-04 13:42:19 +01:00
@@ jwtAuthentication("Insurance")
2025-10-06 18:30:22 +02:00
@@ cors(corsConfig) @@ Middleware.debug
++ SwaggerUI.routes("docs" / "openapi", openAPI)
2025-03-03 00:24:13 +01:00
2025-02-27 18:45:46 +01:00
object App extends ZIOAppDefault:
2025-10-22 15:30:36 +02:00
private val logFormat =
label("timestamp", timestamp.fixed(32)).color(LogColor.BLUE) |-|
label("level", level.fixed(5)).highlight |-|
label("thread", fiberId).color(LogColor.WHITE) |-|
label("message", quoted(line)).highlight |-|
cause
2025-10-13 15:46:22 +02:00
2025-10-22 15:30:36 +02:00
val logFilter = LogFilter.LogLevelByNameConfig(LogLevel.Debug)
override val bootstrap =
Runtime.removeDefaultLoggers >>>
consoleLogger(ConsoleLoggerConfig(logFormat, logFilter)) >>>
CodecConfig.configLayer(
CodecConfig(explicitNulls = ExplicitConfig(encoding = false, decoding = false))
)
2025-10-13 15:46:22 +02:00
2025-03-03 00:24:13 +01:00
val app =
for
routes <- HttpServer.routes
2025-11-04 13:42:19 +01:00
server <- Server.serve(routes)
2025-03-03 00:24:13 +01:00
yield server
2025-02-27 18:45:46 +01:00
2025-03-03 00:24:13 +01:00
override def run = app.provide(
2025-11-04 13:42:19 +01:00
Server.default,
JwtScalaTokenService.live,
2025-11-04 14:37:21 +01:00
CommandEngine.live[ClientCommand, ClientEvent, ClientState],
CommandEngine.live[ContractCommand, ContractEvent, ContractState],
ClientHandlers.live,
ContractHandlers.live,
ClientReducer.live,
ContractReducer.live,
ClientEventRepositoryInMemory.live,
ContractEventRepositoryInMemory.live,
ClientStateRepositoryInMemory.live,
ContractStateRepositoryInMemory.live,
ClientController.live,
ContractController.live,
PremiumServiceImpl.live,
EmployeeServiceImpl.live
2025-03-03 00:24:13 +01:00
)
2025-10-22 15:30:36 +02:00
end App