summaryrefslogtreecommitdiff
path: root/examples/crossBuilds/clientserver/server/src/main/scala/simple/Server.scala
blob: 6b2fea30be53cb7374c2aae7ae2d601f087dafa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package simple

import akka.actor.ActorSystem
import spray.http.{HttpEntity, MediaTypes}
import spray.routing.SimpleRoutingApp

object Server extends SimpleRoutingApp{

  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"){
          extract(_.request.entity.asString) { e =>
            complete {
              val (dir, last) = e.splitAt(e.lastIndexOf("/") + 1)
              val files =
                Option(new java.io.File("./" + dir).listFiles())
                  .toSeq.flatten
              upickle.write(
                for{
                  f <- files
                  if f.getName.startsWith(last)
                } yield FileData(f.getName, f.length())
              )
            }
          }
        }
      }
    }
  }
}