forked from akka/akka-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: first try at providing inspectable routes with minimal changes
- Loading branch information
Showing
8 changed files
with
181 additions
and
36 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
akka-http-tests/src/test/scala/akka/http/scaladsl/server/InspectableRouteSpec.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,43 @@ | ||
package akka.http.scaladsl.server | ||
|
||
import akka.http.impl.util.AkkaSpecWithMaterializer | ||
import Directives._ | ||
import DirectiveRoute.addByNameNullaryApply | ||
import DynamicDirective._ | ||
|
||
class InspectableRouteSpec extends AkkaSpecWithMaterializer { | ||
"Routes" should { | ||
"be inspectable" should { | ||
"routes from method directives" in { | ||
val route = get { post { complete("ok") } } | ||
route shouldBe an[InspectableRoute] | ||
println(route) | ||
} | ||
"route alternatives with ~" in { | ||
val route = get { complete("ok") } ~ post { complete("ok") } | ||
route shouldBe an[InspectableRoute] | ||
println(route) | ||
} | ||
"route alternatives with concat" in { | ||
val route = | ||
concat( | ||
get { complete("ok") }, | ||
post { complete("ok") } | ||
) | ||
route shouldBe an[InspectableRoute] | ||
} | ||
"even for routes with extractions" in { | ||
val route = | ||
parameters("name").static { name => | ||
get { | ||
dynamic { implicit exC => | ||
complete(s"Hello ${name: String}") | ||
} | ||
} | ||
} | ||
route shouldBe an[InspectableRoute] | ||
println(route) | ||
} | ||
} | ||
} | ||
} |
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
93 changes: 93 additions & 0 deletions
93
akka-http/src/main/scala/akka/http/scaladsl/server/InspectableRoute.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,93 @@ | ||
package akka.http.scaladsl.server | ||
|
||
import akka.http.scaladsl.util.FastFuture | ||
import FastFuture._ | ||
|
||
import scala.concurrent.Future | ||
import scala.runtime.ScalaRunTime | ||
|
||
sealed trait InspectableRoute extends Route with Product { | ||
def name: String | ||
def children: Seq[Route] | ||
|
||
// FIXME: only there to help intellij | ||
def apply(ctx: RequestContext): Future[RouteResult] | ||
|
||
override def toString(): String = ScalaRunTime._toString(this) | ||
} | ||
final case class AlternativeRoutes(alternatives: Seq[Route]) extends InspectableRoute { | ||
def name: String = "concat" | ||
def children: Seq[Route] = alternatives | ||
|
||
def apply(ctx: RequestContext): Future[RouteResult] = { | ||
import ctx.executionContext | ||
def tryNext(remaining: List[Route], rejections: Vector[Rejection]): Future[RouteResult] = remaining match { | ||
case head :: tail => | ||
head(ctx).fast.flatMap { | ||
case x: RouteResult.Complete => FastFuture.successful(x) | ||
case RouteResult.Rejected(newRejections) => tryNext(tail, rejections ++ newRejections) | ||
} | ||
case Nil => FastFuture.successful(RouteResult.Rejected(rejections)) | ||
} | ||
tryNext(alternatives.toList, Vector.empty) | ||
} | ||
} | ||
sealed trait DirectiveRoute extends InspectableRoute { | ||
def implementation: Route | ||
def directiveName: String | ||
def child: Route | ||
|
||
def apply(ctx: RequestContext): Future[RouteResult] = implementation(ctx) | ||
def children: Seq[Route] = child :: Nil | ||
def name: String = s"Directive($directiveName)" | ||
} | ||
|
||
object DirectiveRoute { | ||
def wrap(implementation: Route, child: Route, directiveName: String): DirectiveRoute = implementation match { | ||
case i: Impl => | ||
i.copy(child = child, directiveName = directiveName) | ||
case x => Impl(x, child, directiveName) | ||
} | ||
|
||
implicit def addByNameNullaryApply(directive: Directive0): Route => Route = | ||
inner => { | ||
val impl = directive.tapply(_ => inner) | ||
wrap(impl, inner, directive.metaInformation.fold("<unknown>")(_.name)) | ||
} | ||
|
||
private final case class Impl( | ||
implementation: Route, | ||
child: Route, | ||
directiveName: String) extends DirectiveRoute | ||
} | ||
|
||
sealed trait ExtractionToken[+T] | ||
object ExtractionToken { | ||
// syntax sugar | ||
implicit def autoExtract[T](token: ExtractionToken[T])(implicit ctx: ExtractionContext): T = ctx.extract(token) | ||
} | ||
sealed trait ExtractionContext { | ||
def extract[T](token: ExtractionToken[T]): T | ||
} | ||
object DynamicDirective { | ||
import Directives._ | ||
def dynamic: Directive1[ExtractionContext] = extractRequestContext.flatMap { ctx => | ||
provide { | ||
new ExtractionContext { | ||
override def extract[T](token: ExtractionToken[T]): T = ctx.tokenValue(token) | ||
} | ||
} | ||
} | ||
|
||
implicit class AddStatic[T](d: Directive1[T]) { | ||
def static: Directive1[ExtractionToken[T]] = | ||
Directive { innerCons => | ||
val tok = new ExtractionToken[T] {} | ||
val inner = innerCons(Tuple1(tok)) | ||
val real = d { t => ctx => | ||
inner(ctx.addTokenValue(tok, t)) | ||
} | ||
DirectiveRoute.wrap(real, inner, d.metaInformation.fold("<unknown>")(_.name)) | ||
} | ||
} | ||
} |
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