-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #253 from chuwy/0.19
0.19
- Loading branch information
Showing
40 changed files
with
522 additions
and
514 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
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,58 @@ | ||
package org.http4s | ||
package rho | ||
|
||
import cats.Monad | ||
import org.http4s.rho.bits.PathAST.TypedPath | ||
import org.log4s.getLogger | ||
import shapeless.{HList, HNil} | ||
|
||
/** Constructor class for defining routes | ||
* | ||
* The [[RhoRoutes]] provides a convenient way to define routes in a style | ||
* similar to scalatra etc by providing implicit conversions and an implicit | ||
* [[CompileRoutes]] inside the constructor. | ||
* | ||
* {{{ | ||
* new RhoRoutes[IO] { | ||
* POST / "foo" / pathVar[Int] +? param[String]("param") |>> { (p1: Int, param: String) => | ||
* Ok("success") | ||
* } | ||
* } | ||
* }}} | ||
* | ||
* @param routes Routes to prepend before elements in the constructor. | ||
*/ | ||
class RhoRoutes[F[_]: Monad](routes: Seq[RhoRoute[F, _ <: HList]] = Vector.empty) | ||
extends bits.MethodAliases | ||
with bits.ResponseGeneratorInstances[F] | ||
with RoutePrependable[F, RhoRoutes[F]] | ||
with EntityEncoderInstances | ||
with RhoDsl[F] | ||
{ | ||
final private val routesBuilder = RoutesBuilder[F](routes) | ||
|
||
final protected val logger = getLogger | ||
|
||
final implicit protected def compileRoutes: CompileRoutes[F, RhoRoute.Tpe[F]] = routesBuilder | ||
|
||
/** Create a new [[RhoRoutes]] by appending the routes of the passed [[RhoRoutes]] | ||
* | ||
* @param other [[RhoRoutes]] whos routes are to be appended. | ||
* @return A new [[RhoRoutes]] that contains the routes of the other service appended | ||
* the the routes contained in this service. | ||
*/ | ||
final def and(other: RhoRoutes[F]): RhoRoutes[F] = new RhoRoutes(this.getRoutes ++ other.getRoutes) | ||
|
||
/** Get a snapshot of the collection of [[RhoRoute]]'s accumulated so far */ | ||
final def getRoutes: Seq[RhoRoute[F, _ <: HList]] = routesBuilder.routes() | ||
|
||
/** Convert the [[RhoRoute]]'s accumulated into a `HttpRoutes` */ | ||
final def toRoutes(middleware: RhoMiddleware[F] = identity): HttpRoutes[F] = | ||
routesBuilder.toRoutes(middleware) | ||
|
||
final override def toString: String = s"RhoRoutes(${routesBuilder.routes().toString()})" | ||
|
||
final override def /:(prefix: TypedPath[F, HNil]): RhoRoutes[F] = { | ||
new RhoRoutes(routesBuilder.routes().map { prefix /: _ }) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,57 @@ | ||
package org.http4s.rho | ||
|
||
import scala.collection.immutable.VectorBuilder | ||
import cats.Monad | ||
import shapeless.HList | ||
import org.http4s._ | ||
|
||
/** CompileRoutes which accumulates routes and can build a `HttpRoutes` */ | ||
final class RoutesBuilder[F[_]: Monad] private(internalRoutes: VectorBuilder[RhoRoute.Tpe[F]]) extends CompileRoutes[F, RhoRoute.Tpe[F]] { | ||
|
||
/** Turn the accumulated routes into an `HttpRoutes` | ||
* | ||
* @param middleware [[RhoMiddleware]] to apply to the collection of routes. | ||
* @return An `HttpRoutes` which can be mounted by http4s servers. | ||
*/ | ||
def toRoutes(middleware: RhoMiddleware[F] = identity): HttpRoutes[F] = | ||
CompileRoutes.foldRoutes(middleware.apply(internalRoutes.result())) | ||
|
||
/** Get a snapshot of the currently acquired routes */ | ||
def routes(): Seq[RhoRoute.Tpe[F]] = internalRoutes.result() | ||
|
||
/** Append the routes into this [[RoutesBuilder]] | ||
* | ||
* @param routes Routes to accumulate. | ||
* @return `this` instance with its internal state mutated. | ||
*/ | ||
def append(routes: TraversableOnce[RhoRoute.Tpe[F]]): this.type = { | ||
internalRoutes ++= routes | ||
this | ||
} | ||
|
||
/** Accumulate the [[RhoRoute]] into this [[RoutesBuilder]] | ||
* | ||
* This is the same as appending a the single route and returning the same route. | ||
* | ||
* @param route [[RhoRoute]] to compile. | ||
* @tparam T `HList` representation of the result of the route | ||
* @return The [[RhoRoute]] passed to the method. | ||
*/ | ||
override def compile[T <: HList](route: RhoRoute[F, T]): RhoRoute[F, T] = { | ||
internalRoutes += route | ||
route | ||
} | ||
} | ||
|
||
object RoutesBuilder { | ||
/** Constructor method for new `RoutesBuilder` instances */ | ||
def apply[F[_]: Monad](): RoutesBuilder[F] = apply(Seq.empty) | ||
|
||
/** Constructor method for new `RoutesBuilder` instances with existing routes */ | ||
def apply[F[_]: Monad](routes: Seq[RhoRoute.Tpe[F]]): RoutesBuilder[F] = { | ||
val builder = new VectorBuilder[RhoRoute.Tpe[F]] | ||
builder ++= routes | ||
|
||
new RoutesBuilder(builder) | ||
} | ||
} |
Oops, something went wrong.