Created
September 4, 2018 00:26
-
-
Save j5ik2o/a473030d0ca01f6434daeed2d3ed4dd5 to your computer and use it in GitHub Desktop.
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
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 | |
} |
結局、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
Airframe doesn't support binding inside classes, so you need to use constructor injection:
or trait injection: