Meet Bootstrap
The intention of this early 0.16 release (hence M1) is to allow people to experiment with the new Bootstrap
abstraction that supposed to allow serving multiple content-types out of a single Service
(i.e., coproduct endpoint). This was one of the most-wanted features since the type-level content type was shipped, over a year ago.
See the following tickets for more context/history on the subject:
- Content-Type as a Type #541
- Supporting multiple Content-Types in a single endpoint/service #575
- ServiceBulder (abandoned) #583
- Bootstrap (merged) #794
In a nutshell, the new Bootstrap
abstraction is pretty much an evolution of ServiceBuilder
(see #583) that also has a notion of a configuration params for derived services (i.e., Service[Request, Response]
). At this point, we only support two options: includeServerHeader
(enabled by default) and includeDateHeader
(enabled by default), but one can easily imagine this machinery to be scaled to support a wide spectrum of use cases:
The quick start example looks pretty straightforward:
import io.finch._
import io.finch.circe._
import io.finch.generic._
import com.twitter.finagle.Http
scala> val json = get("json") { Ok(Map("foo" -> "bar")) }
json: io.finch.Endpoint[scala.collection.immutable.Map[String,String]] = GET /json
scala> val text = get("text") { Ok("Hello, World!") }
text: io.finch.Endpoint[String] = GET /text
scala> val s = Bootstrap.configure(includeServerHeader = false)
.serve[Application.Json](json)
.serve[Text.Plain](text)
.toService
s: com.twitter.finagle.Service[com.twitter.finagle.http.Request,com.twitter.finagle.http.Response] = <function1>
scala> Http.server.serve(":8081", s)
Jun 15, 2017 5:35:01 PM com.twitter.finagle.Init$$anonfun$5 apply$mcV$sp
INFO: Finagle version 6.45.0 (rev=fadc80cdd804f2885ebc213964542d5568a4f485) built at 20170609-103047
res1: com.twitter.finagle.ListeningServer = com.twitter.finagle.server.ListeningStackServer$$anon$1@1da24919
And then using HTTPie:
$ http :8081/text
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Fri, 16 Jun 2017 00:35:57 GMT
Hello, World!
$ http :8081/json
HTTP/1.1 200 OK
Content-Type: application/json
Date: Fri, 16 Jun 2017 00:36:01 GMT
{
"foo": "bar"
}
If you need more examples, here is how Finch TechEmpower benchmark is evolved after this release: TechEmpower/FrameworkBenchmarks#2869