Creating Scala Play boilerplate
The user wants a boilerplate for a Scala Play Framework application with a basic structure including build.sbt, project/plugins.sbt, conf/routes, controllers, and views. I should assume Play 2.9+ and decide between Scala 2.13 or 3, leaning toward the latest stable version available in October 2024. I’ll finalize a minimal but complete example that includes essential files like project/build.properties, app/controllers/HomeController.scala, and conf/routes, along with a mention of the public folder.Crafting minimal Play Scala boilerplate
In SBT for Play, I should focus on clarity without overwhelming the user. I plan to craft a minimal Play Scala 2.13 boilerplate by checking the canonical build.sbt from the Play docs. It might look like this:
lazy val root = (project in file("."))
.enablePlugins(PlayScala)
.settings(
name := "play-scala-seed",
scalaVersion := "2.13.12",
libraryDependencies ++= Seq(
guice
)
)
For project/plugins.sbt, I’ll add:
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.9.0")
In project/build.properties, I’ll use sbt.version=1.10.0. Then, I'll provide definitions for a simple controller and a view, keeping it concise.Finalizing Play Scala boilerplate
I’m laying out key files for the minimal Play Scala boilerplate. The HTML for the view will look like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>@message</title>
</head>
<body>
<h1>@message</h1>
</body>
</html>
In the conf/routes, I’ll include routes that link to the controller and manage assets. The conf/application.conf should set the allowed hosts like:
play.filters.hosts.allowed = ["localhost", "127.0.0.1", "0:0:0:0:0:0:0:1"]
Finally, I’ll remind the user to run the app with sbt run. Time to compile all this into a neat response! Here’s a minimal but complete boilerplate for a Scala Play Framework app using SBT.
Directory structure
your-app/
app/
controllers/
HomeController.scala
views/
index.scala.html
conf/
application.conf
routes
project/
build.properties
plugins.sbt
public/
stylesheets/
main.css
build.sbt
SBT setup
project/build.properties
sbt.version=1.9.9
(Any recent 1.x version is fine.)
project/plugins.sbt
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.9.0") // use the latest compatible Play version
build.sbt
import play.sbt.PlayImport._
lazy val root = (project in file("."))
.enablePlugins(PlayScala)
.settings(
name := "play-scala-boilerplate",
scalaVersion := "2.13.12",
libraryDependencies ++= Seq(
guice // Dependency injection
)
)
Configuration
conf/application.conf
play.http.errorHandler = "play.api.http.DefaultHttpErrorHandler"
# Allow localhost in dev
play.filters.hosts {
allowed = ["localhost", "127.0.0.1", "0:0:0:0:0:0:0:1"]
}
conf/routes
# Home page
GET / controllers.HomeController.index
# Static assets
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
Controller
app/controllers/HomeController.scala
package controllers
import javax.inject._
import play.api.mvc._
@Singleton
class HomeController @Inject()(val controllerComponents: ControllerComponents)
extends BaseController {
def index = Action { implicit request: Request[AnyContent] =>
Ok(views.html.index("Your new Play application is ready!"))
}
}
View
app/views/index.scala.html
@(message: String)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>@message</title>
<link rel="stylesheet" media="screen" href='@routes.Assets.versioned("stylesheets/main.css")'>
</head>
<body>
<h1>@message</h1>
<p>If you see this page, your Play app boilerplate is working.</p>
</body>
</html>
Static assets (optional)
public/stylesheets/main.css
body {
font-family: Arial, sans-serif;
margin: 2rem;
}
h1 {
color: #336699;
}
Run the application
From the project root:
sbt run
Then open: http://localhost:9000/
If you want, I can also provide a version wired for dependency injection (services), JSON APIs, or with a basic test setup.