summaryrefslogtreecommitdiff
path: root/cask/src/cask/endpoints/StaticEndpoints.scala
blob: 618da5fc9fe6217e860adb54f827636812371755 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cask.endpoints

import cask.router.HttpEndpoint
import cask.model.Request
object StaticUtil{
  def makePath(t: String, ctx: Request) = {
    (cask.internal.Util.splitPath(t) ++ ctx.remainingPathSegments)
      .filter(s => s != "." && s != "..")
      .mkString("/")
  }

}
class staticFiles(val path: String, headers: Seq[(String, String)] = Nil) extends HttpEndpoint[String, Seq[String]]{
  val methods = Seq("get")
  type InputParser[T] = QueryParamReader[T]
  override def subpath = true
  def wrapFunction(ctx: Request, delegate: Delegate): OuterReturned = {
    delegate(Map()).map(t => cask.model.StaticFile(StaticUtil.makePath(t, ctx), headers))
  }

  def wrapPathSegment(s: String): Seq[String] = Seq(s)
}

class staticResources(val path: String,
                      resourceRoot: ClassLoader = getClass.getClassLoader,
                      headers: Seq[(String, String)] = Nil)
  extends HttpEndpoint[String, Seq[String]]{
  val methods = Seq("get")
  type InputParser[T] = QueryParamReader[T]
  override def subpath = true
  def wrapFunction(ctx: Request, delegate: Delegate): OuterReturned = {
    delegate(Map()).map(t =>
      cask.model.StaticResource(StaticUtil.makePath(t, ctx), resourceRoot, headers)
    )
  }

  def wrapPathSegment(s: String): Seq[String] = Seq(s)
}