Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Created September 4, 2018 00:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j5ik2o/a473030d0ca01f6434daeed2d3ed4dd5 to your computer and use it in GitHub Desktop.
Save j5ik2o/a473030d0ca01f6434daeed2d3ed4dd5 to your computer and use it in GitHub Desktop.
class ApiServer extends HttpApp {
protected val design: Design = bind[Design] // ApiServerの外部で定義されているDesginを参照したい
.bind[AtomicReference[ActorSystem]]
.toInstance(systemReference)
.bind[ActorSystem]
.toInstanceProvider[AtomicReference[ActorSystem]](_.get)
// snip
override protected def routes: Route = bind[Routes].route
}
@xerial
Copy link

xerial commented Sep 4, 2018

Airframe doesn't support binding inside classes, so you need to use constructor injection:

class ApiServer(design:Design)

or trait injection:

trait ApiServer {
   protected val design: Design = bind[Design]
      .bind[X].toXXX  
}

@j5ik2o
Copy link
Author

j5ik2o commented Sep 4, 2018

結局、HttpAppは抽象クラスで使いにくかったので利用しないで、traitで書きました。コメントありがとうございました。

trait ApiServer {

  implicit val system           = bind[ActorSystem]
  implicit val materializer     = ActorMaterializer()
  implicit val executionContext = system.dispatcher

  private val routes = bind[Routes].routes

  def start(host: String, port: Int): Future[ServerBinding] = {
    val bindingFuture = Http().bindAndHandle(handler = routes, interface = host, port = port)
    sys.addShutdownHook {
      bindingFuture
        .flatMap(_.unbind())
        .onComplete { _ =>
          materializer.shutdown()
          system.terminate()
        }
    }
    bindingFuture
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment