Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
Use full service name in default mappings for HTTP API (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvlugter authored Jan 27, 2021
1 parent 3a25f6e commit d7d5453
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ object HttpApi {
body = "*", // Parse all input
responseBody = "", // Include all output
additionalBindings = Nil, // No need for additional bindings
pattern = HttpRule.Pattern.Post((Path / "v1" / method.getName).toString)
pattern = HttpRule.Pattern.Post((Path / service.getFullName / method.getName).toString)
)
log.info(s"Using generated HTTP API endpoint using [$rule]")
rule
Expand Down
17 changes: 15 additions & 2 deletions proxy/core/src/main/scala/io/cloudstate/proxy/Serve.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ import akka.grpc.internal.{
}
import akka.NotUsed
import akka.grpc.{ProtobufSerializer, Trailers}
import akka.http.scaladsl.model.{HttpEntity, HttpHeader, HttpRequest, HttpResponse, StatusCodes}
import akka.http.scaladsl.model.{
ContentTypes,
HttpEntity,
HttpHeader,
HttpProtocols,
HttpRequest,
HttpResponse,
StatusCodes
}
import akka.http.scaladsl.model.Uri.Path
import akka.actor.{ActorRef, ActorSystem}
import akka.event.{Logging, LoggingAdapter}
Expand Down Expand Up @@ -196,6 +204,11 @@ object Serve {
)
}

private def isGrpcRequest(request: HttpRequest): Boolean =
(request.protocol == HttpProtocols.`HTTP/2.0`) &&
((request.entity.contentType == ContentTypes.`application/grpc+proto`) ||
(request.entity.contentType.mediaType.value == "application/grpc"))

private[this] final def createGrpcApi(entities: Seq[ServableEntity],
router: UserFunctionRouter,
entityDiscoveryClient: EntityDiscoveryClient,
Expand All @@ -215,7 +228,7 @@ object Serve {
}).toMap

val routes: PartialFunction[HttpRequest, Future[(List[HttpHeader], Source[ProtobufAny, NotUsed])]] = {
case req: HttpRequest if rpcMethodSerializers.contains(req.uri.path) =>
case req: HttpRequest if isGrpcRequest(req) && rpcMethodSerializers.contains(req.uri.path) =>
log.debug("Received gRPC request [{}]", req.uri.path)

val handler = rpcMethodSerializers(req.uri.path)
Expand Down
9 changes: 9 additions & 0 deletions tck/src/main/scala/io/cloudstate/tck/CrdtEntityTCK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,15 @@ trait CrdtEntityTCK extends TCKSpec {
.expectIncoming(command(1, id, "Process", Request(id)))
.expectOutgoing(reply(1, PNCounter.state(0)))

client.http
.request("cloudstate.tck.model.crdt.CrdtTwo/Call", s"""{"id": "$id"}""")
.futureValue mustBe "{}"
interceptor
.expectCrdtEntityConnection()
.expectIncoming(init(ServiceTwo, id))
.expectIncoming(command(1, id, "Call", Request(id)))
.expectOutgoing(reply(1, Response()))

client.http
.request(s"tck/model/crdt/$id", """{"actions": [{"update": {"pncounter": {"change": 42} }}]}""")
.futureValue mustBe """{"state":{"pncounter":{"value":"42"}}}"""
Expand Down
9 changes: 9 additions & 0 deletions tck/src/main/scala/io/cloudstate/tck/EntityTCK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,15 @@ trait EntityTCK extends TCKSpec {
.expectIncoming(command(1, id, "Process", Request(id)))
.expectOutgoing(reply(1, Response()))

client.http
.request("cloudstate.tck.model.valueentity.ValueEntityTwo/Call", s"""{"id": "$id"}""")
.futureValue mustBe """{"message":""}"""
interceptor
.expectEntityConnection()
.expectIncoming(init(ServiceTwo, id))
.expectIncoming(command(1, id, "Call", Request(id)))
.expectOutgoing(reply(1, Response()))

client.http
.request(s"tck/model/entity/$id", """{"actions": [{"update": {"value": "one"}}]}""")
.futureValue mustBe """{"message":"one"}"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,15 @@ trait EventSourcedEntityTCK extends TCKSpec {
.expectIncoming(command(1, id, "Process", Request(id)))
.expectOutgoing(reply(1, Response()))

client.http
.request("cloudstate.tck.model.EventSourcedTwo/Call", s"""{"id": "$id"}""")
.futureValue mustBe """{"message":""}"""
interceptor
.expectEventSourcedEntityConnection()
.expectIncoming(init(ServiceTwo, id))
.expectIncoming(command(1, id, "Call", Request(id)))
.expectOutgoing(reply(1, Response()))

client.http
.request(s"tck/model/eventsourced/$id", """{"actions": [{"emit": {"value": "x"}}]}""")
.futureValue mustBe """{"message":"x"}"""
Expand Down

0 comments on commit d7d5453

Please sign in to comment.