diff options
author | Li Haoyi <haoyi@dropbox.com> | 2015-02-04 06:28:31 -0800 |
---|---|---|
committer | Li Haoyi <haoyi@dropbox.com> | 2015-02-04 06:28:31 -0800 |
commit | 3e92267b4f030f7bc7f0040fef50bd07e4709b21 (patch) | |
tree | e10900fcd96d9b6c8cc53e5b641bdd11ebfe59b8 /examples/crossBuilds/clientserver2/app/jvm/src/main | |
parent | b094df4a6a6cf1bf7eeb36d842970aa8b3d94d5f (diff) | |
download | hands-on-scala-js-3e92267b4f030f7bc7f0040fef50bd07e4709b21.tar.gz hands-on-scala-js-3e92267b4f030f7bc7f0040fef50bd07e4709b21.tar.bz2 hands-on-scala-js-3e92267b4f030f7bc7f0040fef50bd07e4709b21.zip |
clientserver examples now work with crossProject
Diffstat (limited to 'examples/crossBuilds/clientserver2/app/jvm/src/main')
-rw-r--r-- | examples/crossBuilds/clientserver2/app/jvm/src/main/scala/simple/Page.scala | 21 | ||||
-rw-r--r-- | examples/crossBuilds/clientserver2/app/jvm/src/main/scala/simple/Server.scala | 54 |
2 files changed, 75 insertions, 0 deletions
diff --git a/examples/crossBuilds/clientserver2/app/jvm/src/main/scala/simple/Page.scala b/examples/crossBuilds/clientserver2/app/jvm/src/main/scala/simple/Page.scala new file mode 100644 index 0000000..c04107b --- /dev/null +++ b/examples/crossBuilds/clientserver2/app/jvm/src/main/scala/simple/Page.scala @@ -0,0 +1,21 @@ +package simple +import scalatags.Text.all._ + +object Page{ + val boot = + "simple.Client().main(document.getElementById('contents'))" + val skeleton = + html( + head( + script(src:="/appjs-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/app/jvm/src/main/scala/simple/Server.scala b/examples/crossBuilds/clientserver2/app/jvm/src/main/scala/simple/Server.scala new file mode 100644 index 0000000..8967c17 --- /dev/null +++ b/examples/crossBuilds/clientserver2/app/jvm/src/main/scala/simple/Server.scala @@ -0,0 +1,54 @@ +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 |