§在您的應用程式中嵌入 Pekko Http 伺服器
儘管 Play 應用程式最常作為其自己的容器使用,您也可以將 Play 伺服器嵌入您現有的應用程式中。這可以與 Twirl 範本編譯器和 Play 路由編譯器一起使用,但當然不是必要的。常見的用例是僅有幾個簡單路由的應用程式。若要使用嵌入式 Pekko HTTP 伺服器,您需要下列依賴項
libraryDependencies ++= Seq(
pekkoHttpServer
)
啟動 Play Pekko HTTP 伺服器的其中一種方式是使用 PekkoHttpServer
工廠方法。如果您只需要提供一些簡單的路由,您可以決定使用 字串內插路由 DSL 搭配 fromRouterWithComponents
方法
import play.api.mvc._
import play.api.routing.sird._
import play.core.server.PekkoHttpServer
val server = PekkoHttpServer.fromRouterWithComponents() { components =>
import Results._
import components.{ defaultActionBuilder => Action }
{
case GET(p"/hello/$to") =>
Action {
Ok(s"Hello $to")
}
}
}
預設情況下,這會在生產模式下於 9000 埠上啟動伺服器。你可以透過傳入 ServerConfig
來設定伺服器
import play.api.mvc._
import play.api.routing.sird._
import play.core.server.PekkoHttpServer
import play.core.server._
val server = PekkoHttpServer.fromRouterWithComponents(
ServerConfig(
port = Some(19000),
address = "127.0.0.1"
)
) { components =>
import Results._
import components.{ defaultActionBuilder => Action }
{
case GET(p"/hello/$to") =>
Action {
Ok(s"Hello $to")
}
}
}
Play 也提供元件特質,讓你可以輕鬆自訂路由器以外的其他元件。 PekkoHttpServerComponents
特質就是為了這個目的而提供的,而且可以方便地與 BuiltInComponents
結合,以建立它所需要的應用程式。在此範例中,我們使用 DefaultPekkoHttpServerComponents
,它等於 PekkoHttpServerComponents with BuiltInComponents with NoHttpFiltersComponents
import play.api.http.DefaultHttpErrorHandler
import play.api.mvc._
import play.api.routing.Router
import play.api.routing.sird._
import play.core.server.DefaultPekkoHttpServerComponents
import scala.concurrent.Future
val components = new DefaultPekkoHttpServerComponents {
override lazy val router: Router = Router.from {
case GET(p"/hello/$to") =>
Action {
Results.Ok(s"Hello $to")
}
}
override lazy val httpErrorHandler: DefaultHttpErrorHandler = new DefaultHttpErrorHandler(
environment,
configuration,
devContext.map(_.sourceMapper),
Some(router)
) {
protected override def onNotFound(request: RequestHeader, message: String): Future[Result] = {
Future.successful(Results.NotFound("Nothing was found!"))
}
}
}
val server = components.server
在此,你只需要實作 router
方法。其他所有內容都有預設實作,你可以透過覆寫方法來自訂,例如上述的 httpErrorHandler
。你可以透過覆寫 serverConfig
屬性來覆寫伺服器設定。
要在你啟動伺服器後停止伺服器,只要呼叫 stop
方法即可
server.stop()
注意:Play 需要設定 應用程式密碼 才能啟動。你可以透過在應用程式中提供
application.conf
檔案,或使用play.http.secret.key
系統屬性來設定。
另一種方式是透過 GuiceApplicationBuilder
結合 fromApplication
方法來建立 Play 應用程式
import play.api.mvc._
import play.api.routing.sird._
import play.core.server.PekkoHttpServer
import play.core.server.ServerConfig
import play.filters.HttpFiltersComponents
import play.api.Environment
import play.api.ApplicationLoader
import play.api.BuiltInComponentsFromContext
val context = ApplicationLoader.Context.create(Environment.simple())
val components = new BuiltInComponentsFromContext(context) with HttpFiltersComponents {
override def router: Router = Router.from {
case GET(p"/hello/$to") =>
Action {
Results.Ok(s"Hello $to")
}
}
}
val server = PekkoHttpServer.fromApplication(
components.application,
ServerConfig(
port = Some(19000),
address = "127.0.0.1"
)
)
§記錄設定
當使用 Pekko HTTP 作為嵌入式伺服器時,預設情況下不會包含任何記錄相依性。如果你也想將記錄新增到嵌入式應用程式,你可以新增 Play logback 模組
libraryDependencies ++= Seq(
logback
)
然後呼叫 LoggerConfigurator
API
import play.api.mvc._
import play.api.routing.sird._
import play.filters.HttpFiltersComponents
import play.core.server.PekkoHttpServer
import play.core.server.ServerConfig
import play.api.Environment
import play.api.ApplicationLoader
import play.api.LoggerConfigurator
import play.api.BuiltInComponentsFromContext
val context = ApplicationLoader.Context.create(Environment.simple())
// Do the logging configuration
LoggerConfigurator(context.environment.classLoader).foreach {
_.configure(context.environment, context.initialConfiguration, Map.empty)
}
val components = new BuiltInComponentsFromContext(context) with HttpFiltersComponents {
override def router: Router = Router.from {
case GET(p"/hello/$to") =>
Action {
Results.Ok(s"Hello $to")
}
}
}
val server = PekkoHttpServer.fromApplication(
components.application,
ServerConfig(
port = Some(19000),
address = "127.0.0.1"
)
)
在此文件發現錯誤?此頁面的原始碼可以在 這裡 找到。閱讀 文件指南 後,請隨時提交拉取請求。有問題或建議要分享?前往 我們的社群論壇 與社群展開討論。