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/server/src/main/scala/simple/Server.scala | |
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/server/src/main/scala/simple/Server.scala')
-rw-r--r-- | examples/crossBuilds/clientserver2/server/src/main/scala/simple/Server.scala | 51 |
1 files changed, 51 insertions, 0 deletions
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 |