Skip to content

Commit

Permalink
feat: http transcoding grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
WayneWang12 committed Feb 24, 2022
1 parent 86cfd84 commit 6b3cac4
Show file tree
Hide file tree
Showing 7 changed files with 1,359 additions and 1 deletion.
15 changes: 14 additions & 1 deletion codegen/src/main/twirl/templates/ScalaServer/Handler.scala.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import scala.concurrent.ExecutionContext

import akka.grpc.scaladsl.{ GrpcExceptionHandler, GrpcMarshalling }
import akka.grpc.Trailers
import akka.grpc.HttpApi

import akka.actor.ActorSystem
import akka.actor.ClassicActorSystemProvider
Expand Down Expand Up @@ -128,12 +129,24 @@ object @{serviceName}Handler {
.recoverWith(GrpcExceptionHandler.from(eHandler(system.classicSystem))(system, writer))
).getOrElse(unsupportedMediaType)

Function.unlift((req: model.HttpRequest) => req.uri.path match {
@for(method <- service.methods if !method.inputStreaming) {
lazy val @{method.name}Handler = @if(method.outputStreaming) {
HttpApi.getSourceHandler@{if(powerApis) "PowerApi" else ""}("@{method.name}", @{service.name}.descriptor, implementation.@{method.nameSafe})
} else {
HttpApi.getHttpFutureHandler@{if(powerApis) "PowerApi" else ""}("@{method.name}", @{service.name}.descriptor, implementation.@{method.nameSafe})
}
}

val httpApiHandlers = (List.empty[PartialFunction[model.HttpRequest, scala.concurrent.Future[model.HttpResponse]]] @for(method <- service.methods if !method.inputStreaming){ ++ @{method.name}Handler})
.foldLeft(PartialFunction.empty[model.HttpRequest, scala.concurrent.Future[model.HttpResponse]])((acc, p) => acc.orElse(p))

val grpcHandlers = 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
})
httpApiHandlers orElse grpcHandlers
}
}
}
2 changes: 2 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ object Dependencies {

object Protobuf {
val protobufJava = "com.google.protobuf" % "protobuf-java" % Versions.googleProtobuf
val protobufJavaUtil = "com.google.protobuf" % "protobuf-java-util" % Versions.googleProtobuf
val googleCommonProtos = "com.google.protobuf" % "protobuf-java" % Versions.googleProtobuf % "protobuf"
}

Expand All @@ -100,6 +101,7 @@ object Dependencies {
val runtime = l ++= Seq(
Compile.scalapbRuntime,
Protobuf.protobufJava, // or else scalapb pulls older version in transitively
Protobuf.protobufJavaUtil,
Compile.grpcCore,
Compile.grpcStub % "provided", // comes from the generators
Compile.grpcNettyShaded,
Expand Down
31 changes: 31 additions & 0 deletions runtime/src/main/protobuf/google/api/annotations.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2015 Google LLC
//
// Licensed 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.

syntax = "proto3";

package google.api;

import "google/api/http.proto";
import "google/protobuf/descriptor.proto";

option go_package = "google.golang.org/genproto/googleapis/api/annotations;annotations";
option java_multiple_files = true;
option java_outer_classname = "AnnotationsProto";
option java_package = "com.google.api";
option objc_class_prefix = "GAPI";

extend google.protobuf.MethodOptions {
// See `HttpRule`.
HttpRule http = 72295728;
}
Loading

0 comments on commit 6b3cac4

Please sign in to comment.