Introduction
Scala developer is an expert in developing, building, and maintaining Scala-based applications. Additionally, they perform software analysis, write code per app specifications, and work with the software development team to validate application designs.
This article explains the details of Embedding Play in Scala developers along with the details of Embedding an Akka Http server and Embedding a Netty server in your application.
Without further ado, let's get started.

Embedding an Akka Http server in your application
You can embed a Play server within your own existing application, even though Play apps are most frequently utilised as their own containers. The Twirl template compiler and the Play routes compiler can be used in conjunction with this, but they are not required. An application with just a few straightforward routes is a typical use case. You must meet the following requirement in order to utilise Akka HTTP Server embedded:
Code:
libraryDependencies ++= Seq(
akkaHttpServer
)
The factory methods of the AkkaHttpServer object can be used to launch a Play Akka HTTP server. You might choose to utilise the String Interpolating Routing DSL in conjunction with the fromRouterWithComponents method if all you need to do is supply a few simple routes:
Code:
import play.api.mvc._
import play.api.routing.sird._
import play.core.server.AkkaHttpServer
val server = AkkaHttpServer.fromRouterWithComponents() { components =>
import Results._
import components.{ defaultActionBuilder => Action }
{
case GET(p"/hello/$to") =>
Action {
Ok(s"Hello $to")
}
}
}
By default, a server in prod mode will be started on port 9000. A ServerConfig can be used to configure the server.
Code:
import play.api.mvc._
import play.api.routing.sird._
import play.core.server.AkkaHttpServer
import play.core.server._
val server = AkkaHttpServer.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")
}
}
}
In addition, Play offers component qualities that make it simple to customise components other than the router. For this purpose, the AkkaHttpServerComponents trait is offered, and it may be easily used with BuiltInComponents to create the necessary application. The equivalent of AkkaHttpServerComponents with BuiltInComponents and NoHttpFiltersComponents in this example is DefaultAkkaHttpServerComponents:
Code:
import play.api.http.DefaultHttpErrorHandler
import play.api.mvc._
import play.api.routing.Router
import play.api.routing.sird._
import play.core.server.DefaultAkkaHttpServerComponents
import scala.concurrent.Future
val components = new DefaultAkkaHttpServerComponents {
override lazy val router: Router = Router.from {
case GET(p"/hello/$to") =>
Action {
Results.Ok(s"Hello $to")
}
}
override lazy val httpErrorHandler = 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
In this case, a router is the only technique you need to use. The default implementation of anything else can be changed by overriding specific methods, like in the case of the aforementioned httpErrorHandler. By altering the serverConfig property, the server configuration can be changed.
Once you've started the server, just call the stop method to end it.
Code:
server.stop()
Another approach is to use GuiceApplicationBuilder and the fromApplication function to construct a Play Application:
Code:
import play.api.mvc._
import play.api.routing.sird._
import play.core.server.AkkaHttpServer
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 = AkkaHttpServer.fromApplication(
components.application,
ServerConfig(
port = Some(19000),
address = "127.0.0.1"
)
)
Logging configuration
There are no logging dependencies added by default when using Akka HTTP as an embedded server. The Play logback module can be added to the embedded program if you also want to include logging:
Code:
libraryDependencies ++= Seq(
logback
)
Calling the LoggerConfigurator API later
Code:
import play.api.mvc._
import play.api.routing.sird._
import play.filters.HttpFiltersComponents
import play.core.server.AkkaHttpServer
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 = AkkaHttpServer.fromApplication(
components.application,
ServerConfig(
port = Some(19000),
address = "127.0.0.1"
)
)
Let's look at the details of embedding a Netty server in your application.