You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package wow.doge.http4sdemo
  2. import cats.implicits._
  3. import fs2.Stream
  4. import io.odin
  5. import monix.bio.Task
  6. import monix.execution.Scheduler
  7. import org.http4s.client.blaze.BlazeClientBuilder
  8. import org.http4s.implicits._
  9. import org.http4s.server.blaze.BlazeServerBuilder
  10. import org.http4s.server.middleware.Logger
  11. import slick.jdbc.JdbcBackend.DatabaseDef
  12. import slick.jdbc.JdbcProfile
  13. import wow.doge.http4sdemo.routes.LibraryRoutes
  14. import wow.doge.http4sdemo.services.LibraryDbio
  15. import wow.doge.http4sdemo.services.LibraryServiceImpl
  16. final class Server(db: DatabaseDef, p: JdbcProfile, logger: odin.Logger[Task]) {
  17. def stream(implicit s: Scheduler): Stream[Task, Nothing] = {
  18. val logger = io.odin.consoleLogger[Task](formatter =
  19. io.odin.formatter.Formatter.colorful
  20. )
  21. val log: String => Task[Unit] = str => logger.debug(str)
  22. for {
  23. client <- BlazeClientBuilder[Task](s).stream
  24. libraryDbio = new LibraryDbio(p)
  25. libraryService = new LibraryServiceImpl(p, libraryDbio, db)
  26. httpApp = (
  27. new LibraryRoutes(libraryService, logger).routes
  28. ).orNotFound
  29. finalHttpApp = Logger.httpApp(
  30. true,
  31. true,
  32. logAction = log.pure[Option]
  33. )(httpApp)
  34. exitCode <- BlazeServerBuilder[Task](s)
  35. .bindHttp(8081, "0.0.0.0")
  36. .withHttpApp(finalHttpApp)
  37. .serve
  38. } yield exitCode
  39. }.drain
  40. }