-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b047b1b
commit 13e31c8
Showing
12 changed files
with
125 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.finch | ||
|
||
import com.twitter.finagle.Service | ||
import com.twitter.finagle.http.{Request, Response} | ||
import io.finch.internal.ToService | ||
import shapeless._ | ||
|
||
/** | ||
* Captures the `HList` of endpoints as well as the `HList` of their content-types in order to do | ||
* the `toService` conversion. | ||
* | ||
* {{{ | ||
* | ||
* val api: Service[Request, Response] = ServiceBuilder() | ||
* .respond[Application.Json](getUser :+: postUser) | ||
* .respond[Text.Plain](healthcheck) | ||
* .toService | ||
* }}} | ||
*/ | ||
case class ServiceBuilder[ES <: HList, CTS <: HList](endpoints: ES) { self => | ||
|
||
class Respond[CT <: String] { | ||
def apply[E](e: Endpoint[E]): ServiceBuilder[Endpoint[E] :: ES, CT :: CTS] = | ||
self.copy(e :: self.endpoints) | ||
} | ||
|
||
def respond[CT <: String]: Respond[CT] = new Respond[CT] | ||
|
||
def toService(implicit ts: ToService[ES, CTS]): Service[Request, Response] = ts(endpoints) | ||
} | ||
|
||
object ServiceBuilder { | ||
def apply(): ServiceBuilder[HNil, HNil] = ServiceBuilder(HNil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.finch.internal | ||
|
||
import com.twitter.finagle.Service | ||
import com.twitter.finagle.http.{Request, Response, Status } | ||
import com.twitter.util.Future | ||
import io.finch._ | ||
import shapeless._ | ||
|
||
import scala.annotation.implicitNotFound | ||
|
||
@implicitNotFound(""" | ||
You can only convert an endpoint into a Finagle service if its result type is one of the following: | ||
* 'com.twitter.finagle.http.Response' | ||
* a value of type 'A' with 'io.finch.Encode' instance available (for requested Content-Type) | ||
* a 'shapeless.Coproduct' made up of some combination of the above | ||
Make sure this rule evaluates for every endpoint passed to 'respond' method on 'ServiceBuilder'. | ||
""") | ||
trait ToService[ES <: HList, CTS <: HList] { | ||
def apply(endpoints: ES): Service[Request, Response] | ||
} | ||
|
||
object ToService { | ||
|
||
implicit val hnilToService: ToService[HNil, HNil] = new ToService[HNil, HNil] { | ||
def apply(endpoints: HNil): Service[Request, Response] = | ||
Service.mk(_ => Future.value(Response(Status.NotFound))) | ||
} | ||
|
||
implicit def hconsToService[A, EH <: Endpoint[A], ET <: HList, CTH <: String, CTT <: HList](implicit | ||
trH: ToResponse.Aux[Output[A], CTH], | ||
tsT: ToService[ET, CTT] | ||
): ToService[Endpoint[A] :: ET, CTH :: CTT] = new ToService[Endpoint[A] :: ET, CTH :: CTT] { | ||
def apply(endpoints: Endpoint[A] :: ET): Service[Request, Response] = new Service[Request, Response] { | ||
private[this] val basicEndpointHandler: PartialFunction[Throwable, Output[Nothing]] = { | ||
case e: io.finch.Error => Output.failure(e, Status.BadRequest) | ||
} | ||
|
||
private[this] val safeEndpoint = endpoints.head.handle(basicEndpointHandler) | ||
|
||
def apply(req: Request): Future[Response] = safeEndpoint(Input(req)) match { | ||
case Some((remainder, output)) if remainder.isEmpty => | ||
output.map(o => o.toResponse[CTH](req.version)).run | ||
case _ => tsT(endpoints.tail)(req) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters