Skip to content

Commit

Permalink
Update Routes.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
Saturn225 authored Oct 1, 2024
1 parent f29c699 commit 1e66f64
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions zio-http/shared/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,34 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
val tree = self.tree
Handler
.fromFunctionHandler[Request] { req =>
val chunk = tree.get(req.method, req.path)
chunk.length match {
case 0 => Handler.notFound
case 1 => chunk(0)
case n => // TODO: Support precomputed fallback among all chunk elements
var acc = chunk(0)
var i = 1
while (i < n) {
val h = chunk(i)
acc = acc.catchAll { response =>
if (response.status == Status.NotFound) h
else Handler.fail(response)
}
i += 1
val chunk = tree.get(req.method, req.path)
val allowedMethods = tree.getAllMethods(req.path)
req.method match {
case Method.CUSTOM(_) =>
Handler.notImplemented
case _ =>
chunk.length match {
case 0 =>
if (allowedMethods.nonEmpty) {
val allowHeader = Header.Allow(NonEmptyChunk.fromIterableOption(allowedMethods).get)
Handler.methodNotAllowed.addHeader(allowHeader)
} else {
Handler.notFound
}
case 1 => chunk(0)
case n => // TODO: Support precomputed fallback among all chunk elements
var acc = chunk(0)
var i = 1
while (i < n) {
val h = chunk(i)
acc = acc.catchAll { response =>
if (response.status == Status.NotFound) h
else Handler.fail(response)
}
i += 1
}
acc
}
acc
}
}
.merge
Expand All @@ -287,6 +299,7 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
}
_tree.asInstanceOf[Routes.Tree[Env]]
}

}

object Routes extends RoutesCompanionVersionSpecific {
Expand Down Expand Up @@ -344,6 +357,9 @@ object Routes extends RoutesCompanionVersionSpecific {
empty @@ Middleware.serveResources(path, resourcePrefix)

private[http] final case class Tree[-Env](tree: RoutePattern.Tree[RequestHandler[Env, Response]]) { self =>

def getAllMethods(path: Path): Set[Method] = tree.getAllMethods(path)

final def ++[Env1 <: Env](that: Tree[Env1]): Tree[Env1] =
Tree(self.tree ++ that.tree)

Expand All @@ -357,7 +373,7 @@ object Routes extends RoutesCompanionVersionSpecific {
final def get(method: Method, path: Path): Chunk[RequestHandler[Env, Response]] =
tree.get(method, path)
}
private[http] object Tree {
private[http] object Tree {
val empty: Tree[Any] = Tree(RoutePattern.Tree.empty)

def fromRoutes[Env](routes: Chunk[zio.http.Route[Env, Response]])(implicit trace: Trace): Tree[Env] =
Expand Down

0 comments on commit 1e66f64

Please sign in to comment.