-
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
49edd59
commit cfd7922
Showing
8 changed files
with
206 additions
and
77 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,61 @@ | ||
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`. | ||
*/ | ||
type Micro[A] = RequestReader[A] | ||
|
||
/** | ||
* Codename for new-style endpoint. | ||
*/ | ||
type Endpoint[A] = Router[Micro[A]] | ||
|
||
/** | ||
* An HTTP endpoints. | ||
*/ | ||
type Endpoints = Seq[Endpoint[HttpResponse]] | ||
|
||
implicit def endpointsToHttpService(es: Endpoints): Service[HttpRequest, HttpResponse] = | ||
anyRouterToHttpService(es.reduce(_ | _)) | ||
|
||
implicit def anyToHttp[A](a: A)(implicit e: EncodeResponse[A]): HttpResponse = Ok(a) | ||
|
||
implicit def anyMicroToHttpMicro[A]( | ||
m: Micro[A] | ||
)(implicit ev: A => HttpResponse): Micro[HttpResponse] = m.map(ev) | ||
|
||
implicit def anyMicroToHttpService[A]( | ||
m: Micro[A] | ||
)(implicit ev: A => HttpResponse): Service[HttpRequest, HttpResponse] = new Service[HttpRequest, HttpResponse] { | ||
def apply(req: HttpRequest): Future[HttpResponse] = m.map(ev)(req) | ||
} | ||
|
||
implicit def anyRouterToHttpRouter[A]( | ||
r: RouterN[A] | ||
)(implicit ev: A => Micro[HttpResponse]): RouterN[Micro[HttpResponse]] = r.map(ev) | ||
|
||
/** | ||
* Implicitly converts new-style endpoint into `Service`. | ||
*/ | ||
implicit def anyRouterToHttpService[A]( | ||
e: RouterN[A] | ||
)(implicit ev: A => Micro[HttpResponse]): Service[HttpRequest, HttpResponse] = | ||
new Service[HttpRequest, HttpResponse] { | ||
def apply(req: HttpRequest): Future[HttpResponse] = e.map(ev)(requestToRoute(req)) match { | ||
case Some((Nil, rr)) => rr(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
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