From 4e5778cac0a8be14b072a3626567ad6342d9ce31 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sun, 13 Nov 2016 12:00:05 -0800 Subject: Initial commit --- .gitignore | 8 ++++++ app/Filters.scala | 24 ++++++++++++++++ app/controllers/HomeController.scala | 24 ++++++++++++++++ app/views/index.scala.html | 5 ++++ app/views/main.scala.html | 25 +++++++++++++++++ build.sbt | 17 +++++++++++ conf/application.conf | 1 + conf/logback.xml | 41 +++++++++++++++++++++++++++ conf/messages | 1 + conf/routes | 10 +++++++ project/build.properties | 1 + project/plugins.sbt | 2 ++ project/scaffold.sbt | 3 ++ public/images/favicon.png | Bin 0 -> 687 bytes public/javascripts/main.js | 0 public/stylesheets/main.css | 0 test/controllers/HomeControllerSpec.scala | 45 ++++++++++++++++++++++++++++++ 17 files changed, 207 insertions(+) create mode 100644 .gitignore create mode 100644 app/Filters.scala create mode 100644 app/controllers/HomeController.scala create mode 100644 app/views/index.scala.html create mode 100644 app/views/main.scala.html create mode 100644 build.sbt create mode 100644 conf/application.conf create mode 100644 conf/logback.xml create mode 100644 conf/messages create mode 100644 conf/routes create mode 100644 project/build.properties create mode 100644 project/plugins.sbt create mode 100644 project/scaffold.sbt create mode 100644 public/images/favicon.png create mode 100644 public/javascripts/main.js create mode 100644 public/stylesheets/main.css create mode 100644 test/controllers/HomeControllerSpec.scala diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb372fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +logs +target +/.idea +/.idea_modules +/.classpath +/.project +/.settings +/RUNNING_PID diff --git a/app/Filters.scala b/app/Filters.scala new file mode 100644 index 0000000..81a2e15 --- /dev/null +++ b/app/Filters.scala @@ -0,0 +1,24 @@ +import javax.inject.Inject + +import play.api.http.DefaultHttpFilters + +import play.filters.csrf.CSRFFilter +import play.filters.headers.SecurityHeadersFilter +import play.filters.hosts.AllowedHostsFilter + +/** + * Add the following filters by default to all projects + * + * https://www.playframework.com/documentation/latest/ScalaCsrf + * https://www.playframework.com/documentation/latest/AllowedHostsFilter + * https://www.playframework.com/documentation/latest/SecurityHeaders + */ +class Filters @Inject() ( + csrfFilter: CSRFFilter, + allowedHostsFilter: AllowedHostsFilter, + securityHeadersFilter: SecurityHeadersFilter +) extends DefaultHttpFilters( + csrfFilter, + allowedHostsFilter, + securityHeadersFilter +) \ No newline at end of file diff --git a/app/controllers/HomeController.scala b/app/controllers/HomeController.scala new file mode 100644 index 0000000..2032080 --- /dev/null +++ b/app/controllers/HomeController.scala @@ -0,0 +1,24 @@ +package controllers + +import javax.inject._ +import play.api._ +import play.api.mvc._ + +/** + * This controller creates an `Action` to handle HTTP requests to the + * application's home page. + */ +@Singleton +class HomeController @Inject() extends Controller { + + /** + * Create an Action to render an HTML page. + * + * The configuration in the `routes` file means that this method + * will be called when the application receives a `GET` request with + * a path of `/`. + */ + def index = Action { implicit request => + Ok(views.html.index()) + } +} diff --git a/app/views/index.scala.html b/app/views/index.scala.html new file mode 100644 index 0000000..68d37fb --- /dev/null +++ b/app/views/index.scala.html @@ -0,0 +1,5 @@ +@() + +@main("Welcome to Play") { +

Welcome to Play!

+} diff --git a/app/views/main.scala.html b/app/views/main.scala.html new file mode 100644 index 0000000..808a8b8 --- /dev/null +++ b/app/views/main.scala.html @@ -0,0 +1,25 @@ +@* + * This template is called from the `index` template. This template + * handles the rendering of the page header and body tags. It takes + * two arguments, a `String` for the title of the page and an `Html` + * object to insert into the body of the page. + *@ +@(title: String)(content: Html) + + + + + @* Here's where we render the page title `String`. *@ + @title + + + + + + @* And here's where we render the `Html` object containing + * the page content. *@ + @content + + + + diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..3a529fe --- /dev/null +++ b/build.sbt @@ -0,0 +1,17 @@ +name := """play-scalajs-chat""" +organization := "com.lihaoyi" + +version := "1.0-SNAPSHOT" + +lazy val root = (project in file(".")).enablePlugins(PlayScala) + +scalaVersion := "2.11.8" + +libraryDependencies += filters +libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test + +// Adds additional packages into Twirl +//TwirlKeys.templateImports += "com.lihaoyi.controllers._" + +// Adds additional packages into conf/routes +// play.sbt.routes.RoutesKeys.routesImport += "com.lihaoyi.binders._" diff --git a/conf/application.conf b/conf/application.conf new file mode 100644 index 0000000..cb94680 --- /dev/null +++ b/conf/application.conf @@ -0,0 +1 @@ +# https://www.playframework.com/documentation/latest/Configuration diff --git a/conf/logback.xml b/conf/logback.xml new file mode 100644 index 0000000..9df7f6e --- /dev/null +++ b/conf/logback.xml @@ -0,0 +1,41 @@ + + + + + + + ${application.home:-.}/logs/application.log + + %date [%level] from %logger in %thread - %message%n%xException + + + + + + %coloredLevel %logger{15} - %message%n%xException{10} + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/conf/messages b/conf/messages new file mode 100644 index 0000000..0226738 --- /dev/null +++ b/conf/messages @@ -0,0 +1 @@ +# https://www.playframework.com/documentation/latest/ScalaI18N diff --git a/conf/routes b/conf/routes new file mode 100644 index 0000000..2ac6b33 --- /dev/null +++ b/conf/routes @@ -0,0 +1,10 @@ +# Routes +# This file defines all application routes (Higher priority routes first) +# https://www.playframework.com/documentation/latest/ScalaRouting +# ~~~~ + +# An example controller showing a sample home page +GET / controllers.HomeController.index + +# Map static resources from the /public folder to the /assets URL path +GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..27e88aa --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=0.13.13 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..b772558 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1,2 @@ +// The Play plugin +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.9") diff --git a/project/scaffold.sbt b/project/scaffold.sbt new file mode 100644 index 0000000..91355f7 --- /dev/null +++ b/project/scaffold.sbt @@ -0,0 +1,3 @@ +// Defines scaffolding (found under .g8 folder) +// sbt "g8Scaffold form" +addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.7.1") diff --git a/public/images/favicon.png b/public/images/favicon.png new file mode 100644 index 0000000..c7d92d2 Binary files /dev/null and b/public/images/favicon.png differ diff --git a/public/javascripts/main.js b/public/javascripts/main.js new file mode 100644 index 0000000..e69de29 diff --git a/public/stylesheets/main.css b/public/stylesheets/main.css new file mode 100644 index 0000000..e69de29 diff --git a/test/controllers/HomeControllerSpec.scala b/test/controllers/HomeControllerSpec.scala new file mode 100644 index 0000000..836e3ef --- /dev/null +++ b/test/controllers/HomeControllerSpec.scala @@ -0,0 +1,45 @@ +package controllers + +import org.scalatestplus.play._ +import play.api.test._ +import play.api.test.Helpers._ + +/** + * Add your spec here. + * You can mock out a whole application including requests, plugins etc. + * + * For more information, see https://www.playframework.com/documentation/latest/ScalaTestingWithScalaTest + */ +class HomeControllerSpec extends PlaySpec with OneAppPerTest { + + "HomeController GET" should { + + "render the index page from a new instance of controller" in { + val controller = new HomeController + val home = controller.index().apply(FakeRequest()) + + status(home) mustBe OK + contentType(home) mustBe Some("text/html") + contentAsString(home) must include ("Welcome to Play") + } + + "render the index page from the application" in { + val controller = app.injector.instanceOf[HomeController] + val home = controller.index().apply(FakeRequest()) + + status(home) mustBe OK + contentType(home) mustBe Some("text/html") + contentAsString(home) must include ("Welcome to Play") + } + + "render the index page from the router" in { + // Need to specify Host header to get through AllowedHostsFilter + val request = FakeRequest(GET, "/").withHeaders("Host" -> "localhost") + val home = route(app, request).get + + status(home) mustBe OK + contentType(home) mustBe Some("text/html") + contentAsString(home) must include ("Welcome to Play") + } + } +} -- cgit v1.2.3