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.

50 lines
1.5 KiB

3 years ago
  1. package wow.doge.http4sdemo
  2. import cats.implicits._
  3. import fs2.Stream
  4. import monix.bio.Task
  5. import monix.execution.Scheduler
  6. import org.http4s.client.blaze.BlazeClientBuilder
  7. import org.http4s.implicits._
  8. import org.http4s.server.blaze.BlazeServerBuilder
  9. import org.http4s.server.middleware.Logger
  10. import slick.jdbc.JdbcBackend.DatabaseDef
  11. import slick.jdbc.JdbcProfile
  12. import wow.doge.http4sdemo.services.LibraryDbio
  13. import wow.doge.http4sdemo.services.LibraryService
  14. object Http4sdemoServer {
  15. def stream(
  16. db: DatabaseDef,
  17. p: JdbcProfile
  18. )(implicit s: Scheduler): Stream[Task, Nothing] = {
  19. for {
  20. client <- BlazeClientBuilder[Task](s).stream
  21. helloWorldAlg = HelloWorld.impl
  22. jokeAlg = Jokes.impl(client)
  23. ss = new UserService(p, db)
  24. // Combine Service Routes into an HttpApp.
  25. // Can also be done via a Router if you
  26. // want to extract a segments not checked
  27. // in the underlying routes.
  28. libraryDbio = new LibraryDbio(p)
  29. libraryService = new LibraryService(p, libraryDbio, db)
  30. httpApp = (
  31. Http4sdemoRoutes.helloWorldRoutes[Task](helloWorldAlg) <+>
  32. Http4sdemoRoutes.jokeRoutes[Task](jokeAlg) <+>
  33. Http4sdemoRoutes.userRoutes(ss) <+>
  34. Http4sdemoRoutes.libraryRoutes(libraryService)
  35. ).orNotFound
  36. // With Middlewares in place
  37. finalHttpApp = Logger.httpApp(true, true)(httpApp)
  38. exitCode <- BlazeServerBuilder[Task](s)
  39. .bindHttp(8081, "0.0.0.0")
  40. .withHttpApp(finalHttpApp)
  41. .serve
  42. } yield exitCode
  43. }.drain
  44. }