From 1b18e699ea5005e62c8df7743e11e07408e63596 Mon Sep 17 00:00:00 2001 From: Chris Lindholm Date: Mon, 30 Sep 2024 15:04:41 -0600 Subject: [PATCH] Allow constructing servers from HttpRoutes Prior to this commit, servers could only be constructed using subclasses of ServiceInterface, which contain HttpRoutes. It would be useful for the auth use case to be able to manipulate these routes, but as written there was no way to do this. This commit gives us a way to manipulate routes from a service interface before using them to build a server. --- .../latis/server/Latis3ServerBuilder.scala | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/server/src/main/scala/latis/server/Latis3ServerBuilder.scala b/server/src/main/scala/latis/server/Latis3ServerBuilder.scala index e8f50108..ac18c933 100644 --- a/server/src/main/scala/latis/server/Latis3ServerBuilder.scala +++ b/server/src/main/scala/latis/server/Latis3ServerBuilder.scala @@ -52,6 +52,7 @@ object Latis3ServerBuilder { def defaultLandingPage: LandingPage = new DefaultLandingPage(makeServiceInfo("latis.util.BuildInfo$")) + @annotation.targetName("mkServerOld") def mkServer( conf: ServerConf, landingPage: LandingPage, @@ -60,15 +61,21 @@ object Latis3ServerBuilder { )( implicit timer: Temporal[IO] ): Resource[IO, Server] = { + val routes = interfaces.map { (name, si) => (name, si.routes) } + mkServer(conf, landingPage, routes, logger) + } + + def mkServer( + conf: ServerConf, + landingPage: LandingPage, + interfaces: List[(String, HttpRoutes[IO])], + logger: StructuredLogger[IO], + )( + implicit timer: Temporal[IO] + ): Resource[IO, Server] = { - def constructRoutes( - interfaces: List[(String, ServiceInterface)] - ): HttpRoutes[IO] = { - val routes = interfaces.map { - case (prefix, service) => (prefix, service.routes) - } :+ ("/", landingPage.routes) - Router(routes *) - } + val routes = interfaces :+ ("/", landingPage.routes) + val router = Router(routes *) EmberServerBuilder.default[IO] .withHost(host"0.0.0.0") @@ -80,7 +87,7 @@ object Latis3ServerBuilder { CORS.policy .withAllowOriginAll .withAllowMethodsIn(Set(Method.GET, Method.HEAD)) - .apply(constructRoutes(interfaces)) + .apply(router) ).orNotFound, logger ),