Skip to content

Commit

Permalink
chore: More safe request handler partial function (#1975)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanandren authored Sep 17, 2024
1 parent 7473bf8 commit 7c050aa
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions codegen/src/main/twirl/templates/ScalaServer/Handler.scala.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,19 @@ object @{serviceName}Handler {
.recoverWith(GrpcExceptionHandler.from(eHandler(system.classicSystem))(system, writer))
).getOrElse(unsupportedMediaType)

Function.unlift((req: model.HttpRequest) => req.uri.path match {
case model.Uri.Path.Slash(model.Uri.Path.Segment(`prefix`, model.Uri.Path.Slash(model.Uri.Path.Segment(method, model.Uri.Path.Empty)))) =>
Some(handle(spi.onRequest(prefix, method, req), method))
case _ =>
None
})
def isThisService(path: model.Uri.Path): Boolean =
path match {
case model.Uri.Path.Slash(model.Uri.Path.Segment(`prefix`, model.Uri.Path.Slash(_))) => true
case _ => false
}

{
case request if isThisService(request.uri.path) =>
request.uri.path.tail.tail match {
case model.Uri.Path.Slash(model.Uri.Path.Segment(method, model.Uri.Path.Empty)) => handle(spi.onRequest(prefix, method, request), method)
case _ => scala.concurrent.Future.failed(new akka.grpc.GrpcServiceException(io.grpc.Status.INVALID_ARGUMENT.withDescription("Invalid gRPC request path [${request.uri.path}]")))
}
}
}
}
}

0 comments on commit 7c050aa

Please sign in to comment.