Skip to content

Commit

Permalink
Update to Http4s 0.23.0-RC1
Browse files Browse the repository at this point in the history
  • Loading branch information
zarthross committed Jun 9, 2021
1 parent 339de5d commit bbbbd46
Show file tree
Hide file tree
Showing 21 changed files with 65 additions and 61 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/rho/CompileRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object CompileRoutes {
* @param routes `Seq` of routes to bundle into a service.
* @return An `HttpRoutes`
*/
def foldRoutes[F[_]: Defer: Monad](routes: Seq[RhoRoute.Tpe[F]]): HttpRoutes[F] = {
def foldRoutes[F[_]: Monad](routes: Seq[RhoRoute.Tpe[F]]): HttpRoutes[F] = {
val tree = routes.foldLeft(PathTree[F]())((t, r) => t.appendRoute(r))
HttpRoutes((req: Request[F]) => tree.getResult(req).toResponse)
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/rho/RhoRoutes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import shapeless.{HList, HNil}
*
* @param routes Routes to prepend before elements in the constructor.
*/
class RhoRoutes[F[_]: Defer: Monad](routes: Seq[RhoRoute[F, _ <: HList]] = Vector.empty)
class RhoRoutes[F[_]: Monad](routes: Seq[RhoRoute[F, _ <: HList]] = Vector.empty)
extends bits.MethodAliases
with bits.ResponseGeneratorInstances[F]
with RoutePrependable[F, RhoRoutes[F]]
Expand Down
7 changes: 3 additions & 4 deletions core/src/main/scala/org/http4s/rho/RoutesBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import org.http4s._
import scala.collection.compat._

/** CompileRoutes which accumulates routes and can build a `HttpRoutes` */
final class RoutesBuilder[F[_]: Defer: Monad] private (
internalRoutes: VectorBuilder[RhoRoute.Tpe[F]])
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 Down Expand Up @@ -51,10 +50,10 @@ final class RoutesBuilder[F[_]: Defer: Monad] private (
object RoutesBuilder {

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

/** Constructor method for new `RoutesBuilder` instances with existing routes */
def apply[F[_]: Defer: Monad](routes: Seq[RhoRoute.Tpe[F]]): RoutesBuilder[F] = {
def apply[F[_]: Monad](routes: Seq[RhoRoute.Tpe[F]]): RoutesBuilder[F] = {
val builder = new VectorBuilder[RhoRoute.Tpe[F]]
builder ++= routes

Expand Down
1 change: 1 addition & 0 deletions core/src/test/scala/org/http4s/rho/ApiTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.http4s.Uri.uri
import org.specs2.matcher.MatchResult
import org.specs2.mutable._
import shapeless.{HList, HNil}
import cats.effect.unsafe.implicits.global
import scala.util.control.NoStackTrace
import org.http4s.headers.Accept

Expand Down
1 change: 1 addition & 0 deletions core/src/test/scala/org/http4s/rho/AuthedContextSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object MyRoutes extends RhoRoutes[IO] {
}

class AuthedContextSpec extends Specification {
import cats.effect.unsafe.implicits.global

val routes = Auth.authenticated(MyAuth.toService(MyRoutes.toRoutes()))

Expand Down
1 change: 1 addition & 0 deletions core/src/test/scala/org/http4s/rho/CodecRouterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.specs2.mutable.Specification
import scala.collection.compat.immutable.ArraySeq

class CodecRouterSpec extends Specification {
import cats.effect.unsafe.implicits.global

def bodyAndStatus(resp: Response[IO]): (String, Status) = {
val rbody = new String(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import scala.collection.immutable.Seq

import cats.effect.IO
import org.specs2.mutable.Specification
import cats.effect.unsafe.implicits.global

class ParamDefaultValueSpec extends Specification {

Expand Down
1 change: 1 addition & 0 deletions core/src/test/scala/org/http4s/rho/RequestRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.http4s.rho
import cats.effect.IO
import org.http4s._
import org.http4s.HttpRoutes
import cats.effect.unsafe.implicits.global

/** Helper for collecting a the body from a `RhoRoutes` */
trait RequestRunner {
Expand Down
1 change: 1 addition & 0 deletions core/src/test/scala/org/http4s/rho/ResultSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.http4s.headers._
import org.http4s.rho.io._
import org.http4s.HttpDate
import org.specs2.mutable.Specification
import cats.effect.unsafe.implicits.global

class ResultSpec extends Specification {

Expand Down
1 change: 1 addition & 0 deletions core/src/test/scala/org/http4s/rho/RhoRoutesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.http4s.headers.{`Content-Length`, `Content-Type`}
import org.http4s.rho.io._
import org.http4s.Uri.uri
import org.specs2.mutable.Specification
import cats.effect.unsafe.implicits.global
import org.http4s.Uri.Path
import org.typelevel.ci.CIString
import scala.util.control.NoStackTrace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package bits

import cats.effect.IO
import org.specs2.mutable.Specification
import cats.effect.unsafe.implicits.global

class HListToFuncSpec extends Specification {
def getBody(b: EntityBody[IO]): String =
Expand Down
3 changes: 2 additions & 1 deletion core/src/test/scala/org/http4s/rho/bits/PathTreeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import java.nio.charset.StandardCharsets

import cats.effect.IO
import org.specs2.mutable.Specification
import org.http4s.Uri._
import org.http4s.Uri.uri
import org.http4s.server.middleware.TranslateUri
import org.http4s.server.Router
import cats.effect.unsafe.implicits.global

class PathTreeSpec extends Specification {
object pathTree extends PathTreeOps[IO]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.http4s.rho.io._
import org.specs2.mutable.Specification

class ResponseGeneratorSpec extends Specification {
import cats.effect.unsafe.implicits.global

"ResponseGenerator" should {
"Build a response with a body" in {
val result = Ok("Foo").unsafeRunSync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import shapeless.HList
import scala.reflect.runtime.universe._

class ResultMatcherSpec extends Specification {
import cats.effect.unsafe.implicits.global

class TRhoRoutes[F[_]] extends bits.MethodAliases {
var statuses: Set[(Status, Type)] = Set.empty
Expand Down
39 changes: 19 additions & 20 deletions examples/src/main/scala/com/http4s/rho/swagger/demo/Main.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.http4s.rho.swagger.demo

import cats.effect.{Blocker, ExitCode, IO, IOApp}
import cats.effect.{ExitCode, IO, IOApp}
import com.http4s.rho.swagger.ui.SwaggerUi
import org.http4s.implicits._
import org.http4s.rho.swagger.SwaggerMetadata
Expand All @@ -20,23 +20,22 @@ object Main extends IOApp {

logger.info(s"Starting Swagger example on '$port'")

def run(args: List[String]): IO[ExitCode] =
Blocker[IO].use { blocker =>
val metadata = SwaggerMetadata(
apiInfo = Info(title = "Rho demo", version = "1.2.3"),
tags = List(Tag(name = "hello", description = Some("These are the hello routes.")))
)

val swaggerUiRhoMiddleware =
SwaggerUi[IO].createRhoMiddleware(blocker, swaggerMetadata = metadata)
val myRoutes = new MyRoutes[IO](ioSwagger).toRoutes(swaggerUiRhoMiddleware)

BlazeServerBuilder[IO](global)
.withHttpApp(myRoutes.orNotFound)
.bindLocal(port)
.serve
.compile
.drain
.as(ExitCode.Success)
}
def run(args: List[String]): IO[ExitCode] = {
val metadata = SwaggerMetadata(
apiInfo = Info(title = "Rho demo", version = "1.2.3"),
tags = List(Tag(name = "hello", description = Some("These are the hello routes.")))
)

val swaggerUiRhoMiddleware =
SwaggerUi[IO].createRhoMiddleware(swaggerMetadata = metadata)
val myRoutes = new MyRoutes[IO](ioSwagger).toRoutes(swaggerUiRhoMiddleware)

BlazeServerBuilder[IO](global)
.withHttpApp(myRoutes.orNotFound)
.bindLocal(port)
.serve
.compile
.drain
.as(ExitCode.Success)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.http4s.rho.swagger.demo

import cats.Monad
import cats._
import cats.effect._
import cats.implicits._
import com.http4s.rho.swagger.demo.MyRoutes._
Expand All @@ -13,9 +13,10 @@ import shapeless.HNil
import org.http4s.circe.CirceEntityEncoder
import org.http4s.implicits._

class MyRoutes[F[+_]: Effect](swaggerSyntax: SwaggerSyntax[F])
class MyRoutes[F[+_]: Async](swaggerSyntax: SwaggerSyntax[F])
extends RhoRoutes[F]
with CirceEntityEncoder {

import swaggerSyntax._

val requireCookie: TypedHeader[F, HNil] = H[headers.Cookie].existsAndR { cookie =>
Expand Down Expand Up @@ -54,7 +55,7 @@ class MyRoutes[F[+_]: Effect](swaggerSyntax: SwaggerSyntax[F])
}

// Normally this would be part of the constructor since its creation is 'unsafe'
private val counterRef = cats.effect.concurrent.Ref.unsafe[F, Int](0)
private val counterRef = cats.effect.Ref.unsafe[F, Int](0)

"This uses a simple counter for the number of times this route has been requested" **
POST / "counter" |>> { () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.http4s.rho.swagger.demo

import cats.effect.{Blocker, ContextShift, IO}
import cats.effect.{IO}
import org.http4s.dsl.io._
import org.http4s.rho.RhoRoutes
import org.http4s.{HttpRoutes, Request, Response, StaticFile}
Expand All @@ -11,27 +11,26 @@ object StaticContentService {
/** Routes for getting static resources. These might be served more efficiently by apache2 or nginx,
* but its nice to keep it self contained.
*/
def routes(blocker: Blocker)(implicit cs: ContextShift[IO]): HttpRoutes[IO] = new RhoRoutes[IO] {
def routes: HttpRoutes[IO] = new RhoRoutes[IO] {
// Swagger User Interface
GET / "css" / * |>> { (req: Request[IO], _: List[String]) =>
fetchResource(swaggerUiDir + req.pathInfo, req, blocker)
fetchResource(swaggerUiDir + req.pathInfo, req)
}
GET / "images" / * |>> { (req: Request[IO], _: List[String]) =>
fetchResource(swaggerUiDir + req.pathInfo, req, blocker)
fetchResource(swaggerUiDir + req.pathInfo, req)
}
GET / "lib" / * |>> { (req: Request[IO], _: List[String]) =>
fetchResource(swaggerUiDir + req.pathInfo, req, blocker)
fetchResource(swaggerUiDir + req.pathInfo, req)
}
GET / "swagger-ui" |>> { req: Request[IO] =>
fetchResource(swaggerUiDir + "/index.html", req, blocker)
fetchResource(swaggerUiDir + "/index.html", req)
}
GET / "swagger-ui.js" |>> { req: Request[IO] =>
fetchResource(swaggerUiDir + "/swagger-ui.min.js", req, blocker)
fetchResource(swaggerUiDir + "/swagger-ui.min.js", req)
}
}.toRoutes()

private def fetchResource(path: String, req: Request[IO], blocker: Blocker)(implicit
cs: ContextShift[IO]): IO[Response[IO]] =
StaticFile.fromResource(path, blocker, Some(req)).getOrElseF(NotFound())
private def fetchResource(path: String, req: Request[IO]): IO[Response[IO]] =
StaticFile.fromResource(path, Some(req)).getOrElseF(NotFound())

}
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Keys._

// format: off
object Dependencies {
val http4sVersion = "0.22.0-RC1"
val http4sVersion = "0.23.0-RC1"
val specs2Version = "4.10.6"
val circeVersion = "0.14.1"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.http4s.rho.swagger.ui

import cats.effect.{Blocker, ContextShift, Sync}
import cats.effect.{Sync}
import org.http4s.rho.bits.PathAST.{PathMatch, TypedPath}
import org.http4s.rho.swagger.models._
import org.http4s.rho.swagger.{
Expand All @@ -18,15 +18,12 @@ import scala.collection.immutable.Seq
import scala.reflect.runtime.universe.WeakTypeTag

object SwaggerUi {
def apply[F[_]: Sync: ContextShift](implicit etag: WeakTypeTag[F[_]]): SwaggerUi[F] =
new SwaggerUi[F]()
def apply[F[_]: Sync](implicit etag: WeakTypeTag[F[_]]): SwaggerUi[F] = new SwaggerUi[F]()
}

class SwaggerUi[F[_]: Sync: ContextShift](implicit etag: WeakTypeTag[F[_]])
extends SwaggerSyntax[F] {
class SwaggerUi[F[_]: Sync](implicit etag: WeakTypeTag[F[_]]) extends SwaggerSyntax[F] {

def createRhoMiddleware(
blocker: Blocker,
swaggerFormats: SwaggerFormats = swagger.DefaultSwaggerFormats,
swaggerSpecJsonPath: String = "swagger.json",
swaggerSpecYamlPath: String = "swagger.yaml",
Expand Down Expand Up @@ -56,8 +53,7 @@ class SwaggerUi[F[_]: Sync: ContextShift](implicit etag: WeakTypeTag[F[_]])
val relativeSwaggerSpecPath =
("../" * s"$cleanSwaggerUiPath/".count(_ == '/')) + cleanSwaggerSpecPath

val swaggerUiRoutes =
SwaggerUiRoutes[F](swaggerUiPath, relativeSwaggerSpecPath, blocker).getRoutes
val swaggerUiRoutes = SwaggerUiRoutes[F](swaggerUiPath, relativeSwaggerSpecPath).getRoutes

routes ++ swaggerSpecRoute ++ swaggerUiRoutes
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.http4s.rho.swagger.ui

import cats.effect.{Blocker, ContextShift, Sync}
import cats.effect.{Sync}
import cats.implicits._
import org.http4s.headers.`Content-Type`
import org.http4s.rho.RhoRoutes
import org.http4s.rho.bits.PathAST.CaptureTail
import org.http4s.rho.swagger.ui.BuildInfo
import org.http4s._

class SwaggerUiRoutes[F[_]: Sync: ContextShift](
class SwaggerUiRoutes[F[_]: Sync](
swaggerUiPath: String,
swaggerUiResourcesPath: String,
indexHtml: String,
blocker: Blocker)
indexHtml: String)
extends RhoRoutes[F] {

private val htmlEncoder: EntityEncoder[F, String] =
Expand All @@ -35,20 +34,19 @@ class SwaggerUiRoutes[F[_]: Sync: ContextShift](
}

private def fetchResource(path: String, req: Request[F]): F[Response[F]] =
StaticFile.fromResource[F](path, blocker, Some(req)).getOrElseF(NotFound(()).map(_.resp))
StaticFile.fromResource[F](path, Some(req)).getOrElseF(NotFound(()).map(_.resp))

}

object SwaggerUiRoutes {

def apply[F[_]: Sync: ContextShift](
def apply[F[_]: Sync](
swaggerUiPath: String,
swaggerSpecRelativePath: String,
blocker: Blocker): SwaggerUiRoutes[F] = {
swaggerSpecRelativePath: String): SwaggerUiRoutes[F] = {
val swaggerUiResourcesPath =
s"/META-INF/resources/webjars/swagger-ui/${BuildInfo.swaggerUiVersion}/"
val indexHtml = defaultIndexHtml(swaggerSpecRelativePath)
new SwaggerUiRoutes[F](swaggerUiPath, swaggerUiResourcesPath, indexHtml, blocker)
new SwaggerUiRoutes[F](swaggerUiPath, swaggerUiResourcesPath, indexHtml)
}

def defaultIndexHtml(swaggerUrl: String): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package rho
package swagger

import _root_.io.swagger.util.{Json, Yaml}
import cats.effect.Sync
import cats._
import org.http4s.headers.`Content-Type`
import org.http4s.rho.bits.PathAST.{PathMatch, TypedPath}
import org.http4s.rho.swagger.models._
Expand All @@ -13,11 +13,11 @@ import scala.reflect.runtime.universe._
import scala.collection.immutable.Seq

object SwaggerSupport {
def apply[F[_]: Sync](implicit etag: WeakTypeTag[F[_]]): SwaggerSupport[F] =
def apply[F[_]: Monad](implicit etag: WeakTypeTag[F[_]]): SwaggerSupport[F] =
new SwaggerSupport[F] {}
}

abstract class SwaggerSupport[F[_]](implicit F: Sync[F], etag: WeakTypeTag[F[_]])
abstract class SwaggerSupport[F[_]: Monad](implicit etag: WeakTypeTag[F[_]])
extends SwaggerSyntax[F] {

/** Create a RhoMiddleware adding a route to get the Swagger JSON/YAML files
Expand Down

0 comments on commit bbbbd46

Please sign in to comment.