Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow constructing servers from HttpRoutes #723

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions server/src/main/scala/latis/server/Latis3ServerBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ object Latis3ServerBuilder {

def defaultLandingPage: LandingPage = new DefaultLandingPage(makeServiceInfo("latis.util.BuildInfo$"))

@annotation.targetName("mkServerOld")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new to me. What is the motivation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two mkServer methods, but the types after erasure are the same (the interfaces argument) so there needs to be another way to disambiguate.

https://docs.scala-lang.org/scala3/reference/other-new-features/targetName.html

def mkServer(
conf: ServerConf,
landingPage: LandingPage,
Expand All @@ -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")
Expand All @@ -80,7 +87,7 @@ object Latis3ServerBuilder {
CORS.policy
.withAllowOriginAll
.withAllowMethodsIn(Set(Method.GET, Method.HEAD))
.apply(constructRoutes(interfaces))
.apply(router)
).orNotFound,
logger
),
Expand Down
Loading