如何在Finch中将RequestReader绑定到Route

Xia*_*ong 5 scala finch

我想知道如何在Finch中将RequestReader和Route绑定在一起.我没有找到一个完整的例子.

这个例子来自finch github,它运行正常.

import io.finch.route._
import com.twitter.finagle.Httpx

val api: Router[String] = get("hello") { "Hello, World!" }

Httpx.serve(":3000", api.toService)
Run Code Online (Sandbox Code Playgroud)

我明白这段代码将获得路径"hello"并将返回响应"hello world"

然后我想将RequestHeader绑定到它.

  val doSomethingWithRequest: RequestReader[String] =
    for {
      foo <- param("foo")
      bar <- param("bar")
    } yield "u got me"

  val api: Router[RequestReader[String]] = Get / "hello" /> doSomethingWithRequest

  val server = Httpx.serve(":3000", api.toService)
Run Code Online (Sandbox Code Playgroud)

我认为这段代码意味着如果网址是" http:// localhost:3000/hello?foo = 3 ",它将返回"你找我"的回复.但是,响应状态为404.

我认为我对Route和RequestHeader之间的组合做错了.

也许有人可以帮助我这个,也就是说,分享一些关于这个芬奇的好文章会更好.该版本频繁出现,文档已过时https://finagle.github.io/blog/2014/12/10/rest-apis-with-finch/

Vla*_*kov 6

谢谢你问这个!我相信这是StackOverflow上第一个有关Finch的问题.

从0.8(今天已经发布)开始,很有可能使用组合器组合Routers和RequestReaders ?(有关更多详细信息,请参见"编写路由器"一节).

以下是说明此功能的示例.

// GET /hello/:name?title=Mr.
val api: Router[String] = 
  get("hello" / string ? param("title")) { (name: String, title: String) =>
    s"Hello, $title$name!"
  }
Httpx.serve(":8081", api.toService)
Run Code Online (Sandbox Code Playgroud)

你提到的博客文章已经过时了,所有博客帖子都是如此.虽然,有一个关于Github回购的综合文档,我们试图保持实际.