Skip to content

Commit

Permalink
Rename RhoService to RhoRoutes second pass
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy committed Oct 29, 2018
1 parent bf440e5 commit f67eed3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/org/http4s/rho/AuthedContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import org.http4s.rho.bits.{FailureResponseOps, SuccessResponse, TypedHeader}
*
* object Auth extends AuthedContext[IO, User]
*
* object BobService extends RhoRoutes[IO] {
* object BobRoutes extends RhoRoutes[IO] {
* GET +? param("foo", "bar") >>> Auth.auth |>> { (foo: String, user: User) =>
* Ok(s"Bob with id ${user.id}, foo $foo")
* }
* }
*
* val service = middleware.apply(Auth.toService(BobService.toRoutes()))
* val service = middleware.apply(Auth.toService(BobRoutes.toRoutes()))
* }}}
*
* @tparam U authInfo type for this service.
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/scala/org/http4s/rho/RhoRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class RhoRoutes[F[_]: Monad](routes: Seq[RhoRoute[F, _ <: HList]] = Vector.empty
with EntityEncoderInstances
with RhoDsl[F]
{
final private val serviceBuilder = ServiceBuilder[F](routes)
final private val routesBuilder = RoutesBuilder[F](routes)

final protected val logger = getLogger

final implicit protected def compileService: CompileRoutes[F, RhoRoute.Tpe[F]] = serviceBuilder
final implicit protected def compileRoutes: CompileRoutes[F, RhoRoute.Tpe[F]] = routesBuilder

/** Create a new [[RhoRoutes]] by appending the routes of the passed [[RhoRoutes]]
*
Expand All @@ -44,15 +44,15 @@ class RhoRoutes[F[_]: Monad](routes: Seq[RhoRoute[F, _ <: HList]] = Vector.empty
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]] = serviceBuilder.routes()
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] =
serviceBuilder.toRoutes(middleware)
routesBuilder.toRoutes(middleware)

final override def toString: String = s"RhoRoutes(${serviceBuilder.routes().toString()})"
final override def toString: String = s"RhoRoutes(${routesBuilder.routes().toString()})"

final override def /:(prefix: TypedPath[F, HNil]): RhoRoutes[F] = {
new RhoRoutes(serviceBuilder.routes().map { prefix /: _ })
new RhoRoutes(routesBuilder.routes().map { prefix /: _ })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import cats.Monad
import shapeless.HList
import org.http4s._

/** CompileService which accumulates routes and can build a `HttpRoutes` */
final class ServiceBuilder[F[_]: Monad] private(internalRoutes: VectorBuilder[RhoRoute.Tpe[F]]) extends CompileRoutes[F, RhoRoute.Tpe[F]] {
/** 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`
*
Expand All @@ -19,7 +19,7 @@ final class ServiceBuilder[F[_]: Monad] private(internalRoutes: VectorBuilder[Rh
/** Get a snapshot of the currently acquired routes */
def routes(): Seq[RhoRoute.Tpe[F]] = internalRoutes.result()

/** Append the routes into this [[ServiceBuilder]]
/** Append the routes into this [[RoutesBuilder]]
*
* @param routes Routes to accumulate.
* @return `this` instance with its internal state mutated.
Expand All @@ -29,7 +29,7 @@ final class ServiceBuilder[F[_]: Monad] private(internalRoutes: VectorBuilder[Rh
this
}

/** Accumulate the [[RhoRoute]] into this [[ServiceBuilder]]
/** Accumulate the [[RhoRoute]] into this [[RoutesBuilder]]
*
* This is the same as appending a the single route and returning the same route.
*
Expand All @@ -43,15 +43,15 @@ final class ServiceBuilder[F[_]: Monad] private(internalRoutes: VectorBuilder[Rh
}
}

object ServiceBuilder {
/** Constructor method for new `ServiceBuilder` instances */
def apply[F[_]: Monad](): ServiceBuilder[F] = apply(Seq.empty)
object RoutesBuilder {
/** Constructor method for new `RoutesBuilder` instances */
def apply[F[_]: Monad](): RoutesBuilder[F] = apply(Seq.empty)

/** Constructor method for new `ServiceBuilder` instances with existing routes */
def apply[F[_]: Monad](routes: Seq[RhoRoute.Tpe[F]]): ServiceBuilder[F] = {
/** 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 ServiceBuilder(builder)
new RoutesBuilder(builder)
}
}
8 changes: 4 additions & 4 deletions core/src/test/scala/org/http4s/rho/CompileRoutesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class CompileRoutesSpec extends Specification {

"CompileService" should {
"Build a single route" in {
val c = ServiceBuilder[IO]()
val c = RoutesBuilder[IO]()
getFoo(c)

"GetFoo" === RRunner(c.toRoutes()).checkOk(Request(uri=Uri(path="/hello")))
}

"Build multiple routes" in {
val c = ServiceBuilder[IO]()
val c = RoutesBuilder[IO]()
getFoo(c)
putFoo(c)

Expand All @@ -45,8 +45,8 @@ class CompileRoutesSpec extends Specification {
}

"Concatenate correctly" in {
val c1 = ServiceBuilder[IO](); getFoo(c1)
val c2 = ServiceBuilder[IO](); putFoo(c2)
val c1 = RoutesBuilder[IO](); getFoo(c1)
val c2 = RoutesBuilder[IO](); putFoo(c2)

val srvc = c1.append(c2.routes()).toRoutes()
"GetFoo" === RRunner(srvc).checkOk(Request(uri=Uri(path="/hello")))
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/scala/org/http4s/rho/RhoRoutesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,15 @@ class RhoRoutesSpec extends Specification with RequestRunner {
GET / "foo2" |>> "Foo2"
}
val both: RhoRoutes[IO] = routes1 and routes2
val bothService = both.toRoutes()
val bothRoutes = both.toRoutes()

both.getRoutes === routes1.getRoutes ++ routes2.getRoutes

val req1 = Request[IO](uri = uri("foo1"))
getBody(bothService(req1).value.unsafeRunSync().getOrElse(Response.notFound).body) === "Foo1"
getBody(bothRoutes(req1).value.unsafeRunSync().getOrElse(Response.notFound).body) === "Foo1"

val req2 = Request[IO](uri = uri("foo2"))
getBody(bothService(req2).value.unsafeRunSync().getOrElse(Response.notFound).body) === "Foo2"
getBody(bothRoutes(req2).value.unsafeRunSync().getOrElse(Response.notFound).body) === "Foo2"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SwaggerSupportSpec extends Specification {
import org.json4s.JsonAST._
import org.json4s.jackson._

val baseService = new RhoRoutes[IO] {
val baseRoutes = new RhoRoutes[IO] {
GET / "hello" |>> { () => Ok("hello world") }
GET / "hello"/ pathVar[String] |>> { world: String => Ok("hello " + world) }
}
Expand All @@ -25,25 +25,25 @@ class SwaggerSupportSpec extends Specification {
GET / "goodbye"/ pathVar[String] |>> { world: String => Ok("goodbye " + world) }
}

val trailingSlashService = new RhoRoutes[IO] {
val trailingSlashRoutes = new RhoRoutes[IO] {
GET / "foo" / "" |>> { () => Ok("hello world") }
}

val mixedTrailingSlashesService = new RhoRoutes[IO] {
val mixedTrailingSlashesRoutes = new RhoRoutes[IO] {
GET / "foo" / "" |>> { () => Ok("hello world") }
GET / "foo" |>> { () => Ok("hello world") }
GET / "bar" |>> { () => Ok("hello world") }
}

val metaDataService = new RhoRoutes[IO] {
val metaDataRoutes = new RhoRoutes[IO] {
"Hello" ** GET / "hello" |>> { () => Ok("hello world") }
Map("hello"->List("bye")) ^^ "Bye" ** GET / "bye" |>> { () => Ok("bye world") }
Map("bye"->List("hello")) ^^ GET / "goodbye" |>> { () => Ok("goodbye world") }
}

"SwaggerSupport" should {
"Expose an API listing" in {
val service = baseService.toRoutes(createRhoMiddleware(swaggerRoutesInSwagger = true))
val service = baseRoutes.toRoutes(createRhoMiddleware(swaggerRoutesInSwagger = true))

val r = Request[IO](GET, Uri(path = "/swagger.json"))

Expand All @@ -54,7 +54,7 @@ class SwaggerSupportSpec extends Specification {
}

"Support prefixed routes" in {
val service = ("foo" /: baseService).toRoutes(createRhoMiddleware(swaggerRoutesInSwagger = true))
val service = ("foo" /: baseRoutes).toRoutes(createRhoMiddleware(swaggerRoutesInSwagger = true))
val r = Request[IO](GET, Uri(path = "/swagger.json"))

val JObject(List((a, JObject(_)), (b, JObject(_)), (c, JObject(_)))) =
Expand All @@ -64,59 +64,59 @@ class SwaggerSupportSpec extends Specification {
}

"Provide a method to build the Swagger model for a list of routes" in {
val swaggerSpec = createSwagger()(baseService.getRoutes)
val swaggerSpec = createSwagger()(baseRoutes.getRoutes)

swaggerSpec.paths must haveSize(2)
}

"Provide a way to aggregate routes from multiple RhoRoutes" in {
val aggregateSwagger = createSwagger()(baseService.getRoutes ++ moarRoutes.getRoutes)
val aggregateSwagger = createSwagger()(baseRoutes.getRoutes ++ moarRoutes.getRoutes)
val swaggerRoutes = createSwaggerRoute(aggregateSwagger)
val httpServices = NonEmptyList.of(baseService, moarRoutes, swaggerRoutes).map(_.toRoutes())
val httpRoutess = NonEmptyList.of(baseRoutes, moarRoutes, swaggerRoutes).map(_.toRoutes())

val allthogetherService = httpServices.reduceLeft(_ combineK _)
val allthogetherRoutes = httpRoutess.reduceLeft(_ combineK _)

val r = Request[IO](GET, Uri(path = "/swagger.json"))

val JObject(List((a, JObject(_)), (b, JObject(_)), (c, JObject(_)), (d, JObject(_)))) =
parseJson(RRunner(allthogetherService).checkOk(r)) \\ "paths"
parseJson(RRunner(allthogetherRoutes).checkOk(r)) \\ "paths"

Set(a, b, c, d) should_== Set("/hello", "/hello/{string}", "/goodbye", "/goodbye/{string}")
}

"Support endpoints which end in a slash" in {
val service = trailingSlashService.toRoutes(createRhoMiddleware())
val service = trailingSlashRoutes.toRoutes(createRhoMiddleware())
val r = Request[IO](GET, Uri(path = "/swagger.json"))
val JObject(List((a, JObject(_)))) = parseJson(RRunner(service).checkOk(r)) \\ "paths"

a should_== "/foo/"
}

"Support endpoints which end in a slash being mixed with normal endpoints" in {
val service = mixedTrailingSlashesService.toRoutes(createRhoMiddleware())
val service = mixedTrailingSlashesRoutes.toRoutes(createRhoMiddleware())
val r = Request[IO](GET, Uri(path = "/swagger.json"))
val JObject(List((a, JObject(_)), (b, JObject(_)), (c, JObject(_)))) = parseJson(RRunner(service).checkOk(r)) \\ "paths"

Set(a, b, c) should_== Set("/foo/", "/foo", "/bar")
}

"Provide a way to agregate routes from multiple RhoRoutes, with mixed trailing slashes and non-trailing slashes" in {
val aggregateSwagger = createSwagger()(baseService.getRoutes ++ moarRoutes.getRoutes ++ mixedTrailingSlashesService.getRoutes)
val aggregateSwagger = createSwagger()(baseRoutes.getRoutes ++ moarRoutes.getRoutes ++ mixedTrailingSlashesRoutes.getRoutes)
val swaggerRoutes = createSwaggerRoute(aggregateSwagger)
val httpServices = NonEmptyList.of(baseService, moarRoutes, swaggerRoutes).map(_.toRoutes())
val httpRoutess = NonEmptyList.of(baseRoutes, moarRoutes, swaggerRoutes).map(_.toRoutes())

val allthogetherService = httpServices.reduceLeft(_ combineK _)
val allthogetherRoutes = httpRoutess.reduceLeft(_ combineK _)

val r = Request[IO](GET, Uri(path = "/swagger.json"))

val JObject(List((a, JObject(_)), (b, JObject(_)), (c, JObject(_)), (d, JObject(_)), (e, JObject(_)), (f, JObject(_)), (g, JObject(_)))) =
parseJson(RRunner(allthogetherService).checkOk(r)) \\ "paths"
parseJson(RRunner(allthogetherRoutes).checkOk(r)) \\ "paths"

Set(a, b, c, d, e, f, g) should_== Set("/hello", "/hello/{string}", "/goodbye", "/goodbye/{string}", "/foo/", "/foo", "/bar")
}

"Check metadata in API listing" in {
val service = metaDataService.toRoutes(createRhoMiddleware(swaggerRoutesInSwagger = true))
val service = metaDataRoutes.toRoutes(createRhoMiddleware(swaggerRoutesInSwagger = true))

val r = Request[IO](GET, Uri(path = "/swagger.json"))

Expand All @@ -133,7 +133,7 @@ class SwaggerSupportSpec extends Specification {
}

"Swagger support for complex meta data" in {
val service = baseService.toRoutes(createRhoMiddleware(
val service = baseRoutes.toRoutes(createRhoMiddleware(
apiPath = "swagger-test.json",
apiInfo = Info(
title = "Complex Meta Data API",
Expand Down Expand Up @@ -188,7 +188,7 @@ class SwaggerSupportSpec extends Specification {
}

"Check metadata in API listing" in {
val service = metaDataService.toRoutes(createRhoMiddleware(swaggerRoutesInSwagger = true))
val service = metaDataRoutes.toRoutes(createRhoMiddleware(swaggerRoutesInSwagger = true))

val r = Request[IO](GET, Uri(path = "/swagger.json"))

Expand Down

0 comments on commit f67eed3

Please sign in to comment.