-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement app skeleton
app/run
works now
- Loading branch information
1 parent
0f1783a
commit 8065238
Showing
10 changed files
with
128 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package lila.search | ||
package app | ||
|
||
import cats.effect.* | ||
import org.typelevel.log4cats.Logger | ||
import org.typelevel.log4cats.slf4j.Slf4jLogger | ||
|
||
object App extends IOApp.Simple: | ||
|
||
given Logger[IO] = Slf4jLogger.getLogger[IO] | ||
|
||
override def run: IO[Unit] = app.useForever | ||
|
||
def app: Resource[IO, Unit] = | ||
for | ||
config <- AppConfig.load.toResource | ||
_ <- Logger[IO].info(s"Starting fide-api with config: $config").toResource | ||
_ <- SearchApp(config).run() | ||
yield () | ||
|
||
class SearchApp(config: AppConfig)(using Logger[IO]): | ||
def run(): Resource[IO, Unit] = | ||
for | ||
httpApp <- Routes | ||
server <- MkHttpServer.apply.newEmber(config.server, httpApp) | ||
_ <- Logger[IO].info(s"Starting server on ${config.server.host}:${config.server.port}").toResource | ||
yield () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package lila.search | ||
package app | ||
|
||
import cats.effect.IO | ||
import org.http4s.* | ||
import org.http4s.implicits.* | ||
import org.http4s.server.middleware.* | ||
|
||
import scala.concurrent.duration.* | ||
|
||
type Middleware = HttpRoutes[IO] => HttpRoutes[IO] | ||
def ApplyMiddleware(routes: HttpRoutes[IO]): HttpApp[IO] = | ||
|
||
val autoSlash: Middleware = AutoSlash(_) | ||
val timeout: Middleware = Timeout(60.seconds) | ||
|
||
val middleware = autoSlash.andThen(timeout) | ||
|
||
middleware(routes).orNotFound |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package lila.search | ||
package app | ||
|
||
import cats.data.NonEmptyList | ||
import cats.effect.{ IO, Resource } | ||
import cats.syntax.all.* | ||
import lila.search.spec.* | ||
import org.http4s.{ HttpApp, HttpRoutes } | ||
import org.typelevel.log4cats.Logger | ||
import smithy4s.http4s.SimpleRestJsonBuilder | ||
|
||
def Routes(using Logger[IO]): Resource[IO, HttpApp[IO]] = | ||
|
||
val healthServiceImpl: HealthService[IO] = new HealthService.Default[IO](IO.stub) | ||
|
||
val searchServiceImpl: SearchService[IO] = new SearchService.Default[IO](IO.stub) | ||
|
||
val search: Resource[IO, HttpRoutes[IO]] = | ||
SimpleRestJsonBuilder.routes(searchServiceImpl).resource | ||
|
||
val health: Resource[IO, HttpRoutes[IO]] = | ||
SimpleRestJsonBuilder.routes(healthServiceImpl).resource | ||
|
||
val docs = smithy4s.http4s.swagger.docs[IO](SearchService, HealthService) | ||
|
||
NonEmptyList | ||
.of(search, health) | ||
.sequence | ||
.map(_.reduceK) | ||
.map(_ <+> docs) | ||
.map(ApplyMiddleware) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package lila.search | ||
package app | ||
|
||
import cats.effect.{ IO, Resource } | ||
import fs2.io.net.Network | ||
import org.http4s.* | ||
import org.http4s.ember.server.EmberServerBuilder | ||
import org.http4s.server.Server | ||
import org.typelevel.log4cats.Logger | ||
|
||
import scala.concurrent.duration.* | ||
|
||
trait MkHttpServer: | ||
def newEmber(cfg: HttpServerConfig, httpApp: HttpApp[IO]): Resource[IO, Server] | ||
|
||
object MkHttpServer: | ||
|
||
def apply(using server: MkHttpServer): MkHttpServer = server | ||
|
||
given forAsyncLogger(using Logger[IO]): MkHttpServer = new: | ||
|
||
def newEmber(cfg: HttpServerConfig, httpApp: HttpApp[IO]): Resource[IO, Server] = EmberServerBuilder | ||
.default[IO] | ||
.withHost(cfg.host) | ||
.withPort(cfg.port) | ||
.withHttpApp(httpApp) | ||
.withShutdownTimeout(cfg.shutdownTimeout.seconds) | ||
.build | ||
.evalTap(showBanner) | ||
|
||
private def showBanner(s: Server): IO[Unit] = | ||
Logger[IO].info(s"lila-search started at ${s.address}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters