Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind RequestReader to Route in Finch

Tags:

scala

finch

I wonder how to bind RequestReader and Route together in Finch. I didnt find a complete example about it.

This example is coming from finch github, and it is working properly.

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

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

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

I understand that this code will get path "hello" and will return the response "hello world"

and then I want to bind RequestHeader to it.

  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)

I thought this code means if the url is given "http://localhost:3000/hello?foo=3" it will return the response "u got me". However, the response status is 404.

I think I did something wrong for the combination between Route and RequestHeader.

Maybe someone can help me on this one, also, it would be better to share some good doc about this Finch. the version is bumping up so frequently and the doc is outdated https://finagle.github.io/blog/2014/12/10/rest-apis-with-finch/

like image 952
Xiaohe Dong Avatar asked Aug 11 '15 00:08

Xiaohe Dong


1 Answers

Thanks for asking this! I believe this is the first ever Finch question on StackOverflow.

Since 0.8 (that has been released today) it's quite possible to compose Routers and RequestReaders together using the ? combinator (see section "Composing Routers" for more details).

Here is the example that illustrates this functionality.

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

The blog post you're mentioning is dramatically outdated, which is pretty much the case for all the blog posts. Although, there is a comprehensive documentation on the Github repo, which we're trying to keep actual.

like image 189
Vladimir Kostyukov Avatar answered Sep 21 '22 16:09

Vladimir Kostyukov