gpt4 book ai didi

scala - 如何将 RequestReader 绑定(bind)到 Finch 中的 Route

转载 作者:行者123 更新时间:2023-12-04 18:37:31 25 4
gpt4 key购买 nike

我想知道如何在 Finch 中将 RequestReader 和 Route 绑定(bind)在一起。我没有找到关于它的完整示例。

这个例子来自 finch github,它工作正常。

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

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

Httpx.serve(":3000", api.toService)

我了解此代码将获得路径“hello”并返回响应“hello world”

然后我想将 RequestHeader 绑定(bind)到它。
  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)

我认为这段代码意味着如果给定 url “ http://localhost:3000/hello?foo=3”,它将返回响应“u got me”。但是,响应状态为 404。

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

也许有人可以帮助我解决这个问题,另外,最好分享一些关于这个 Finch 的好文档。版本更新如此频繁,文档已过时 https://finagle.github.io/blog/2014/12/10/rest-apis-with-finch/

最佳答案

谢谢你问这个!我相信这是关于 StackOverflow 的第一个 Finch 问题。

从 0.8(即 has been released today )开始,很可能组成 Router s 和 RequestReader一起使用 ?组合子(更多细节见 "Composing Routers" 部分)。

这是说明此功能的示例。

// 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)

您提到的博客文章已经过时了,几乎所有博客文章都是如此。虽然,有一个 comprehensive documentation在 Github repo 上,我们试图保持真实。

关于scala - 如何将 RequestReader 绑定(bind)到 Finch 中的 Route,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31931423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com