summaryrefslogtreecommitdiff
path: root/readme.md
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2018-08-08 12:34:20 +0800
committerLi Haoyi <haoyi.sg@gmail.com>2018-08-08 12:36:41 +0800
commita89ebd17dab5af6814d58f02d410acb1eb60e592 (patch)
tree3476274a706099c247453c9bde8ee9e950db4332 /readme.md
parent229ab52fe36c0882bac8aa84ae484a12d339242a (diff)
downloadcask-a89ebd17dab5af6814d58f02d410acb1eb60e592.tar.gz
cask-a89ebd17dab5af6814d58f02d410acb1eb60e592.tar.bz2
cask-a89ebd17dab5af6814d58f02d410acb1eb60e592.zip
Add simple TodoMvcApi server test case
Diffstat (limited to 'readme.md')
-rw-r--r--readme.md52
1 files changed, 51 insertions, 1 deletions
diff --git a/readme.md b/readme.md
index 3774c8e..e1942d5 100644
--- a/readme.md
+++ b/readme.md
@@ -359,4 +359,54 @@ Decorators are useful for things like:
fails), or access to some system resource that needs to be released.
Writing Custom Endpoints
------------------------- \ No newline at end of file
+------------------------
+
+TodoMVC Api Server
+------------------
+
+```scala
+object TodoMvcApi extends cask.MainRoutes{
+ case class Todo(checked: Boolean, text: String)
+ object Todo{
+ implicit def todoRW = upickle.default.macroRW[Todo]
+ }
+ var todos = Seq(
+ Todo(true, "Get started with Cask"),
+ Todo(false, "Profit!")
+ )
+
+ @cask.get("/list/:state")
+ def list(state: String) = {
+ val filteredTodos = state match{
+ case "all" => todos
+ case "active" => todos.filter(!_.checked)
+ case "completed" => todos.filter(_.checked)
+ }
+ upickle.default.write(filteredTodos)
+ }
+
+ @cask.post("/add")
+ def add(request: cask.Request) = {
+ todos = Seq(Todo(false, new String(request.data.readAllBytes()))) ++ todos
+ }
+
+ @cask.post("/toggle/:index")
+ def toggle(index: Int) = {
+ todos = todos.updated(index, todos(index).copy(checked = !todos(index).checked))
+ }
+
+ @cask.post("/delete/:index")
+ def delete(index: Int) = {
+ todos = todos.patch(index, Nil, 1)
+ }
+
+ initialize()
+}
+```
+
+This is a simple self-contained example of using Cask to write an API server for
+the common [TodoMVC example app](http://todomvc.com/).
+
+This minimal example intentionally does not contain javascript, HTML, styles,
+etc.. Those can be managed via the normal mechanism for
+[Serving Static Files](#serving-static-files).