-
Notifications
You must be signed in to change notification settings - Fork 22
gRPC directives for pekko-http #615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
timw
wants to merge
2
commits into
apache:main
Choose a base branch
from
indexity-io:grpc-directives
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
77 changes: 77 additions & 0 deletions
77
runtime/src/main/scala/org/apache/pekko/grpc/javadsl/GrpcDirectives.scala
This file contains hidden or 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,77 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.pekko.grpc.javadsl | ||
|
|
||
| import org.apache.pekko | ||
| import pekko.http.javadsl.server.Route | ||
|
|
||
| import java.util.function.Supplier | ||
|
|
||
| /** | ||
| * Provides directives to support serving of gRPC services. | ||
| */ | ||
| object GrpcDirectives { | ||
|
|
||
| import pekko.grpc.scaladsl.{ GrpcDirectives => G } | ||
| import pekko.http.javadsl.server.directives.RouteAdapter | ||
|
|
||
| /** | ||
| * Wraps the inner route, passing only standard gRPC (i.e. not grpc-web) requests. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| def grpc(inner: Supplier[Route]): Route = | ||
| RouteAdapter { | ||
| G.grpc { | ||
| inner.get() match { | ||
| case ra: RouteAdapter => ra.delegate | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Wraps the inner route, passing only gRPC-Web requests. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| def grpcWeb(inner: Supplier[Route]): Route = | ||
| RouteAdapter { | ||
| G.grpcWeb { | ||
| inner.get() match { | ||
| case ra: RouteAdapter => ra.delegate | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Wraps the inner route, passing requests for all gRPC protocols. | ||
| * | ||
| * Unlike a combined grpc | grpcWeb directive, this will provide a single rejection specifying all supported protocols. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| def grpcAll(inner: Supplier[Route]): Route = | ||
| RouteAdapter { | ||
| G.grpcAll { | ||
| inner.get() match { | ||
| case ra: RouteAdapter => ra.delegate | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
74 changes: 74 additions & 0 deletions
74
runtime/src/main/scala/org/apache/pekko/grpc/scaladsl/GrpcDirectives.scala
This file contains hidden or 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,74 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.pekko.grpc.scaladsl | ||
|
|
||
| import org.apache.pekko | ||
| import pekko.http.scaladsl.server._ | ||
|
|
||
| /** | ||
| * Provides directives to support serving of gRPC services. | ||
| */ | ||
| object GrpcDirectives { | ||
| import Directives._ | ||
| import pekko.grpc.GrpcProtocol | ||
| import pekko.grpc.internal.{ GrpcProtocolNative, GrpcProtocolWeb, GrpcProtocolWebText } | ||
| import pekko.http.scaladsl.model.{ ContentTypeRange, MediaType } | ||
|
|
||
| /** | ||
| * Wraps the inner route, passing only standard gRPC (i.e. not grpc-web) requests. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| def grpc: Directive0 = grpc(GrpcProtocolNative) | ||
|
|
||
| /** | ||
| * Wraps the inner route, passing only gRPC-Web requests. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| def grpcWeb: Directive0 = grpc(GrpcProtocolWeb, GrpcProtocolWebText) | ||
|
|
||
| /** | ||
| * Wraps the inner route, passing requests for all gRPC protocols. | ||
| * | ||
| * Unlike a combined grpc | grpcWeb directive, this will provide a single rejection specifying all supported protocols. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| def grpcAll: Directive0 = grpc(GrpcProtocolNative, GrpcProtocolWeb, GrpcProtocolWebText) | ||
|
|
||
| /** | ||
| * Wraps the inner route, passing requests only for a specific set of Grpc protocols. | ||
| * @param protocols the protocols to accept and pass to the inner route. | ||
| */ | ||
| private def grpc(protocols: GrpcProtocol*): Directive0 = { | ||
| val acceptedMediaTypes = protocols.flatMap(_.mediaTypes).map(_.asInstanceOf[MediaType]).toSet | ||
| extractRequest.flatMap { request => | ||
| if (acceptedMediaTypes.contains(request.entity.contentType.mediaType)) | ||
| pass | ||
| else | ||
| reject( | ||
| UnsupportedRequestContentTypeRejection( | ||
| acceptedMediaTypes.map(mt => ContentTypeRange(mt)), | ||
| Some(request.entity.contentType) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| } |
134 changes: 134 additions & 0 deletions
134
runtime/src/test/scala/org/apache/pekko/grpc/scaladsl/GrpcDirectivesSpec.scala
This file contains hidden or 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,134 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.pekko.grpc.scaladsl | ||
|
|
||
| import org.apache.pekko | ||
| import org.scalatest.Inside.inside | ||
| import org.scalatest.Inspectors | ||
| import org.scalatest.matchers.should.Matchers | ||
| import org.scalatest.wordspec.AnyWordSpec | ||
| import pekko.grpc.GrpcProtocol | ||
| import pekko.grpc.internal.{ GrpcProtocolNative, GrpcProtocolWeb, GrpcProtocolWebText } | ||
| import pekko.http.scaladsl.model._ | ||
| import pekko.http.scaladsl.server.{ Directives, Route, UnsupportedRequestContentTypeRejection } | ||
| import pekko.http.scaladsl.testkit.ScalatestRouteTest | ||
|
|
||
| class GrpcDirectivesSpec extends AnyWordSpec with Matchers with Inspectors with Directives with ScalatestRouteTest { | ||
| import pekko.grpc.scaladsl.GrpcDirectives._ | ||
|
|
||
| private val actual = "actual" | ||
| private val exampleStatus = StatusCodes.Created | ||
|
|
||
| private val requestContent = Array[Byte]() | ||
|
|
||
| private def protocolRequests(grpcProtocol: GrpcProtocol*): Seq[HttpRequest] = | ||
| grpcProtocol.flatMap(_.mediaTypes).map { mt => | ||
| Post("/service", | ||
| HttpEntity( | ||
| ContentType(mt.asInstanceOf[MediaType.Binary]), | ||
| requestContent | ||
| )) | ||
| } | ||
|
|
||
| private def validRequest(route: Route)(request: HttpRequest): Unit = { | ||
| request ~> route ~> check { | ||
| responseAs[String] shouldBe actual | ||
| response.status shouldBe exampleStatus | ||
| } | ||
| } | ||
|
|
||
| private def invalidRequest(route: Route, acceptedProtocols: GrpcProtocol*)(request: HttpRequest): Unit = { | ||
| val expectedContentTypes = acceptedProtocols.flatMap(_.mediaTypes).map(_.asInstanceOf[MediaType.Binary]).map(mt => | ||
| ContentTypeRange(mt)).toSet | ||
| request ~> route ~> check { | ||
| inside(rejections) { | ||
| case UnsupportedRequestContentTypeRejection(contentTypeRanges) +: _ => contentTypeRanges shouldBe | ||
| expectedContentTypes | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private val nonGrpcRequests = Seq( | ||
| Get("/healthz"), | ||
| Post("/service", | ||
| HttpEntity( | ||
| ContentType(MediaType.applicationBinary("grpc-not", MediaType.Compressible)), | ||
| requestContent | ||
| )), | ||
| Post("/service", | ||
| HttpEntity( | ||
| ContentType(MediaTypes.`application/json`), | ||
| requestContent | ||
| )) | ||
| ) | ||
|
|
||
| "The grpc directive" should { | ||
| val route = grpc { | ||
| complete(HttpResponse(exampleStatus, Nil, HttpEntity(actual))) | ||
| } | ||
| "pass only grpc native protocol" in { | ||
| forAll(protocolRequests(GrpcProtocolNative))(validRequest(route)) | ||
| } | ||
|
|
||
| "not pass non-grpc native protocols" in { | ||
| forAll(nonGrpcRequests ++ protocolRequests(GrpcProtocolWeb, GrpcProtocolWebText))(invalidRequest(route, | ||
| GrpcProtocolNative)) | ||
| } | ||
| } | ||
|
|
||
| "The grpcWeb directive" should { | ||
| val route = grpcWeb { | ||
| complete(HttpResponse(exampleStatus, Nil, HttpEntity(actual))) | ||
| } | ||
| "pass all matching grpc-web protocols" in { | ||
| forAll(protocolRequests(GrpcProtocolWeb, GrpcProtocolWebText))(validRequest(route)) | ||
| } | ||
|
|
||
| "not pass non-grpc and non-matching grpc protocols" in { | ||
| forAll(nonGrpcRequests ++ protocolRequests(GrpcProtocolNative))(invalidRequest(route, GrpcProtocolWeb, | ||
| GrpcProtocolWebText)) | ||
| } | ||
| } | ||
|
|
||
| "The grpcAll directive" should { | ||
| val route = grpcAll { | ||
| complete(HttpResponse(exampleStatus, Nil, HttpEntity(actual))) | ||
| } | ||
| "pass all grpc protocols" in { | ||
| forAll(protocolRequests(GrpcProtocolNative, GrpcProtocolWeb, GrpcProtocolWebText))(validRequest(route)) | ||
| } | ||
|
|
||
| "not pass non-grpc and non-matching grpc protocols" in { | ||
| forAll(nonGrpcRequests)(invalidRequest(route, GrpcProtocolNative, GrpcProtocolWeb, GrpcProtocolWebText)) | ||
| } | ||
| } | ||
|
|
||
| "Combined grpc | grpcWeb directive" should { | ||
| val route = (grpc | grpcWeb) { | ||
| complete(HttpResponse(exampleStatus, Nil, HttpEntity(actual))) | ||
| } | ||
| "pass all grpc protocols" in { | ||
| forAll(protocolRequests(GrpcProtocolNative, GrpcProtocolWeb, GrpcProtocolWebText))(validRequest(route)) | ||
| } | ||
|
|
||
| "not pass non-grpc and non-matching grpc protocols" in { | ||
| forAll(nonGrpcRequests)(invalidRequest(route, GrpcProtocolNative)) | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move keep the pekko imports together?