-
Notifications
You must be signed in to change notification settings - Fork 220
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
49edd59
commit ee8a1cd
Showing
10 changed files
with
246 additions
and
110 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,51 @@ | ||
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 { | ||
|
||
/** | ||
* `RequestReader` is a composable microservice or just `Micro`. | ||
*/ | ||
type Micro[A] = RequestReader[A] | ||
|
||
/** | ||
* A companion object for `Micro`. | ||
*/ | ||
val Micro = RequestReader | ||
|
||
/** | ||
* | ||
*/ | ||
type Endpoint = Router[Micro[HttpResponse]] | ||
|
||
implicit class MicroRouterOps[A](r: Router[Micro[A]]) { | ||
def |[B](that: Router[Micro[B]])(implicit eA: EncodeResponse[A], eB: EncodeResponse[B]): Endpoint = | ||
r.map(_.map(Ok(_))) orElse that.map(_.map(Ok(_))) | ||
} | ||
|
||
implicit def microToHttpMicro[A](m: Micro[A])( | ||
implicit e: EncodeResponse[A] | ||
): Micro[HttpResponse] = m.map(Ok(_)) | ||
|
||
implicit def microRouterToEndpoint[M](r: Router[M])( | ||
implicit ev: M => Micro[HttpResponse] | ||
): Endpoint = r.map(ev) | ||
|
||
implicit def endpointToFinagleService[M](r: Router[M])( | ||
implicit ev: M => Micro[HttpResponse] | ||
): Service[HttpRequest, HttpResponse] = new Service[HttpRequest, HttpResponse] { | ||
def apply(req: HttpRequest): Future[HttpResponse] = r.map(ev)(requestToRoute(req)) match { | ||
case Some((Nil, micro)) => micro(req) | ||
case _ => RouteNotFound(s"${req.method.toString.toUpperCase} ${req.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[A](rr: RequestReader[A]) { | ||
def ~~>[B](fn: A => Future[B]): RequestReader[B] = | ||
rr.embedFlatMap(fn) | ||
|
||
def ~>[B](fn: A => B): RequestReader[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
12 changes: 12 additions & 0 deletions
12
core/src/main/scala/io/finch/route/LowPriorityRouterImplicits.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,12 @@ | ||
package io.finch.route | ||
|
||
trait LowPriorityRouterImplicits { | ||
|
||
/** | ||
* Add `/>` compositors to `RouterN` to compose it with function of one argument. | ||
*/ | ||
implicit class RArrow1[A](r: RouterN[A]) { | ||
def />[B](fn: A => B): RouterN[B] = r.map(fn) | ||
def |[B >: A](that: RouterN[B]): RouterN[B] = r orElse that | ||
} | ||
} |
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
Oops, something went wrong.