/shipping/read-front/src/main/scala/ecommerce/shipping/HttpService.scala

https://github.com/pawelkaczor/ddd-leaven-akka-v2 · Scala · 40 lines · 29 code · 11 blank · 0 comment · 0 complexity · 2b3e2feb661f37a7e081113af5e41d00 MD5 · raw file

  1. package ecommerce.shipping
  2. import akka.actor.{Actor, ActorLogging, Props}
  3. import akka.http.scaladsl.Http
  4. import akka.http.scaladsl.server.Directives
  5. import pl.newicom.dddd.streams.ImplicitMaterializer
  6. import akka.util.Timeout
  7. import com.typesafe.config.Config
  8. import ecommerce.shipping.app.ShipmentViewEndpoint
  9. import org.json4s.Formats
  10. import pl.newicom.dddd.serialization.JsonSerHints._
  11. import pl.newicom.dddd.view.sql.SqlViewStore
  12. import scala.concurrent.duration.FiniteDuration
  13. import slick.jdbc.PostgresProfile
  14. object HttpService {
  15. def props(interface: String, port: Int, askTimeout: FiniteDuration): Props =
  16. Props(new HttpService(interface, port)(askTimeout))
  17. }
  18. class HttpService(interface: String, port: Int)(implicit askTimeout: Timeout) extends Actor with ActorLogging
  19. with ShippingReadFrontConfiguration with ImplicitMaterializer with Directives {
  20. import context.dispatcher
  21. implicit val formats: Formats = fromConfig(config)
  22. implicit val profile = PostgresProfile
  23. Http(context.system).bindAndHandle(route, interface, port)
  24. log.info(s"Listening on $interface:$port")
  25. override def receive: Receive = Actor.emptyBehavior
  26. override def config: Config = context.system.settings.config
  27. lazy val endpoints: ShipmentViewEndpoint = new ShipmentViewEndpoint
  28. private def route = (provide(new SqlViewStore(config)) & pathPrefix("ecommerce" / "shipping"))(endpoints)
  29. }