diff options
author | lihaoyi <haoyi.sg@gmail.com> | 2014-11-23 05:37:28 -0800 |
---|---|---|
committer | lihaoyi <haoyi.sg@gmail.com> | 2014-11-23 05:37:28 -0800 |
commit | 1c4cb72b209ab11c9e52c3bb490adf759f17fd0c (patch) | |
tree | fddc2a4f7b4848902b31aa4fa5c77023045f0166 /examples/crossBuilds/clientserver2 | |
parent | bc640b3b440735a9343185856fdbec2ab064a369 (diff) | |
download | hands-on-scala-js-1c4cb72b209ab11c9e52c3bb490adf759f17fd0c.tar.gz hands-on-scala-js-1c4cb72b209ab11c9e52c3bb490adf759f17fd0c.tar.bz2 hands-on-scala-js-1c4cb72b209ab11c9e52c3bb490adf759f17fd0c.zip |
Added autowire section in client-server
Diffstat (limited to 'examples/crossBuilds/clientserver2')
6 files changed, 162 insertions, 0 deletions
diff --git a/examples/crossBuilds/clientserver2/build.sbt b/examples/crossBuilds/clientserver2/build.sbt new file mode 100644 index 0000000..c64a6fd --- /dev/null +++ b/examples/crossBuilds/clientserver2/build.sbt @@ -0,0 +1,37 @@ +import utest.jsrunner.JsCrossBuild +import scala.scalajs.sbtplugin.ScalaJSPlugin._ +import ScalaJSKeys._ +val sharedSettings = Seq( + unmanagedSourceDirectories in Compile += + baseDirectory.value / "shared" / "main" / "scala", + libraryDependencies ++= Seq( + "com.scalatags" %%% "scalatags" % "0.4.2", + "com.lihaoyi" %%% "upickle" % "0.2.5", + "com.lihaoyi" %%% "autowire" % "0.2.3" + ), + scalaVersion := "2.11.4" +) + +lazy val client = project.in(file("client")) + .settings(scalaJSSettings:_*) + .settings(sharedSettings:_*) + .settings( + libraryDependencies ++= Seq( + "org.scala-lang.modules.scalajs" %%% "scalajs-dom" % "0.6" + ) +) + +lazy val server = project.in(file("server")) + .settings(sharedSettings:_*) + .settings( + libraryDependencies ++= Seq( + "io.spray" %% "spray-can" % "1.3.2", + "io.spray" %% "spray-routing" % "1.3.2", + "com.typesafe.akka" %% "akka-actor" % "2.3.6" + ), + (resources in Compile) += { + (fastOptJS in (client, Compile)).value + (artifactPath in (client, Compile, fastOptJS)).value + } +) + diff --git a/examples/crossBuilds/clientserver2/client/src/main/scala/simple/Client.scala b/examples/crossBuilds/clientserver2/client/src/main/scala/simple/Client.scala new file mode 100644 index 0000000..f10eede --- /dev/null +++ b/examples/crossBuilds/clientserver2/client/src/main/scala/simple/Client.scala @@ -0,0 +1,47 @@ +//js/src/main/scala/simple/Platform.scala +package simple +import scalatags.JsDom.all._ +import org.scalajs.dom +import scala.scalajs.js.annotation.JSExport +import scalajs.concurrent.JSExecutionContext.Implicits.runNow +import autowire._ + +object Ajaxer extends autowire.Client[String, upickle.Reader, upickle.Writer]{ + override def doCall(req: Request) = { + dom.extensions.Ajax.post( + url = "/ajax/" + req.path.mkString("/"), + data = upickle.write(req.args) + ).map(_.responseText) + } + + def read[Result: upickle.Reader](p: String) = upickle.read[Result](p) + def write[Result: upickle.Writer](r: Result) = upickle.write(r) +} + +@JSExport +object Client extends{ + @JSExport + def main(container: dom.HTMLDivElement) = { + val inputBox = input.render + val outputBox = ul.render + def update() = Ajaxer[Api].list(inputBox.value).call().foreach{ data => + outputBox.innerHTML = "" + for(FileData(name, size) <- data){ + outputBox.appendChild( + li( + b(name), " - ", size, " bytes" + ).render + ) + } + } + inputBox.onkeyup = (e: dom.Event) => update() + update() + container.appendChild( + div( + h1("File Search"), + inputBox, + outputBox + ).render + ) + } +}
\ No newline at end of file diff --git a/examples/crossBuilds/clientserver2/project/build.sbt b/examples/crossBuilds/clientserver2/project/build.sbt new file mode 100644 index 0000000..7c60a91 --- /dev/null +++ b/examples/crossBuilds/clientserver2/project/build.sbt @@ -0,0 +1,5 @@ +/*project/build.sbt*/ +addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.5.5") + +addSbtPlugin("com.lihaoyi" % "utest-js-plugin" % "0.2.4") + diff --git a/examples/crossBuilds/clientserver2/server/shared b/examples/crossBuilds/clientserver2/server/shared new file mode 120000 index 0000000..f32be42 --- /dev/null +++ b/examples/crossBuilds/clientserver2/server/shared @@ -0,0 +1 @@ +../client/shared
\ No newline at end of file diff --git a/examples/crossBuilds/clientserver2/server/src/main/scala/simple/Page.scala b/examples/crossBuilds/clientserver2/server/src/main/scala/simple/Page.scala new file mode 100644 index 0000000..ce6617c --- /dev/null +++ b/examples/crossBuilds/clientserver2/server/src/main/scala/simple/Page.scala @@ -0,0 +1,21 @@ +package simple +import scalatags.Text.all._ + +object Page{ + val boot = + "Client().main(document.getElementById('contents'))" + val skeleton = + html( + head( + script(src:="/client-fastopt.js"), + link( + rel:="stylesheet", + href:="http://yui.yahooapis.com/pure/0.5.0/pure-min.css" + ) + ), + body( + onload:=boot, + div(id:="contents") + ) + ) +} diff --git a/examples/crossBuilds/clientserver2/server/src/main/scala/simple/Server.scala b/examples/crossBuilds/clientserver2/server/src/main/scala/simple/Server.scala new file mode 100644 index 0000000..df5f877 --- /dev/null +++ b/examples/crossBuilds/clientserver2/server/src/main/scala/simple/Server.scala @@ -0,0 +1,51 @@ +package simple + +import akka.actor.ActorSystem +import spray.http.{HttpEntity, MediaTypes} +import spray.routing.SimpleRoutingApp +import scala.concurrent.ExecutionContext.Implicits.global + +object Router extends autowire.Server[String, upickle.Reader, upickle.Writer]{ + def read[Result: upickle.Reader](p: String) = upickle.read[Result](p) + def write[Result: upickle.Writer](r: Result) = upickle.write(r) +} + +object Server extends SimpleRoutingApp with Api{ + def main(args: Array[String]): Unit = { + implicit val system = ActorSystem() + startServer("localhost", port = 8080){ + get{ + pathSingleSlash{ + complete{ + HttpEntity( + MediaTypes.`text/html`, + Page.skeleton.render + ) + } + } ~ + getFromResourceDirectory("") + } ~ + post{ + path("ajax" / Segments){ s => + extract(_.request.entity.asString) { e => + complete { + Router.route[Api](Server)( + autowire.Core.Request(s, upickle.read[Map[String, String]](e)) + ) + } + } + } + } + } + } + def list(path: String) = { + val (dir, last) = path.splitAt(path.lastIndexOf("/") + 1) + val files = + Option(new java.io.File("./" + dir).listFiles()) + .toSeq.flatten + for{ + f <- files + if f.getName.startsWith(last) + } yield FileData(f.getName, f.length()) + } +}
\ No newline at end of file |