-
Notifications
You must be signed in to change notification settings - Fork 221
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
74dbe4e
commit 761a12c
Showing
13 changed files
with
494 additions
and
357 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,64 @@ | ||
package io.finch | ||
|
||
import com.twitter.finagle.Service | ||
import com.twitter.util.Future | ||
|
||
import io.finch.route.{Endpoint => _, _} | ||
import io.finch.response._ | ||
import io.finch.request._ | ||
|
||
/** | ||
* An experimental package that enables `micro`-services support in Finch. | ||
*/ | ||
package object micro { | ||
|
||
/** | ||
* An alias for polymorphic [[PRequestReader]]. | ||
*/ | ||
type PMicro[R, A] = PRequestReader[R, A] | ||
|
||
/** | ||
* A [[PMicro]] with request type fixed to [[HttpRequest]]. | ||
*/ | ||
type Micro[A] = PMicro[HttpRequest, A] | ||
|
||
/** | ||
* A companion object for `Micro`. | ||
*/ | ||
val Micro = RequestReader | ||
|
||
/** | ||
* A [[Router]] that fetches a [[PMicro]] is called an endpoint. | ||
*/ | ||
type PEndpoint[R] = Router[PMicro[R, HttpResponse]] | ||
|
||
/** | ||
* A [[PEndpoint]] with request type fixed to [[HttpRequest]]. | ||
*/ | ||
type Endpoint = PEndpoint[HttpRequest] | ||
|
||
implicit class MicroRouterOps[R, A](r: Router[PMicro[R, A]]) { | ||
def |[B](that: Router[PMicro[R, B]])(implicit eA: EncodeResponse[A], eB: EncodeResponse[B]): PEndpoint[R] = | ||
r.map(_.map(Ok(_))) orElse that.map(_.map(Ok(_))) | ||
} | ||
|
||
implicit def microToHttpMicro[R, A](m: PMicro[R, A])( | ||
implicit e: EncodeResponse[A] | ||
): PMicro[R, HttpResponse] = m.map(Ok(_)) | ||
|
||
implicit def microRouterToEndpoint[R, M](r: Router[M])( | ||
implicit ev: M => PMicro[R, HttpResponse] | ||
): PEndpoint[R] = r.map(ev) | ||
|
||
implicit def endpointToFinagleService[M, R](r: Router[M])( | ||
implicit evM: M => PMicro[R, HttpResponse], evR: R %> HttpRequest | ||
): Service[R, HttpResponse] = new Service[R, HttpResponse] { | ||
def apply(req: R): Future[HttpResponse] = { | ||
val httpReq = evR(req) | ||
r.map(evM)(requestToRoute(httpReq)) match { | ||
case Some((Nil, micro)) => micro(req) | ||
case _ => RouteNotFound(s"${httpReq.method.toString.toUpperCase} ${httpReq.path}").toFutureException | ||
} | ||
} | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
core/src/main/scala/io/finch/request/LowPriorityRequestReaderImplicits.scala
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,37 @@ | ||
package io.finch.request | ||
|
||
import com.twitter.util.{Future, Try} | ||
|
||
import scala.reflect.ClassTag | ||
|
||
/** | ||
* Trait with low-priority implicits to avoid conflicts that would arise from adding implicits that would work with | ||
* any type in the same scope as implicits for concrete types. | ||
* | ||
* Implicits defined in super-types have lower priority than those defined in a sub-type. Therefore we define low- | ||
* priority implicits here and mix this trait into the package object. | ||
*/ | ||
trait LowPriorityRequestReaderImplicits { | ||
|
||
/** | ||
* Creates a [[io.finch.request.DecodeMagnet DecodeMagnet]] from | ||
* [[io.finch.request.DecodeAnyRequest DecodeAnyRequest]]. | ||
*/ | ||
implicit def magnetFromAnyDecode[A](implicit d: DecodeAnyRequest, tag: ClassTag[A]): DecodeMagnet[A] = | ||
new DecodeMagnet[A] { | ||
def apply(): DecodeRequest[A] = new DecodeRequest[A] { | ||
def apply(req: String): Try[A] = d(req)(tag) | ||
} | ||
} | ||
|
||
/** | ||
* Adds a `~>` and `~~>` compositors to `RequestReader` to compose it with function of one argument. | ||
*/ | ||
implicit class RrArrow1[R, A](rr: PRequestReader[R, A]) { | ||
def ~~>[B](fn: A => Future[B]): PRequestReader[R, B] = | ||
rr.embedFlatMap(fn) | ||
|
||
def ~>[B](fn: A => B): PRequestReader[R, B] = | ||
rr.map(fn) | ||
} | ||
} |
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
Oops, something went wrong.