aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jakob@odersky.com>2018-04-21 15:13:16 -0700
committerJakob Odersky <jakob@odersky.com>2018-04-21 15:13:16 -0700
commit0fe29e5b5c201733cc10ff28759605c7c4cba189 (patch)
tree051ee985a48a71d9235e89af9e95f92c1262d01a
parent82fdb0dee1f3a85b127a71a0c952eadd45b70215 (diff)
downloadcreative-scala-template-0fe29e5b5c201733cc10ff28759605c7c4cba189.tar.gz
creative-scala-template-0fe29e5b5c201733cc10ff28759605c7c4cba189.tar.bz2
creative-scala-template-0fe29e5b5c201733cc10ff28759605c7c4cba189.zip
Http exampleHEADmaster
-rw-r--r--src/main/scala/Example.scala68
1 files changed, 57 insertions, 11 deletions
diff --git a/src/main/scala/Example.scala b/src/main/scala/Example.scala
index e485b9a..bb67e68 100644
--- a/src/main/scala/Example.scala
+++ b/src/main/scala/Example.scala
@@ -1,16 +1,62 @@
-import doodle.core._
-import doodle.core.Image._
-import doodle.syntax._
-import doodle.jvm.Java2DFrame._
-import doodle.backend.StandardInterpreter._
-
-// To use this example, open the SBT console and type:
-//
-// Example.image.draw
+trait HttpRequest {
+ val host: String
+ val path: String
+}
+case class Get(host: String, path: String) extends HttpRequest
+case class Post(host: String, path: String) extends HttpRequest
+case class Delete(host: String, path: String) extends HttpRequest
+
+case class HttpResponse(status: Int, body: String)
+
+case class Server(route: PartialFunction[HttpRequest, HttpResponse]) {
+ def handle(request: HttpRequest): HttpResponse = {
+ if (route.isDefinedAt(request)) {
+ route(request)
+ } else {
+ HttpResponse(500, s"unknown request type $request")
+ }
+
+ }
+}
+
object Example {
- val image = circle(10).fillColor(Color.red) on circle(20) on circle(30)
def main(args: Array[String]): Unit = {
- image.draw
+ //println(check(Complex(Number(3),Number(4))))
+
+ val route1: PartialFunction[HttpRequest, HttpResponse] = {
+ case Get(host, path) => HttpResponse(200, path)
+ case Post("localhost", path) =>
+ // upload a file
+ HttpResponse(200, "file uploaded")
+ case Post(_, path) =>
+ HttpResponse(403, "forbidden")
+ }
+
+ val route2: PartialFunction[HttpRequest, HttpResponse] = {
+ case Delete("localhost", _) => HttpResponse(200, "deleted")
+ }
+
+
+ val route = route1 orElse route2
+
+ val server: Server = Server(route)
+
+
+ val requests: List[HttpRequest] = List(
+ Post("localhost", "foo.txt"),
+ Get("localhost", "foo.txt"),
+ Delete("localhost","foo.txt"),
+ Get("localhost", "foo.txt")
+ )
+
+ val responses: List[HttpResponse] = requests.map(server.handle(_))
+
+ for (response <- responses) {
+ println(response)
+ }
+
}
+
}
+