Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lihaoyi/cask
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed Apr 19, 2020
2 parents 275be48 + fedb822 commit 7b71341
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
6 changes: 6 additions & 0 deletions cask/src/cask/endpoints/WebEndpoints.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class post(val path: String, override val subpath: Boolean = false) extends WebE
class put(val path: String, override val subpath: Boolean = false) extends WebEndpoint{
val methods = Seq("put")
}
class patch(val path: String, override val subpath: Boolean = false) extends WebEndpoint{
val methods = Seq("patch")
}
class delete(val path: String, override val subpath: Boolean = false) extends WebEndpoint{
val methods = Seq("delete")
}
class route(val path: String, val methods: Seq[String], override val subpath: Boolean = false) extends WebEndpoint

abstract class QueryParamReader[T]
Expand Down
8 changes: 6 additions & 2 deletions cask/src/cask/main/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,16 @@ object Main{
)
}

def prepareRouteTries(allRoutes: Seq[Routes]) = {
def prepareRouteTries(allRoutes: Seq[Routes]): Map[String, DispatchTrie[(Routes, EndpointMetadata[_])]] = {
val routeList = for{
routes <- allRoutes
route <- routes.caskMetadata.value.map(x => x: EndpointMetadata[_])
} yield (routes, route)
Seq("get", "put", "post", "websocket")

val allMethods: Set[String] =
routeList.flatMap(_._2.endpoint.methods).map(_.toLowerCase).toSet

allMethods
.map { method =>
method -> DispatchTrie.construct[(Routes, EndpointMetadata[_])](0,
for ((route, metadata) <- routeList if metadata.endpoint.methods.contains(method))
Expand Down
2 changes: 2 additions & 0 deletions cask/src/cask/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ package object cask {
type get = endpoints.get
type post = endpoints.post
type put = endpoints.put
type delete = endpoints.delete
type patch = endpoints.patch
type route = endpoints.route
type staticFiles = endpoints.staticFiles
type staticResources = endpoints.staticResources
Expand Down
8 changes: 4 additions & 4 deletions docs/pages/1 - Cask: a Scala HTTP micro-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ via the following coordinates:
```scala
// Mill
ivy"com.lihaoyi::cask:0.5.2"
ivy"com.lihaoyi::cask:0.5.6"
// SBT
"com.lihaoyi" %% "cask" % "0.5.2"
"com.lihaoyi" %% "cask" % "0.5.6"
```
The `./mill` command is just a wrapper around the
Expand Down Expand Up @@ -113,7 +113,7 @@ The rough outline of how the minimal example works should be easy to understand:
`cask.Response` if you want further customization: response code, headers,
etc.
- Your function can tale an optional `cask.Request`, which exposes the entire
- Your function can take an optional `cask.Request`, which exposes the entire
incoming HTTP request if necessary. In the above example, we use it to read
the request body into a string and return it reversed.
Expand Down Expand Up @@ -222,7 +222,7 @@ the relevant headers or status code for you.
Cask doesn't come bundled with HTML templating functionality, but it makes it
really easy to use community-standard libraries like
[Scalatags](https://github.com/lihaoyi/scalatags) to render your HTML. Simply
adding the relevant `ivy"com.lihaoyi::scalatags:0.8.0"` dependency to your
adding the relevant `ivy"com.lihaoyi::scalatags:0.8.2"` dependency to your
`build.sc` file is enough to render Scalatags templates:
$$$scalatags
Expand Down
10 changes: 10 additions & 0 deletions example/httpMethods/app/src/HttpMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@ object HttpMethods extends cask.MainRoutes{
else "show_the_login_form"
}

@cask.route("/session", methods = Seq("delete"))
def session(request: cask.Request) = {
"delete_the_session"
}

@cask.route("/session", methods = Seq("secretmethod"))
def admin(request: cask.Request) = {
"security_by_obscurity"
}

initialize()
}
2 changes: 2 additions & 0 deletions example/httpMethods/app/test/src/ExampleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ object ExampleTests extends TestSuite{
test("HttpMethods") - withServer(HttpMethods){ host =>
requests.post(s"$host/login").text() ==> "do_the_login"
requests.get(s"$host/login").text() ==> "show_the_login_form"
requests.delete(s"$host/session").text() ==> "delete_the_session"
requests.get.copy(verb="secretmethod")(s"$host/session").text() ==> "security_by_obscurity"
}
}
}

0 comments on commit 7b71341

Please sign in to comment.