Skip to content

Commit

Permalink
Include bound path segments in request
Browse files Browse the repository at this point in the history
This makes it possible to access raw path parameters in a decorator.
  • Loading branch information
jodersky committed Nov 3, 2024
1 parent 3200adf commit 0d1c8d2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
7 changes: 3 additions & 4 deletions cask/src/cask/main/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,17 @@ object Main{
.toList

dispatchTrie.lookup(decodedSegments, Vector()) match {
case None => Main.writeResponse(exchange, handleNotFound(Request(exchange, decodedSegments)))
case None => Main.writeResponse(exchange, handleNotFound(Request(exchange, decodedSegments, Map())))
case Some((methodMap, routeBindings, remaining)) =>
methodMap.get(effectiveMethod) match {
case None => Main.writeResponse(exchange, handleMethodNotAllowed(Request(exchange, remaining)))
case None => Main.writeResponse(exchange, handleMethodNotAllowed(Request(exchange, remaining, routeBindings)))
case Some((routes, metadata)) =>
val req = Request(exchange, remaining)
val req = Request(exchange, remaining, routeBindings)
Decorator.invoke(
req,
metadata.endpoint,
metadata.entryPoint.asInstanceOf[EntryPoint[Routes, _]],
routes,
routeBindings,
(mainDecorators ++ routes.decorators ++ metadata.decorators).toList,
Nil,
Nil
Expand Down
2 changes: 1 addition & 1 deletion cask/src/cask/model/Params.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.undertow.server.handlers.CookieImpl
case class QueryParams(value: Map[String, collection.Seq[String]])
case class RemainingPathSegments(value: Seq[String])

case class Request(exchange: HttpServerExchange, remainingPathSegments: Seq[String])
case class Request(exchange: HttpServerExchange, remainingPathSegments: Seq[String], boundPathSegments: Map[String, String])
extends geny.ByteData with geny.Readable {
import collection.JavaConverters._
lazy val cookies: Map[String, Cookie] = {
Expand Down
5 changes: 2 additions & 3 deletions cask/src/cask/router/Decorators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,21 @@ object Decorator{
endpoint: Endpoint[_, _, _, _],
entryPoint: EntryPoint[T, _],
routes: T,
routeBindings: Map[String, String],
remainingDecorators: List[Decorator[_, _, _, _]],
inputContexts: List[Any],
bindings: List[Map[String, Any]]): Result[Any] = try {
remainingDecorators match {
case head :: rest =>
head.asInstanceOf[Decorator[Any, Any, Any, Any]].wrapFunction(
ctx,
(ictx, args) => invoke(ctx, endpoint, entryPoint, routes, routeBindings, rest, ictx :: inputContexts, args :: bindings)
(ictx, args) => invoke(ctx, endpoint, entryPoint, routes, rest, ictx :: inputContexts, args :: bindings)
.asInstanceOf[Result[Nothing]]
)

case Nil =>
endpoint.wrapFunction(ctx, { (ictx: Any, endpointBindings: Map[String, Any]) =>

val mergedEndpointBindings = endpointBindings ++ routeBindings.mapValues(endpoint.wrapPathSegment)
val mergedEndpointBindings = endpointBindings ++ ctx.boundPathSegments.mapValues(endpoint.wrapPathSegment)
val finalBindings = mergedEndpointBindings :: bindings

entryPoint
Expand Down

0 comments on commit 0d1c8d2

Please sign in to comment.