I'm not talking about how to disable PlayNettyServer playback and enable AkkaHttpServer, which is described in the documentation, using:
lazy val root = (project in file(".")) .enablePlugins(PlayScala, PlayAkkaHttpServer) .disablePlugins(PlayNettyServer)
I mean that using dependency injection in the game and other tools like play-slick, and using akka-http directly in the code, for example:
class AppInitiation @Inject()(implicit val system: ActorSystem, configuration: Configuration) { implicit val materializer = ActorMaterializer() implicit val timeout: Timeout = 5 seconds val logger = Logger("Server") val milkywayPath = path(Segment ~ RestPath) val methods = get | put | post | delete val gatewayExceptionHandler = ExceptionHandler { case e: AskTimeoutException => complete(HttpResponse(InternalServerError, Nil, "Ask timeout!")) case e: Exception => logger.error("unknown error", e) complete(HttpResponse(InternalServerError, Nil, "Unknown error! Please contact administratro!")) } implicit def rejectionHandler = RejectionHandler.newBuilder() .handle { case MissingHeaderRejection("X-Caller-Service") => complete(HttpResponse(BadRequest, Nil, "Missing required header X-Caller-Service!")) } .handle { case MissingQueryParamRejection("v") => complete(HttpResponse(BadRequest, Nil, "Missing required parameter v!")) } .result() val requestHandler = system.actorOf(Props(new RequestHandler)) val routes = handleExceptions(gatewayExceptionHandler) { (milkywayPath & methods & parameter('v.as[String]) & headerValueByName("X-Caller-Service") & extractRequest) { (contextPath: String, resource: Path, v, caller, request) => complete { val startTime = System.currentTimeMillis() val callerInfo = CallerInfo(caller, contextPath, v, resource.toString()) val f = (requestHandler ? RequestHandlerMsg(callerInfo, request)).mapTo[HttpResponse] f onComplete { case r => accessLogger.info(s"method=${request.method.name} " + s"uri=${request.uri} " + s"caller=$caller " + s"totalTime=${System.currentTimeMillis() - startTime}ms " + s"resultStatus=${r.map(_.status.intValue).getOrElse(500)}") } f } } } val host = configuration.getString("gateway.host").getOrElse("localhost") val port = configuration.getInt("gateway.port").getOrElse(9001) Http().bindAndHandle(routes, host, port).onComplete(_ => logger.info(s"Server started listening request on port $port")) }
and in the module I can install it as:
override def configure(): Unit = { bind(classOf[AppInitiation]).asEagerSingleton() }
It works. But I still want to know how I can start it without starting PlayNettyServer. I tried:
lazy val `root` = (project in file(".")) .enablePlugins(PlayScala) .disablePlugins(PlayNettyServer)
However, an exception occurs:
[info] palcActorSystemProvider - Starting application default Akka system: application play.core.server.ServerStartException: No ServerProvider configured with key 'play.server.provider' at play.core.server.ServerProvider$$anonfun$1.apply(ServerProvider.scala:54) at play.core.server.ServerProvider$$anonfun$1.apply(ServerProvider.scala:54)
I am wondering if there is a way to make full use of the Play platform with all its functions, as well as create a server with higher performance with akka http.