diff --git a/buildSrc/src/main/groovy/com.flipkart.varadhi.java-common-conventions.gradle b/buildSrc/src/main/groovy/com.flipkart.varadhi.java-common-conventions.gradle index f007b54c..cd715708 100644 --- a/buildSrc/src/main/groovy/com.flipkart.varadhi.java-common-conventions.gradle +++ b/buildSrc/src/main/groovy/com.flipkart.varadhi.java-common-conventions.gradle @@ -18,6 +18,8 @@ ext { slf4j_version = "2.0.7" javax_version = "1.3.2" vertx_version = "4.4.4" + protoc_version = "3.23.2" + grpc_version = "1.56.0" otl_version = "1.25.0" micrometer_version = "1.10.6" diff --git a/server/build.gradle b/server/build.gradle index ba2ab0fe..955809f8 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -1,9 +1,36 @@ import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform +buildscript { + dependencies { + // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier gradle versions + classpath 'com.google.protobuf:protobuf-gradle-plugin:0.9.3' + } +} + plugins { id 'com.flipkart.varadhi.java-application-conventions' + id 'com.google.protobuf' version "0.9.3" } +protobuf { + protoc { + artifact = "com.google.protobuf:protoc:$protoc_version" + } + plugins { + grpc { + artifact = "io.grpc:protoc-gen-grpc-java:$grpc_version" + } + vertx { + artifact = "io.vertx:vertx-grpc-protoc-plugin2:$vertx_version" + } + } + generateProtoTasks { + all()*.plugins { + grpc + vertx + } + } +} dependencies { @@ -22,6 +49,9 @@ dependencies { implementation("io.vertx:vertx-config") implementation("io.vertx:vertx-config-yaml") implementation("io.vertx:vertx-web") + implementation("io.vertx:vertx-grpc-server:$vertx_version") + implementation("io.vertx:vertx-grpc-client:$vertx_version") + implementation("io.vertx:vertx-grpc-context-storage:$vertx_version") implementation("io.vertx:vertx-auth-common") implementation("io.vertx:vertx-auth-jwt") implementation("io.vertx:vertx-opentelemetry") diff --git a/server/src/main/java/com/flipkart/varadhi/RestVerticle.java b/server/src/main/java/com/flipkart/varadhi/RestVerticle.java index 7125fb6a..ddb0c2c2 100644 --- a/server/src/main/java/com/flipkart/varadhi/RestVerticle.java +++ b/server/src/main/java/com/flipkart/varadhi/RestVerticle.java @@ -1,18 +1,26 @@ package com.flipkart.varadhi; import com.flipkart.varadhi.exceptions.InvalidStateException; +import com.flipkart.varadhi.web.Extensions; import com.flipkart.varadhi.web.FailureHandler; import com.flipkart.varadhi.web.routes.RouteBehaviour; import com.flipkart.varadhi.web.routes.RouteConfigurator; import com.flipkart.varadhi.web.routes.RouteDefinition; +import com.flipkart.varadhi.web.v1.proto.MessageProducerGrpc; +import com.flipkart.varadhi.web.v1.proto.SingleMessageResponse; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promise; +import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerOptions; +import io.vertx.core.http.HttpVersion; import io.vertx.ext.web.Route; import io.vertx.ext.web.Router; +import io.vertx.grpc.common.GrpcStatus; +import io.vertx.grpc.server.GrpcServer; import lombok.extern.slf4j.Slf4j; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -36,6 +44,27 @@ public void start(Promise startPromise) { log.info("HttpServer Starting."); Router router = Router.router(vertx); + // grpc + GrpcServer grpcServer = GrpcServer.server(vertx); + + grpcServer.callHandler(MessageProducerGrpc.getProduceMethod(), request -> { + request.response().status(GrpcStatus.OK).statusMessage("OK") + .end(SingleMessageResponse.newBuilder().setOffset("0001").build()); + }); + + router.route() + .consumes("application/grpc") + .produces("application/grpc") + .handler(req -> grpcServer.handle(req.request())); + + router.route() + .consumes("application/json") + .produces("application/json") + .path("/topics/produce") + .method(HttpMethod.POST) + .handler(rc -> Extensions.RoutingContextExtension.endRequestWithResponse(rc, 200, "0001")); + + // http FailureHandler failureHandler = new FailureHandler(); for (RouteDefinition def : apiRoutes) { Route route = router.route().method(def.method()).path(def.path()); diff --git a/server/src/main/java/com/flipkart/varadhi/web/v1/TopicHandlers.java b/server/src/main/java/com/flipkart/varadhi/web/v1/TopicHandlers.java index 86dd52b5..d4dfb011 100644 --- a/server/src/main/java/com/flipkart/varadhi/web/v1/TopicHandlers.java +++ b/server/src/main/java/com/flipkart/varadhi/web/v1/TopicHandlers.java @@ -9,6 +9,7 @@ import com.flipkart.varadhi.services.VaradhiTopicService; import com.flipkart.varadhi.web.Extensions.RequestBodyExtension; import com.flipkart.varadhi.web.Extensions.RoutingContextExtension; +import com.flipkart.varadhi.web.HandlerUtil; import com.flipkart.varadhi.web.routes.RouteDefinition; import com.flipkart.varadhi.web.routes.RouteProvider; import com.flipkart.varadhi.web.routes.SubRoutes; diff --git a/server/src/main/proto/api.v1.proto b/server/src/main/proto/api.v1.proto new file mode 100644 index 00000000..886a6596 --- /dev/null +++ b/server/src/main/proto/api.v1.proto @@ -0,0 +1,19 @@ +syntax = "proto3"; + +option java_multiple_files = true; +option java_package = "com.flipkart.varadhi.web.v1.proto"; +option java_outer_classname = "ProduceApi"; + +// The greeting service definition. +service MessageProducer { + // Sends a greeting + rpc produce (SingleMessageRequest) returns (SingleMessageResponse) {} +} + +message SingleMessageRequest { + string payload = 1; +} + +message SingleMessageResponse { + string offset = 1; +}