From 183fa808cce50ce206914a7c90d49c1e966fc9f7 Mon Sep 17 00:00:00 2001 From: Matthew de Detrich Date: Sat, 4 Mar 2023 16:15:23 +0100 Subject: [PATCH] Undo removal of pekko-http-cors --- build.sbt | 1 + docs/src/main/paradox/server/grpc-web.md | 6 ++--- plugin-tester-java/build.gradle | 4 +-- plugin-tester-java/pom.xml | 14 ++++++---- plugin-tester-scala/build.gradle | 4 +-- plugin-tester-scala/pom.xml | 14 ++++++---- project/Dependencies.scala | 6 +++-- .../pekko/grpc/javadsl/WebHandler.scala | 14 +++++----- .../pekko/grpc/scaladsl/WebHandler.scala | 26 +++++++++---------- 9 files changed, 50 insertions(+), 39 deletions(-) diff --git a/build.sbt b/build.sbt index 76b061d1..7078d752 100644 --- a/build.sbt +++ b/build.sbt @@ -16,6 +16,7 @@ lazy val mkBatAssemblyTask = taskKey[File]("Create a Windows bat assembly") (ThisBuild / dynverSeparator) := "-" ThisBuild / resolvers += "Apache Snapshots".at("https://repository.apache.org/content/repositories/snapshots/") +ThisBuild / resolvers ++= Resolver.sonatypeOssRepos("snapshot") // Remove when proper release of pekko-http-cors is made val pekkoGrpcCodegenId = s"$pekkoPrefix-codegen" lazy val codegen = Project(id = "codegen", base = file("codegen")) diff --git a/docs/src/main/paradox/server/grpc-web.md b/docs/src/main/paradox/server/grpc-web.md index 1118fb1d..94f1050c 100644 --- a/docs/src/main/paradox/server/grpc-web.md +++ b/docs/src/main/paradox/server/grpc-web.md @@ -34,12 +34,12 @@ native gRPC protocol on a different port than gRPC-Web, as the two protocols will likely require a different security story. You can use @apidoc[WebHandler.grpcWebHandler](WebHandler$) to serve your gRPC-Web endpoint with basic CORS infrastructure in place. To use CORS, -you will need to add the akka-http-cors dependency to your project: +you will need to add the pekko-http-cors dependency to your project: @@dependency[sbt,Maven,Gradle] { group="ch.megard" - artifact="akka-http-cors_2.12" - version="0.4.2" + artifact="pekko-http-cors_2.12" + version="0.0.0-SNAPSHOT" // Update when proper release of pekko-http-cors is made } And then serve the handlers with @apidoc[WebHandler.grpcWebHandler](WebHandler$) like this: diff --git a/plugin-tester-java/build.gradle b/plugin-tester-java/build.gradle index c8a36689..71fb0fcc 100644 --- a/plugin-tester-java/build.gradle +++ b/plugin-tester-java/build.gradle @@ -13,6 +13,7 @@ pekkoGrpc { repositories { mavenLocal() + maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } // Remove when proper release of pekko-http-cors is made } def scalaFullVersion = "2.12.16" @@ -20,8 +21,7 @@ def scalaVersion = org.gradle.util.VersionNumber.parse(scalaFullVersion) def scalaBinaryVersion = "${scalaVersion.major}.${scalaVersion.minor}" dependencies { - // see https://github.com/apache/incubator-pekko-grpc/issues/22 - // implementation group: 'ch.megard', name: "akka-http-cors_${scalaBinaryVersion}", version: '1.1.3' + implementation group: 'ch.megard', name: "pekko-http-cors_${scalaBinaryVersion}", version: '0.0.0-SNAPSHOT' implementation "org.scala-lang:scala-library:${scalaFullVersion}" testImplementation "org.apache.pekko:pekko-stream-testkit_${scalaBinaryVersion}:0.0.0+26599-83545a33-SNAPSHOT" testImplementation "org.scalatest:scalatest_${scalaBinaryVersion}:3.2.15" diff --git a/plugin-tester-java/pom.xml b/plugin-tester-java/pom.xml index e72df146..c0e799e6 100644 --- a/plugin-tester-java/pom.xml +++ b/plugin-tester-java/pom.xml @@ -15,7 +15,7 @@ 1.8 3.1.2 3.0.0 - 1.1.0 + 0.0.0-SNAPSHOT 1.48.1 UTF-8 @@ -26,6 +26,12 @@ Apache Snapshots Repository https://repository.apache.org/content/repositories/snapshots/ + + + sonatype-snapshots + Sonatype Snapshots Repository + https://oss.sonatype.org/content/repositories/snapshots/ + @@ -34,13 +40,11 @@ pekko-grpc-runtime_2.12 ${pekko.grpc.project.version} - diff --git a/plugin-tester-scala/build.gradle b/plugin-tester-scala/build.gradle index ec588b83..59d6e440 100644 --- a/plugin-tester-scala/build.gradle +++ b/plugin-tester-scala/build.gradle @@ -8,6 +8,7 @@ pekkoGrpc { repositories { mavenLocal() + maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } // Remove when proper release of pekko-http-cors is made } def scalaFullVersion = "2.12.16" @@ -15,8 +16,7 @@ def scalaVersion = org.gradle.util.VersionNumber.parse(scalaFullVersion) def scalaBinaryVersion = "${scalaVersion.major}.${scalaVersion.minor}" dependencies { - // see https://github.com/apache/incubator-pekko-grpc/issues/22 - // implementation group: 'ch.megard', name: "akka-http-cors_${scalaBinaryVersion}", version: '1.1.3' + implementation group: 'ch.megard', name: "pekko-http-cors_${scalaBinaryVersion}", version: '0.0.0-SNAPSHOT' implementation "org.scala-lang:scala-library:${scalaFullVersion}" testImplementation "org.apache.pekko:pekko-stream-testkit_${scalaBinaryVersion}:0.0.0+26599-83545a33-SNAPSHOT" testImplementation "org.scalatest:scalatest_${scalaBinaryVersion}:3.2.12" diff --git a/plugin-tester-scala/pom.xml b/plugin-tester-scala/pom.xml index fc7b7686..09b51098 100644 --- a/plugin-tester-scala/pom.xml +++ b/plugin-tester-scala/pom.xml @@ -14,7 +14,7 @@ 1.8 1.8 0.0.0+26599-83545a33-SNAPSHOT - 0.4.2 + 0.0.0-SNAPSHOT 1.48.1 UTF-8 @@ -25,6 +25,12 @@ Apache Snapshots Repository https://repository.apache.org/content/repositories/snapshots/ + + + sonatype-snapshots + Sonatype Snapshots Repository + https://oss.sonatype.org/content/repositories/snapshots/ + @@ -33,13 +39,11 @@ pekko-grpc-runtime_2.12 ${pekko.grpc.project.version} - org.apache.pekko diff --git a/project/Dependencies.scala b/project/Dependencies.scala index a697cfbc..748a82d5 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -39,6 +39,8 @@ object Dependencies { val pekkoDiscovery = "org.apache.pekko" %% "pekko-discovery" % Versions.pekko val pekkoSlf4j = "org.apache.pekko" %% "pekko-slf4j" % Versions.pekko + val pekkoHttpCors = "ch.megard" %% "pekko-http-cors" % "0.0.0-SNAPSHOT" // Apache v2 + val scalapbCompilerPlugin = "com.thesamet.scalapb" %% "compilerplugin" % scalapb.compiler.Version.scalapbVersion val scalapbRuntime = ("com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion) .exclude("io.grpc", "grpc-netty") @@ -103,7 +105,7 @@ object Dependencies { Compile.pekkoHttpCore, Compile.pekkoHttp, Compile.pekkoDiscovery, - // Compile.akkaHttpCors % "provided", + Compile.pekkoHttpCors % "provided", Test.pekkoTestkit, Test.pekkoStreamTestkit, Test.scalaTest, @@ -136,7 +138,7 @@ object Dependencies { val pluginTester = l ++= Seq( // usually automatically added by `suggestedDependencies`, which doesn't work with ReflectiveCodeGen Compile.grpcStub, - // Compile.akkaHttpCors, + Compile.pekkoHttpCors, Test.scalaTest, Test.scalaTestPlusJunit, Protobuf.googleCommonProtos) diff --git a/runtime/src/main/scala/org/apache/pekko/grpc/javadsl/WebHandler.scala b/runtime/src/main/scala/org/apache/pekko/grpc/javadsl/WebHandler.scala index e80ab716..262b3f3a 100644 --- a/runtime/src/main/scala/org/apache/pekko/grpc/javadsl/WebHandler.scala +++ b/runtime/src/main/scala/org/apache/pekko/grpc/javadsl/WebHandler.scala @@ -23,8 +23,8 @@ import pekko.japi.function.{ Function => JFunction } import pekko.stream.Materializer import pekko.stream.javadsl.{ Keep, Sink, Source } import pekko.util.ConstantFun -//import ch.megard.akka.http.cors.javadsl.settings.CorsSettings -//import ch.megard.akka.http.cors.javadsl.CorsDirectives +import ch.megard.pekko.http.cors.javadsl.settings.CorsSettings +import ch.megard.pekko.http.cors.javadsl.CorsDirectives @ApiMayChange object WebHandler { @@ -37,11 +37,11 @@ object WebHandler { * - If the request s not a CORS pre-flight request, and has an invalid media type, then a _415: Unsupported Media Type_ response is produced. * - Otherise if the request is not handled by one of the provided handlers, a _404: Not Found_ response is produced. */ - /*def grpcWebHandler( + def grpcWebHandler( handlers: util.List[JFunction[HttpRequest, CompletionStage[HttpResponse]]], as: ClassicActorSystemProvider, mat: Materializer): JFunction[HttpRequest, CompletionStage[HttpResponse]] = - grpcWebHandler(handlers, as, mat, scaladsl.WebHandler.defaultCorsSettings)*/ + grpcWebHandler(handlers, as, mat, scaladsl.WebHandler.defaultCorsSettings) // Adapt Marshaller.futureMarshaller(fromResponse) to javadsl private implicit val csResponseMarshaller: ToResponseMarshaller[CompletionStage[HttpResponse]] = { @@ -64,13 +64,13 @@ object WebHandler { def grpcWebHandler( handlers: util.List[JFunction[HttpRequest, CompletionStage[HttpResponse]]], as: ClassicActorSystemProvider, - mat: Materializer /*, - corsSettings: CorsSettings*/ ): JFunction[HttpRequest, CompletionStage[HttpResponse]] = { + mat: Materializer, + corsSettings: CorsSettings): JFunction[HttpRequest, CompletionStage[HttpResponse]] = { import scala.collection.JavaConverters._ val servicesHandler = concatOrNotFound(handlers.asScala.toList: _*) val servicesRoute = RouteAdapter(MarshallingDirectives.handleWith(servicesHandler.apply(_))) - val handler = asyncHandler(/*CorsDirectives.cors(corsSettings, () => */ servicesRoute /*)*/, as, mat) + val handler = asyncHandler(CorsDirectives.cors(corsSettings, () => servicesRoute), as, mat) (req: HttpRequest) => if (scaladsl.ServiceHandler.isGrpcWebRequest(req) /*|| scaladsl.WebHandler.isCorsPreflightRequest(req)*/ ) handler(req) diff --git a/runtime/src/main/scala/org/apache/pekko/grpc/scaladsl/WebHandler.scala b/runtime/src/main/scala/org/apache/pekko/grpc/scaladsl/WebHandler.scala index 7daaa035..1083a50b 100644 --- a/runtime/src/main/scala/org/apache/pekko/grpc/scaladsl/WebHandler.scala +++ b/runtime/src/main/scala/org/apache/pekko/grpc/scaladsl/WebHandler.scala @@ -4,25 +4,25 @@ package org.apache.pekko.grpc.scaladsl -//import scala.collection.immutable +import scala.collection.immutable import scala.concurrent.Future import org.apache.pekko import pekko.actor.ClassicActorSystemProvider import pekko.annotation.ApiMayChange -//import pekko.http.javadsl.{ model => jmodel } -import pekko.http.scaladsl.model.{ HttpRequest, HttpResponse } -//import pekko.http.scaladsl.model.headers._ +import pekko.http.javadsl.{ model => jmodel } +import pekko.http.scaladsl.model.{ HttpMethods, HttpRequest, HttpResponse } +import pekko.http.scaladsl.model.headers._ import pekko.http.scaladsl.server.Route import pekko.http.scaladsl.server.directives.MarshallingDirectives.handleWith -//import ch.megard.akka.http.cors.scaladsl.CorsDirectives.cors -//import ch.megard.akka.http.cors.scaladsl.model.HttpHeaderRange -//import ch.megard.akka.http.cors.scaladsl.settings.CorsSettings +import ch.megard.pekko.http.cors.scaladsl.CorsDirectives.cors +import ch.megard.pekko.http.cors.scaladsl.model.HttpHeaderRange +import ch.megard.pekko.http.cors.scaladsl.settings.CorsSettings @ApiMayChange object WebHandler { /** Default CORS settings to use for grpc-web */ - /*val defaultCorsSettings: CorsSettings = CorsSettings.defaultSettings + val defaultCorsSettings: CorsSettings = CorsSettings.defaultSettings .withAllowCredentials(true) .withAllowedMethods(immutable.Seq(HttpMethods.POST, HttpMethods.OPTIONS)) .withExposedHeaders(immutable.Seq(headers.`Status`.name, headers.`Status-Message`.name, `Content-Encoding`.name)) @@ -38,7 +38,7 @@ object WebHandler { private[grpc] def isCorsPreflightRequest(r: jmodel.HttpRequest): Boolean = r.method == HttpMethods.OPTIONS && r.getHeader(classOf[Origin]).isPresent && r .getHeader(classOf[`Access-Control-Request-Method`]) - .isPresent*/ + .isPresent /** * Creates a `HttpRequest` to `HttpResponse` handler for gRPC services that can be used in @@ -49,13 +49,13 @@ object WebHandler { * - Otherise if the request is not handled by one of the provided handlers, a _404: Not Found_ response is produced. */ def grpcWebHandler(handlers: PartialFunction[HttpRequest, Future[HttpResponse]]*)( - implicit as: ClassicActorSystemProvider /*, - corsSettings: CorsSettings = defaultCorsSettings*/ ): HttpRequest => Future[HttpResponse] = { + implicit as: ClassicActorSystemProvider, + corsSettings: CorsSettings = defaultCorsSettings): HttpRequest => Future[HttpResponse] = { implicit val system = as.classicSystem val servicesHandler = ServiceHandler.concat(handlers: _*) - Route.toFunction /*cors(corsSettings)*/ { + Route.toFunction(cors(corsSettings) { handleWith(servicesHandler) - } + }) } }