diff options
author | Jakob Odersky <jakob@odersky.com> | 2016-11-13 14:02:14 -0800 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2016-11-13 14:02:14 -0800 |
commit | e317debf1a5d639c9a5fde0f0353a7b3b7ae86a0 (patch) | |
tree | fd0821029975be254ebe47565fae0e991d443151 | |
parent | 4e5778cac0a8be14b072a3626567ad6342d9ce31 (diff) | |
download | play-scalajs-chat-e317debf1a5d639c9a5fde0f0353a7b3b7ae86a0.tar.gz play-scalajs-chat-e317debf1a5d639c9a5fde0f0353a7b3b7ae86a0.tar.bz2 play-scalajs-chat-e317debf1a5d639c9a5fde0f0353a7b3b7ae86a0.zip |
Basic composite app with uPickle
-rw-r--r-- | .gitignore | 7 | ||||
-rw-r--r-- | app/Filters.scala | 24 | ||||
-rw-r--r-- | app/views/index.scala.html | 5 | ||||
-rw-r--r-- | build.sbt | 40 | ||||
-rw-r--r-- | client/src/main/scala/chat/Main.scala | 26 | ||||
-rw-r--r-- | project/plugins.sbt | 4 | ||||
-rw-r--r-- | project/scaffold.sbt | 3 | ||||
-rw-r--r-- | server/app/controllers/HomeController.scala (renamed from app/controllers/HomeController.scala) | 10 | ||||
-rw-r--r-- | server/app/views/index.scala.html | 9 | ||||
-rw-r--r-- | server/app/views/main.scala.html (renamed from app/views/main.scala.html) | 3 | ||||
-rw-r--r-- | server/conf/application.conf (renamed from conf/application.conf) | 0 | ||||
-rw-r--r-- | server/conf/logback.xml (renamed from conf/logback.xml) | 0 | ||||
-rw-r--r-- | server/conf/messages (renamed from conf/messages) | 0 | ||||
-rw-r--r-- | server/conf/routes (renamed from conf/routes) | 1 | ||||
-rw-r--r-- | server/public/images/favicon.png (renamed from public/images/favicon.png) | bin | 687 -> 687 bytes | |||
-rw-r--r-- | server/public/javascripts/main.js (renamed from public/javascripts/main.js) | 0 | ||||
-rw-r--r-- | server/public/stylesheets/main.css (renamed from public/stylesheets/main.css) | 0 | ||||
-rw-r--r-- | server/test/controllers/HomeControllerSpec.scala (renamed from test/controllers/HomeControllerSpec.scala) | 0 | ||||
-rw-r--r-- | shared/src/main/scala/chat/Messages.scala | 3 |
19 files changed, 86 insertions, 49 deletions
@@ -1,8 +1,13 @@ logs -target +target/ /.idea /.idea_modules /.classpath /.project /.settings +/.ensime +/.ensime_cache/ /RUNNING_PID + +/shared/.js/ +/shared/.jvm/ diff --git a/app/Filters.scala b/app/Filters.scala deleted file mode 100644 index 81a2e15..0000000 --- a/app/Filters.scala +++ /dev/null @@ -1,24 +0,0 @@ -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/views/index.scala.html b/app/views/index.scala.html deleted file mode 100644 index 68d37fb..0000000 --- a/app/views/index.scala.html +++ /dev/null @@ -1,5 +0,0 @@ -@() - -@main("Welcome to Play") { - <h1>Welcome to Play!</h1> -} @@ -1,17 +1,35 @@ -name := """play-scalajs-chat""" -organization := "com.lihaoyi" - version := "1.0-SNAPSHOT" +organization in ThisBuild := "com.lihaoyi" +scalaVersion in ThisBuild := "2.11.8" -lazy val root = (project in file(".")).enablePlugins(PlayScala) +lazy val server = (project in file("server")) + .enablePlugins(PlayScala) + .settings( + scalaJSProjects := Seq(client), + pipelineStages in Assets := Seq(scalaJSPipeline), + // triggers scalaJSPipeline when using compile or continuous compilation + compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline).value, + libraryDependencies += "com.lihaoyi" %%% "upickle" % "0.4.3" + ) + .dependsOn(sharedJvm) -scalaVersion := "2.11.8" +lazy val client = (project in file("client")) + .enablePlugins(ScalaJSPlugin, ScalaJSWeb) + .settings( + persistLauncher := true, + persistLauncher in Test := false, + libraryDependencies ++= Seq( + "org.scala-js" %%% "scalajs-dom" % "0.9.1", + "com.lihaoyi" %%% "upickle" % "0.4.3" + ) + ) + .dependsOn(sharedJs) -libraryDependencies += filters -libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test +lazy val shared = (crossProject.crossType(CrossType.Pure) in file("shared")) + .jsConfigure(_ enablePlugins ScalaJSWeb) -// Adds additional packages into Twirl -//TwirlKeys.templateImports += "com.lihaoyi.controllers._" +lazy val sharedJvm = shared.jvm +lazy val sharedJs = shared.js -// Adds additional packages into conf/routes -// play.sbt.routes.RoutesKeys.routesImport += "com.lihaoyi.binders._" +// loads the server project at sbt startup +onLoad in Global := (Command.process("project server", _: State)) compose (onLoad in Global).value diff --git a/client/src/main/scala/chat/Main.scala b/client/src/main/scala/chat/Main.scala new file mode 100644 index 0000000..c68e0d9 --- /dev/null +++ b/client/src/main/scala/chat/Main.scala @@ -0,0 +1,26 @@ +package chat + +import scala.scalajs.js + +import org.scalajs.dom +import scala.util.{ Failure, Success } +import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue +import upickle.default._ + +object Main extends js.JSApp { + + def main(): Unit = { + val root = dom.document.getElementById("root") + + dom.ext.Ajax.get("/message").onComplete { + case Success(msg) if 200 <= msg.status && msg.status < 300 => + root.textContent = "OK, " + read[Message](msg.responseText).data + case Success(msg) => + root.textContent = msg.responseText + case Failure(err) => + root.textContent = "ERROR: " + err + } + //dom.document.getElementById("scalajsShoutOut").textContent = SharedMessages.itWorks + } + +} diff --git a/project/plugins.sbt b/project/plugins.sbt index b772558..d44581f 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,2 +1,6 @@ // The Play plugin addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.9") + +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.13") + +addSbtPlugin("com.vmunier" % "sbt-web-scalajs" % "1.0.3") diff --git a/project/scaffold.sbt b/project/scaffold.sbt deleted file mode 100644 index 91355f7..0000000 --- a/project/scaffold.sbt +++ /dev/null @@ -1,3 +0,0 @@ -// Defines scaffolding (found under .g8 folder) -// sbt "g8Scaffold form" -addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.7.1") diff --git a/app/controllers/HomeController.scala b/server/app/controllers/HomeController.scala index 2032080..13bc8dc 100644 --- a/app/controllers/HomeController.scala +++ b/server/app/controllers/HomeController.scala @@ -1,15 +1,14 @@ package controllers -import javax.inject._ import play.api._ import play.api.mvc._ +import upickle.default._ /** * This controller creates an `Action` to handle HTTP requests to the * application's home page. */ -@Singleton -class HomeController @Inject() extends Controller { +class HomeController extends Controller { /** * Create an Action to render an HTML page. @@ -21,4 +20,9 @@ class HomeController @Inject() extends Controller { def index = Action { implicit request => Ok(views.html.index()) } + + def message = Action { implicit request => + Ok(write(chat.Message("hello"))) + } + } diff --git a/server/app/views/index.scala.html b/server/app/views/index.scala.html new file mode 100644 index 0000000..911bdaf --- /dev/null +++ b/server/app/views/index.scala.html @@ -0,0 +1,9 @@ +@() + +@main("Welcome to Play") { + <main id="root">placeholder</main> + <script src="@routes.Assets.versioned("client-fastopt.js")" type="text/javascript"></script> + <script type="text/javascript"> + chat.Main().main() + </script> +} diff --git a/app/views/main.scala.html b/server/app/views/main.scala.html index 808a8b8..0e9906e 100644 --- a/app/views/main.scala.html +++ b/server/app/views/main.scala.html @@ -13,13 +13,12 @@ <title>@title</title> <link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")"> <link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")"> - </head> <body> @* And here's where we render the `Html` object containing * the page content. *@ @content - <script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script> + <script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script> </body> </html> diff --git a/conf/application.conf b/server/conf/application.conf index cb94680..cb94680 100644 --- a/conf/application.conf +++ b/server/conf/application.conf diff --git a/conf/logback.xml b/server/conf/logback.xml index 9df7f6e..9df7f6e 100644 --- a/conf/logback.xml +++ b/server/conf/logback.xml diff --git a/conf/messages b/server/conf/messages index 0226738..0226738 100644 --- a/conf/messages +++ b/server/conf/messages diff --git a/conf/routes b/server/conf/routes index 2ac6b33..ca64d25 100644 --- a/conf/routes +++ b/server/conf/routes @@ -5,6 +5,7 @@ # An example controller showing a sample home page GET / controllers.HomeController.index +GET /message controllers.HomeController.message # 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/public/images/favicon.png b/server/public/images/favicon.png Binary files differindex c7d92d2..c7d92d2 100644 --- a/public/images/favicon.png +++ b/server/public/images/favicon.png diff --git a/public/javascripts/main.js b/server/public/javascripts/main.js index e69de29..e69de29 100644 --- a/public/javascripts/main.js +++ b/server/public/javascripts/main.js diff --git a/public/stylesheets/main.css b/server/public/stylesheets/main.css index e69de29..e69de29 100644 --- a/public/stylesheets/main.css +++ b/server/public/stylesheets/main.css diff --git a/test/controllers/HomeControllerSpec.scala b/server/test/controllers/HomeControllerSpec.scala index 836e3ef..836e3ef 100644 --- a/test/controllers/HomeControllerSpec.scala +++ b/server/test/controllers/HomeControllerSpec.scala diff --git a/shared/src/main/scala/chat/Messages.scala b/shared/src/main/scala/chat/Messages.scala new file mode 100644 index 0000000..deff8eb --- /dev/null +++ b/shared/src/main/scala/chat/Messages.scala @@ -0,0 +1,3 @@ +package chat + +case class Message(data: String) |