-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
334 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
87 changes: 87 additions & 0 deletions
87
codegen/src/main/twirl/templates/PlayJavaServer/RouterUsingActions.scala.txt
This file contains 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,87 @@ | ||
@* | ||
* Copyright (C) 2018-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*@ | ||
|
||
@(service: akka.grpc.gen.javadsl.Service, powerApis: Boolean) | ||
|
||
@akka.grpc.gen.Constants.DoNotEditComment | ||
package @service.packageName; | ||
|
||
import akka.annotation.InternalApi; | ||
import akka.actor.ActorSystem; | ||
import akka.grpc.GrpcServiceException; | ||
import akka.grpc.internal.PlayRouterHelper$; | ||
import akka.grpc.internal.PlayRouterUsingActions; | ||
import akka.grpc.javadsl.GrpcExceptionHandler; | ||
import akka.http.javadsl.model.HttpRequest; | ||
import akka.http.javadsl.model.HttpResponse; | ||
import akka.japi.Function; | ||
import akka.stream.Materializer; | ||
import play.api.mvc.EssentialAction; | ||
import play.api.mvc.RequestHeader; | ||
import play.api.mvc.PlayBodyParsers; | ||
import play.api.mvc.ActionBuilder; | ||
import play.api.mvc.Request; | ||
import play.api.mvc.AnyContent; | ||
|
||
import scala.Function1; | ||
import scala.NotImplementedError; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
@defining(if (powerApis) service.name + "PowerApi" else service.name) { serviceName => | ||
/** | ||
* Abstract base class for implementing @{serviceName} and using as a play Router | ||
*/ | ||
public abstract class Abstract@{serviceName}Router extends PlayRouterUsingActions implements @{serviceName} { | ||
private final Function<ActorSystem, Function<Throwable, io.grpc.Status>> eHandler; | ||
private final ActorSystem system; | ||
private final PlayBodyParsers parsers; | ||
private final ActionBuilder<Request, AnyContent> actionBuilder; | ||
@{ | ||
val (streamingInputMethods: Seq[String], unaryInputMethods: Seq[String]) = service.methods.partition(_.inputStreaming) match { | ||
case (streaming, unary) => (streaming.map(_.grpcName), unary.map(_.grpcName)) | ||
} | ||
s""" | ||
private final List<String> streamingInputMethods = Arrays.asList(${streamingInputMethods.mkString("\"", "\", \"", "\"")}); | ||
private final List<String> unaryInputMethods = Arrays.asList(${unaryInputMethods.mkString("\"", "\", \"", "\"")}); | ||
""" | ||
} | ||
|
||
public Abstract@{serviceName}Router(Materializer mat, ActorSystem system, PlayBodyParsers parsers, ActionBuilder<Request, AnyContent> actionBuilder) { | ||
this(mat, system, parsers, actionBuilder, GrpcExceptionHandler.defaultMapper()); | ||
} | ||
|
||
public Abstract@{serviceName}Router(Materializer mat, ActorSystem system, PlayBodyParsers parsers, ActionBuilder<Request, AnyContent> actionBuilder, Function<ActorSystem, Function<Throwable, io.grpc.Status>> eHandler) { | ||
super(mat, @{service.name}.name, parsers, actionBuilder); | ||
this.eHandler = eHandler; | ||
this.system = system; | ||
this.parsers = parsers; | ||
this.actionBuilder = actionBuilder; | ||
} | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
@@InternalApi | ||
@@Override | ||
final public Function1<RequestHeader, EssentialAction> createHandler(String serviceName, Materializer mat) { | ||
return new Function1<RequestHeader, EssentialAction>() { | ||
Function<HttpRequest, CompletionStage<HttpResponse>> handler = @{serviceName}HandlerFactory.create(Abstract@{serviceName}Router.this, serviceName, mat, eHandler, system); | ||
public EssentialAction apply(RequestHeader reqOuter) { | ||
String[] pathSegments = reqOuter.path().split("/"); | ||
if ((pathSegments.length == 3) && (pathSegments[1].equals(serviceName))) { | ||
String method = pathSegments[2]; | ||
if (streamingInputMethods.contains(method)) return createStreamingAction(PlayRouterHelper$.MODULE$.handlerFor(handler), mat.executionContext()); | ||
else if (unaryInputMethods.contains(method)) return createUnaryAction(PlayRouterHelper$.MODULE$.handlerFor(handler), mat.executionContext()); | ||
else throw new NotImplementedError("Not implemented: " + method); | ||
} else { | ||
throw new GrpcServiceException(io.grpc.Status.INTERNAL.withDescription("Unexpected/handled path " + reqOuter.path())); | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
codegen/src/main/twirl/templates/PlayScala/RouterUsingActions.scala.txt
This file contains 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,53 @@ | ||
@* | ||
* Copyright (C) 2018-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*@ | ||
|
||
@(service: akka.grpc.gen.scaladsl.Service, powerApis: Boolean) | ||
|
||
@akka.grpc.gen.Constants.DoNotEditComment | ||
package @service.packageName | ||
|
||
import akka.annotation.InternalApi | ||
import akka.actor.ActorSystem | ||
import akka.grpc.GrpcServiceException | ||
import akka.grpc.internal.PlayRouterUsingActions | ||
import akka.grpc.scaladsl.GrpcExceptionHandler.defaultMapper | ||
import akka.http.scaladsl.model.Uri.Path | ||
import akka.http.scaladsl.model.Uri.Path.Segment | ||
import akka.stream.Materializer | ||
import io.grpc.Status | ||
import play.api.mvc.{EssentialAction, RequestHeader, PlayBodyParsers, ActionBuilder, Request, AnyContent} | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
@defining(if (powerApis) service.name + "PowerApi" else service.name) { serviceName => | ||
/** | ||
* Abstract base class for implementing @{serviceName} and using as a play Router | ||
*/ | ||
abstract class Abstract@{serviceName}Router(mat: Materializer, system: ActorSystem, parsers: PlayBodyParsers, actionBuilder: ActionBuilder[Request, AnyContent], eHandler: ActorSystem => PartialFunction[Throwable, Status] = defaultMapper) extends PlayRouterUsingActions(mat, @{service.name}.name, parsers, actionBuilder) with @{serviceName} { | ||
|
||
@{ | ||
val (streamingInputMethods: Seq[String], unaryInputMethods: Seq[String]) = service.methods.partition(_.inputStreaming) match { | ||
case (streaming, unary) => (streaming.map(_.grpcName), unary.map(_.grpcName)) | ||
} | ||
"val (streamingInputMethods: Seq[String], unaryInputMethods: Seq[String]) = (Seq(" + streamingInputMethods.mkString("\"", "\", \"", "\"") + "), Seq(" + unaryInputMethods.mkString("\"", "\", \"", "\"") + "))" | ||
} | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
@@InternalApi | ||
final override protected def createHandler(serviceName: String, mat: Materializer): RequestHeader => EssentialAction = { | ||
val handler = @{serviceName}Handler(this, serviceName, eHandler)(mat, system) | ||
reqOuter => | ||
implicit val ec: ExecutionContext = mat.executionContext | ||
Path(reqOuter.path) match { | ||
case Path.Slash(Segment(`serviceName`, Path.Slash(Segment(method, Path.Empty)))) => | ||
if (streamingInputMethods.contains(method)) createStreamingAction(handler) | ||
else if (unaryInputMethods.contains(method)) createUnaryAction(handler) | ||
else throw new NotImplementedError(s"Not implemented: $method") | ||
case _ => throw new GrpcServiceException(io.grpc.Status.INTERNAL.withDescription(s"Unexpected/handled path ${reqOuter.path}")) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.