diff options
-rw-r--r-- | cask/src/cask/Cask.scala | 11 | ||||
-rw-r--r-- | cask/src/cask/Util.scala | 4 | ||||
-rw-r--r-- | cask/test/src/test/cask/CaskTest.scala | 8 | ||||
-rw-r--r-- | cask/test/src/test/cask/HelloWorld.scala | 12 |
4 files changed, 26 insertions, 9 deletions
diff --git a/cask/src/cask/Cask.scala b/cask/src/cask/Cask.scala index 3445594..97c145a 100644 --- a/cask/src/cask/Cask.scala +++ b/cask/src/cask/Cask.scala @@ -10,7 +10,12 @@ import java.nio.ByteBuffer import io.undertow.Undertow import io.undertow.server.{HttpHandler, HttpServerExchange} import io.undertow.util.{Headers, HttpString} -class route(val path: String) extends StaticAnnotation +trait RouteBase{ + val path: String +} +class get(val path: String) extends StaticAnnotation with RouteBase +class post(val path: String) extends StaticAnnotation with RouteBase +class put(val path: String) extends StaticAnnotation with RouteBase class Main(servers: Routes*){ val port: Int = 8080 @@ -107,7 +112,7 @@ object Response{ } } object Routes{ - case class RouteMetadata[T](metadata: route, entryPoint: EntryPoint[T]) + case class RouteMetadata[T](metadata: RouteBase, entryPoint: EntryPoint[T]) case class Metadata[T](value: RouteMetadata[T]*) object Metadata{ implicit def initialize[T] = macro initializeImpl[T] @@ -115,7 +120,7 @@ object Routes{ import c.universe._ val router = new cask.Router(c) val routes = c.weakTypeOf[T].members - .map(m => (m, m.annotations.filter(_.tree.tpe =:= c.weakTypeOf[route]))) + .map(m => (m, m.annotations.filter(_.tree.tpe <:< c.weakTypeOf[RouteBase]))) .collect{case (m, Seq(a)) => ( m, diff --git a/cask/src/cask/Util.scala b/cask/src/cask/Util.scala index 0856f4d..89adfa1 100644 --- a/cask/src/cask/Util.scala +++ b/cask/src/cask/Util.scala @@ -1,7 +1,9 @@ package cask object Util { - def trimSplit(p: String) = p.dropWhile(_ == '/').reverse.dropWhile(_ == '/').reverse.split('/') + def trimSplit(p: String) = + p.dropWhile(_ == '/').reverse.dropWhile(_ == '/').reverse.split('/').filter(_.nonEmpty) + def matchRoute(route: String, path: String): Option[Map[String, String]] = { val routeSegments = trimSplit(route) val pathSegments = trimSplit(path) diff --git a/cask/test/src/test/cask/CaskTest.scala b/cask/test/src/test/cask/CaskTest.scala index 9a0876d..01c905a 100644 --- a/cask/test/src/test/cask/CaskTest.scala +++ b/cask/test/src/test/cask/CaskTest.scala @@ -1,19 +1,17 @@ package test.cask object MyServer extends cask.Routes{ - - - @cask.route("/user/:userName") + @cask.get("/user/:userName") def showUserProfile(userName: String) = { s"User $userName" } - @cask.route("/post/:postId") + @cask.post("/post/:postId") def showPost(postId: Int, query: String) = { s"Post $postId $query" } - @cask.route("/path/::subPath") + @cask.put("/path/::subPath") def showSubpath(subPath: String) = { s"Subpath $subPath" } diff --git a/cask/test/src/test/cask/HelloWorld.scala b/cask/test/src/test/cask/HelloWorld.scala new file mode 100644 index 0000000..69a4370 --- /dev/null +++ b/cask/test/src/test/cask/HelloWorld.scala @@ -0,0 +1,12 @@ +package test.cask + +object HelloRoutes extends cask.Routes{ + @cask.get("/") + def hello() = { + "Hello World!" + } + + initialize() +} + +object HelloWorld extends cask.Main(HelloRoutes) |