From fad17164dfe9252d3e3b6f90c0b74f8378c059e3 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Wed, 9 Oct 2019 08:10:00 +0800 Subject: upgrade to quill 3.4.10, re-enable database examples --- build.sc | 10 +- cask/src/cask/decorators/compress.scala | 5 +- .../1 - Cask: a Scala HTTP micro-framework.md | 116 ++++++++++----------- example/todo/app/src/TodoServer.scala | 15 +-- example/todo/app/test/src/ExampleTests.scala | 2 +- example/todo/build.sc | 2 +- example/todoDb/app/src/TodoMvcDb.scala | 15 +-- example/todoDb/app/test/src/ExampleTests.scala | 2 +- example/todoDb/build.sc | 2 +- 9 files changed, 89 insertions(+), 80 deletions(-) diff --git a/build.sc b/build.sc index 24ce047..10771c3 100644 --- a/build.sc +++ b/build.sc @@ -17,7 +17,7 @@ import $file.example.scalatags.build import $file.example.staticFiles.build import $file.example.todo.build import $file.example.todoApi.build -//import $file.example.todoDb.build +import $file.example.todoDb.build import $file.example.twirl.build import $file.example.variableRoutes.build import $file.example.websockets.build @@ -115,9 +115,9 @@ object example extends Module{ object redirectAbort extends $file.example.redirectAbort.build.AppModule with LocalModule object scalatags extends $file.example.scalatags.build.AppModule with LocalModule object staticFiles extends $file.example.staticFiles.build.AppModule with LocalModule -// object todo extends $file.example.todo.build.AppModule with LocalModule + object todo extends $file.example.todo.build.AppModule with LocalModule object todoApi extends $file.example.todoApi.build.AppModule with LocalModule -// object todoDb extends $file.example.todoDb.build.AppModule with LocalModule + object todoDb extends $file.example.todoDb.build.AppModule with LocalModule object twirl extends $file.example.twirl.build.AppModule with LocalModule object variableRoutes extends $file.example.variableRoutes.build.AppModule with LocalModule object websockets extends $file.example.websockets.build.AppModule with LocalModule @@ -159,9 +159,9 @@ def uploadToGithub(authKey: String) = T.command{ $file.example.redirectAbort.build.millSourcePath, $file.example.scalatags.build.millSourcePath, $file.example.staticFiles.build.millSourcePath, -// $file.example.todo.build.millSourcePath, + $file.example.todo.build.millSourcePath, $file.example.todoApi.build.millSourcePath, -// $file.example.todoDb.build.millSourcePath, + $file.example.todoDb.build.millSourcePath, $file.example.twirl.build.millSourcePath, $file.example.variableRoutes.build.millSourcePath, $file.example.websockets.build.millSourcePath, diff --git a/cask/src/cask/decorators/compress.scala b/cask/src/cask/decorators/compress.scala index 0ffab28..3738bd0 100644 --- a/cask/src/cask/decorators/compress.scala +++ b/cask/src/cask/decorators/compress.scala @@ -7,7 +7,10 @@ import cask.model.{Request, Response} import collection.JavaConverters._ class compress extends cask.RawDecorator{ def wrapFunction(ctx: Request, delegate: Delegate) = { - val acceptEncodings = ctx.exchange.getRequestHeaders.get("Accept-Encoding").asScala.flatMap(_.split(", ")) + val acceptEncodings = Option(ctx.exchange.getRequestHeaders.get("Accept-Encoding")) + .toSeq + .flatMap(_.asScala) + .flatMap(_.split(", ")) delegate(Map()).map{ v => val (newData, newHeaders) = if (acceptEncodings.exists(_.toLowerCase == "gzip")) { new Response.Data { diff --git a/docs/pages/1 - Cask: a Scala HTTP micro-framework.md b/docs/pages/1 - Cask: a Scala HTTP micro-framework.md index 25126e7..aa878c6 100644 --- a/docs/pages/1 - Cask: a Scala HTTP micro-framework.md +++ b/docs/pages/1 - Cask: a Scala HTTP micro-framework.md @@ -391,61 +391,61 @@ etc.. Those can be managed via the normal mechanism for [Serving Static Files](#serving-static-files). - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +## TodoMVC Database Integration + +$$$todoDb + +This example demonstrates how to use Cask to write a TodoMVC API server that +persists it's state in a database rather than in memory. We use the +[Quill](http://getquill.io/) database access library to write a `@transactional` +decorator that automatically opens one transaction per call to an endpoint, +ensuring that database queries are properly committed on success or rolled-back +on error. Note that because the default database connector propagates its +transaction context in a thread-local, `@transactional` does not need to pass +the `ctx` object into each endpoint as an additional parameter list, and so we +simply leave it out. + +While this example is specific to Quill, you can easily modify the +`@transactional` decorator to make it