import io.finch._
import cats.effect.IO
import com.twitter.finagle.Http
import com.twitter.io.Buf
import com.twitter.util.Await
object Test extends App with Endpoint.Module[IO] {
implicit val encodeException: Encode.Text[Exception] =
Encode.text((_, cs) => Buf.ByteArray.Owned("ERR!".getBytes(cs.name)))
val api: Endpoint[IO, String] = get("hello") {
throw new Exception("test")
Ok("Hello, World!")
}
Await.ready(Http.server.serve(":8080", api.toServiceAs[Text.Plain]))
}
http://127.0.0.1:8080/hello does not output ERR! in the browser
Service[Request, Response]
or use middleware filters like here:import io.finch._
import cats.effect.IO
import com.twitter.finagle.Http
import com.twitter.io.Buf
import com.twitter.util.Await
object Test extends App with Endpoint.Module[IO] {
implicit val encodeException: Encode.Text[Exception] =
Encode.text((_, cs) => Buf.ByteArray.Owned("ERR!".getBytes(cs.name)))
val api: Endpoint[IO, String] = get("hello") {
if (Random.nextBoolean()) {
Ok("Hello, World!")
} else {
BadRequest(new Exception("oh no"))
}
}
Await.ready(Http.server.serve(":8080", api.toServiceAs[Text.Plain]))
}
This should work
*>
come from?
override protected def getCompiledOverallEndpoint(state: ServerState): Endpoint.Compiled[IO] =
Bootstrap.serve[Application.Json](getEndpointsForState(state)).compile :+: ???
:+:
doesn't work: that operator doesn't exist on compiled endpoints apparently
Bootstrap
.serve[Application.Json](getJSONEndpointsForState(state))
.serve[Text.Plain](getTextEndpointsForState(state))
.compile
Hi,
I have strange error while trying to compile one of the examples:
https://github.com/finagle/finch/blob/master/examples/src/main/scala/io/finch/iteratee/Main.scala#L54
the error is could not find implicit value for parameter LR: io.finch.LiftReader[io.iteratee.Enumerator,cats.effect.IO]
I don't see what it is missing especially that I copied exactly the same imports
this is my build
"com.github.finagle" %% "finchx-core" % 0.32.1",
"com.github.finagle" %% "finchx-circe" % 0.32.1",
"com.github.finagle" %% "finchx-iteratee" % 0.32.1",
Am I missing some library or import ? Anyone have an idea ?
Hey @beckgael
Finatra & Play provide you with opionated and out-of-the-box solutions, while Finch is minimalistic & highly-customizable, but requires more investments in setting up things manually.
Essentially, it depends on you. If you wish to have an absolute control over your application, it's easier to obtain it with Finch. If you're looking for something to quickly start with, but willing to give up some of the control to the framework: roll with Play or Finatra
i.e. in Finch you decide what effect type you're going to use: IO
, Task
, ZIO
, Kleisli
or whatever
in Play & Finatra you're stuck with Scala or Twitter Future respectively. So there is that.
With Finch you can pick out of many JSON libraries to encode & decode data in your API, in Finatra I believe you have to deal with Jackson.
etc etc
toServiceAs[Text.Html]
does not necessarily require Response, but if you use another type you'll most likely need to provide your own Encode.Aux[A, Text.Html]
. to your question about the Response
's status getting overridden by the Output
's status, I would expect that. The why around it is to return the Response
directly without the output wrapping it. Finch is able to serve both raw Response
and things wrapped in Output
Hi all, I'm having some issues with CORS, it's my first time using Scala on an edge-service that communicates with a React client. Am I correct in thinking that using:
new Cors.HttpFilter(Cors.UnsafePermissivePolicy).andThen(
(Bootstrap
.serve[Application.Json](
...
.toService)
Will essentially disable CORS on the backend?
finch
in the-benchmarker/web-frameworks#3675. This a benchmarking project. We care about having consent (or at least non-opposition) before doing such a thing. Do core dev / maintainers are not opposed of add finch
in this list ?
Hi all!
Using Bootstrap.serve[Text.Html]
on a endpoint containing finagle Response
, content-type isn't added.
When added on response, it's alright, but then nothing should be specified on Bootstrap.serve
which isn't possible.
From my understanding, serve[CT](A :: B :: CNil)
compiled will look for io.finch.Encode
types Aux[A, CT]
and Aux[B, CT]
if type isn't com.twitter.finagle.http.Response
.
As it is a com.twitter.finagle.http.Response
, CT
should not be mandatory ?
What would be the right way ? I'm using scalatags, should I create an encoder for something like (Status, Option[TypedTag])
?
How to serve Response
without specifying content type as it's not use from Bootstrap
?
from one hand, I understand your point on the lack of consistency
from the other hand, we don't want to mangle with user created Response
as we assume they know what they're doing going with the low-level abstraction