Skip to content

Instantly share code, notes, and snippets.

@seratch
Created October 25, 2011 15:29
Show Gist options
  • Save seratch/1313140 to your computer and use it in GitHub Desktop.
Save seratch/1313140 to your computer and use it in GitHub Desktop.
unfiltered snippets #daimonscala 20
import unfiltered.request._
import unfiltered.response._
// Planify使ってpartial functionでintentの中だけ書く
val echo = unfiltered.filter.Planify {
case Path(Seg(p :: Nil)) => ResponseString(p)
}
unfiltered.jetty.Http.anylocal.filter(echo).run()
// Planを継承したobjectを作ってintentを定義する
object Echo extends unfiltered.filter.Plan {
def intent = {
case Path(Seg(p :: Nil)) => ResponseString(p)
}
}
unfiltered.jetty.Http.anylocal.filter(Echo).run()
// filterでPlanをもう一つ追加する
val nice = unfiltered.filter.Planify {
case _ => ResponseString("I can echo exactly one path element.")
}
unfiltered.jetty.Http.anylocal.filter(echo).filter(nice).run()
// echoとniceを一つのPlanにする
val echoNice = unfiltered.filter.Planify {
case Path(Seg(p :: Nil)) => ResponseString(p)
case _ => ResponseString("I can echo exactly one path element.")
}
unfiltered.jetty.Http.anylocal.filter(echoNice).run()
// GETパラメータを受け取る
val search = unfiltered.filter.Planify {
case req @ Path(Seg("search1" :: Nil)) => req match {
case GET(_) => {
// http://localhost:xxxx/search1?q=scala -> q:scala
val Params(params) = req
ResponseString(params("q").head)
}
}
case req @ Path(Seg("search2" :: Nil)) & QueryString(qs) => req match {
case GET(_) => {
// http://localhost:xxxx/search2?q=scala&foo=bar -> q=scala&foo=bar
ResponseString(qs)
}
}
}
unfiltered.jetty.Http.anylocal.filter(search).run()
// POSTでパラメータを受け取る
val loginForm = unfiltered.filter.Planify {
case req @ Path(Seg("login" :: Nil)) => req match {
case GET(_) => {
Html((<form action="/login" method="post">
<input type="text" name="id" />
<input type="password" name="password" />
<input type="submit" />
</form>))
}
case POST(_) => {
val Params(params) = req
ResponseString(params("id").head + "&" + params("password").head)
}
}
}
unfiltered.jetty.Http.anylocal.filter(loginForm).run()
// URLにパラメータを埋め込む
val urlSep = unfiltered.filter.Planify {
case GET(Path(Seg("foo" :: id :: Nil))) => {
// http://localhost:xxxx/foo/12345 -> id:12345
// http://localhost:xxxx/foo/12345/ -> id:12345
ResponseString(id)
}
}
unfiltered.jetty.Http.anylocal.filter(urlSep).run()
// POSTでファイルアップロードして中身を受け取る
// unfiltered-netty
// Basic認証を追加する
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment