|
| 1 | +package io.vertx.howtos.grpcweb; |
| 2 | + |
| 3 | +import io.vertx.core.Future; |
| 4 | +import io.vertx.core.Promise; |
| 5 | +import io.vertx.core.Handler; |
| 6 | +import io.vertx.core.http.HttpServerRequest; |
| 7 | +import io.vertx.core.streams.ReadStream; |
| 8 | +import io.vertx.core.streams.WriteStream; |
| 9 | +import io.vertx.grpc.common.GrpcStatus; |
| 10 | +import io.vertx.grpc.common.ServiceName; |
| 11 | +import io.vertx.grpc.common.ServiceMethod; |
| 12 | +import io.vertx.grpc.common.GrpcReadStream; |
| 13 | +import io.vertx.grpc.common.GrpcWriteStream; |
| 14 | +import io.vertx.grpc.common.GrpcMessageDecoder; |
| 15 | +import io.vertx.grpc.common.GrpcMessageEncoder; |
| 16 | +import io.vertx.grpc.server.GrpcServerResponse; |
| 17 | +import io.vertx.grpc.server.GrpcServer; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +public class VertxGreeterGrpcServer { |
| 23 | + |
| 24 | + public static final ServiceMethod<io.vertx.howtos.grpcweb.HelloRequest, io.vertx.howtos.grpcweb.HelloReply> SayHello = ServiceMethod.server( |
| 25 | + ServiceName.create("helloworld", "Greeter"), |
| 26 | + "SayHello", |
| 27 | + GrpcMessageEncoder.encoder(), |
| 28 | + GrpcMessageDecoder.decoder(io.vertx.howtos.grpcweb.HelloRequest.parser())); |
| 29 | + public static final ServiceMethod<io.vertx.howtos.grpcweb.HelloRequest, io.vertx.howtos.grpcweb.HelloReply> SayHello_JSON = ServiceMethod.server( |
| 30 | + ServiceName.create("helloworld", "Greeter"), |
| 31 | + "SayHello", |
| 32 | + GrpcMessageEncoder.json(), |
| 33 | + GrpcMessageDecoder.json(() -> io.vertx.howtos.grpcweb.HelloRequest.newBuilder())); |
| 34 | + |
| 35 | + public static class GreeterApi { |
| 36 | + |
| 37 | + public Future<io.vertx.howtos.grpcweb.HelloReply> sayHello(io.vertx.howtos.grpcweb.HelloRequest request) { |
| 38 | + throw new UnsupportedOperationException("Not implemented"); |
| 39 | + } |
| 40 | + public void sayHello(io.vertx.howtos.grpcweb.HelloRequest request, Promise<io.vertx.howtos.grpcweb.HelloReply> response) { |
| 41 | + sayHello(request) |
| 42 | + .onSuccess(msg -> response.complete(msg)) |
| 43 | + .onFailure(error -> response.fail(error)); |
| 44 | + } |
| 45 | + |
| 46 | + public final void handle_sayHello(io.vertx.grpc.server.GrpcServerRequest<io.vertx.howtos.grpcweb.HelloRequest, io.vertx.howtos.grpcweb.HelloReply> request) { |
| 47 | + Promise<io.vertx.howtos.grpcweb.HelloReply> promise = Promise.promise(); |
| 48 | + request.handler(msg -> { |
| 49 | + try { |
| 50 | + sayHello(msg, promise); |
| 51 | + } catch (RuntimeException err) { |
| 52 | + promise.tryFail(err); |
| 53 | + } |
| 54 | + }); |
| 55 | + promise.future() |
| 56 | + .onFailure(err -> request.response().status(GrpcStatus.INTERNAL).end()) |
| 57 | + .onSuccess(resp -> request.response().end(resp)); |
| 58 | + } |
| 59 | + public GreeterApi bind_sayHello(GrpcServer server) { |
| 60 | + return bind_sayHello(server, io.vertx.grpc.common.WireFormat.PROTOBUF); |
| 61 | + } |
| 62 | + public GreeterApi bind_sayHello(GrpcServer server, io.vertx.grpc.common.WireFormat format) { |
| 63 | + ServiceMethod<io.vertx.howtos.grpcweb.HelloRequest,io.vertx.howtos.grpcweb.HelloReply> serviceMethod; |
| 64 | + switch(format) { |
| 65 | + case PROTOBUF: |
| 66 | + serviceMethod = SayHello; |
| 67 | + break; |
| 68 | + case JSON: |
| 69 | + serviceMethod = SayHello_JSON; |
| 70 | + break; |
| 71 | + default: |
| 72 | + throw new AssertionError(); |
| 73 | + } |
| 74 | + server.callHandler(serviceMethod, this::handle_sayHello); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public final GreeterApi bindAll(GrpcServer server) { |
| 79 | + bind_sayHello(server); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public final GreeterApi bindAll(GrpcServer server, io.vertx.grpc.common.WireFormat format) { |
| 84 | + bind_sayHello(server, format); |
| 85 | + return this; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments