summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2019-10-09 10:58:39 +0800
committerLi Haoyi <haoyi.sg@gmail.com>2019-10-09 10:58:39 +0800
commit96375271d90541c89f9f72098df14097e8b4cc3b (patch)
treed4974b99c8409938acf43de7be86303ebf11ee5a
parent70dbe3ed561172a80ebff0515e727434b03040bf (diff)
downloadcask-96375271d90541c89f9f72098df14097e8b4cc3b.tar.gz
cask-96375271d90541c89f9f72098df14097e8b4cc3b.tar.bz2
cask-96375271d90541c89f9f72098df14097e8b4cc3b.zip
Add example of setting static file headers, compressing static files
-rw-r--r--build.sc3
-rw-r--r--docs/pages/1 - Cask: a Scala HTTP micro-framework.md5
-rw-r--r--example/staticFiles2/app/resources/cask/example.txt1
-rw-r--r--example/staticFiles2/app/src/StaticFiles2.scala19
-rw-r--r--example/staticFiles2/app/test/src/ExampleTests.scala35
-rw-r--r--example/staticFiles2/build.sc25
6 files changed, 88 insertions, 0 deletions
diff --git a/build.sc b/build.sc
index dc7f051..7da6002 100644
--- a/build.sc
+++ b/build.sc
@@ -15,6 +15,7 @@ import $file.example.minimalApplication2.build
import $file.example.redirectAbort.build
import $file.example.scalatags.build
import $file.example.staticFiles.build
+import $file.example.staticFiles2.build
import $file.example.todo.build
import $file.example.todoApi.build
import $file.example.todoDb.build
@@ -116,6 +117,7 @@ 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 staticFiles2 extends $file.example.staticFiles2.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
@@ -161,6 +163,7 @@ def uploadToGithub(authKey: String) = T.command{
$file.example.redirectAbort.build.millSourcePath,
$file.example.scalatags.build.millSourcePath,
$file.example.staticFiles.build.millSourcePath,
+ $file.example.staticFiles2.build.millSourcePath,
$file.example.todo.build.millSourcePath,
$file.example.todoApi.build.millSourcePath,
$file.example.todoDb.build.millSourcePath,
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 8cf80ec..2e191d3 100644
--- a/docs/pages/1 - Cask: a Scala HTTP micro-framework.md
+++ b/docs/pages/1 - Cask: a Scala HTTP micro-framework.md
@@ -204,6 +204,11 @@ contents from the corresponding file on disk (and 404 otherwise).
Similarly, `@cask.staticResources` attempts to serve a request based on the JVM
resource path, returning the data if a resource is present and a 404 otherwise.
+You can also configure the `headers` you wish to return to static file requests,
+or use `@cask.decorators.compress` to compress the responses:
+
+$$$staticFiles2
+
## Redirects or Aborts
$$$redirectAbort
diff --git a/example/staticFiles2/app/resources/cask/example.txt b/example/staticFiles2/app/resources/cask/example.txt
new file mode 100644
index 0000000..5184576
--- /dev/null
+++ b/example/staticFiles2/app/resources/cask/example.txt
@@ -0,0 +1 @@
+the quick brown fox jumps over the lazy dog \ No newline at end of file
diff --git a/example/staticFiles2/app/src/StaticFiles2.scala b/example/staticFiles2/app/src/StaticFiles2.scala
new file mode 100644
index 0000000..7354cdb
--- /dev/null
+++ b/example/staticFiles2/app/src/StaticFiles2.scala
@@ -0,0 +1,19 @@
+package app
+object StaticFiles2 extends cask.MainRoutes{
+ @cask.get("/")
+ def index() = {
+ "Hello!"
+ }
+
+ @cask.staticFiles("/static/file", headers = Seq("Cache-Control" -> "max-age=31536000"))
+ def staticFileRoutes() = "app/resources/cask"
+
+ @cask.decorators.compress
+ @cask.staticResources("/static/resource")
+ def staticResourceRoutes() = "cask"
+
+ @cask.staticResources("/static/resource2")
+ def staticResourceRoutes2() = "."
+
+ initialize()
+}
diff --git a/example/staticFiles2/app/test/src/ExampleTests.scala b/example/staticFiles2/app/test/src/ExampleTests.scala
new file mode 100644
index 0000000..514f534
--- /dev/null
+++ b/example/staticFiles2/app/test/src/ExampleTests.scala
@@ -0,0 +1,35 @@
+package app
+import io.undertow.Undertow
+
+import utest._
+
+object ExampleTests extends TestSuite{
+ def withServer[T](example: cask.main.Main)(f: String => T): T = {
+ val server = Undertow.builder
+ .addHttpListener(8080, "localhost")
+ .setHandler(example.defaultHandler)
+ .build
+ server.start()
+ val res =
+ try f("http://localhost:8080")
+ finally server.stop()
+ res
+ }
+
+ val tests = Tests{
+
+ test("StaticFiles") - withServer(StaticFiles2){ host =>
+ requests.get(s"$host/static/file/example.txt").text() ==>
+ "the quick brown fox jumps over the lazy dog"
+
+ requests.get(s"$host/static/resource/example.txt").text() ==>
+ "the quick brown fox jumps over the lazy dog"
+
+ requests.get(s"$host/static/resource2/cask/example.txt").text() ==>
+ "the quick brown fox jumps over the lazy dog"
+
+ requests.get(s"$host/static/file/../../../build.sc").statusCode ==> 404
+ }
+
+ }
+}
diff --git a/example/staticFiles2/build.sc b/example/staticFiles2/build.sc
new file mode 100644
index 0000000..e0f8519
--- /dev/null
+++ b/example/staticFiles2/build.sc
@@ -0,0 +1,25 @@
+import mill._, scalalib._
+
+
+trait AppModule extends ScalaModule{
+ def scalaVersion = "2.13.0"
+
+ def forkWorkingDir = build.millSourcePath
+ def ivyDeps = Agg[Dep](
+ )
+ object test extends Tests{
+ def testFrameworks = Seq("utest.runner.Framework")
+
+ def ivyDeps = Agg(
+ ivy"com.lihaoyi::utest::0.7.1",
+ ivy"com.lihaoyi::requests::0.2.0",
+ )
+
+ def forkWorkingDir = build.millSourcePath
+
+ // redirect this to the forked `test` to make sure static file serving works
+ def testLocal(args: String*) = T.command{
+ test(args:_*)
+ }
+ }
+} \ No newline at end of file