generated from vertx-howtos/howto-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServerVerticle.java
47 lines (39 loc) · 1.36 KB
/
ServerVerticle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package io.vertx.howtos.grpcweb;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.grpc.server.GrpcServer;
public class ServerVerticle extends VerticleBase {
@Override
public Future<?> start() {
// tag::grpcServer[]
VertxGreeterGrpcServer.GreeterApi stub = new VertxGreeterGrpcServer.GreeterApi() {
@Override
public Future<HelloReply> sayHello(HelloRequest request) {
return Future.succeededFuture(HelloReply.newBuilder().setMessage("Hello " + request.getName()).build());
}
};
GrpcServer grpcServer = GrpcServer.server(vertx);
stub.bindAll(grpcServer);
// end::grpcServer[]
// tag::routerAndServer[]
Router router = Router.router(vertx);
router.route()
.consumes("application/grpc-web-text") // <1>
.handler(rc -> grpcServer.handle(rc.request()));
router.get().handler(StaticHandler.create()); // <2>
return vertx.createHttpServer()
.requestHandler(router)
.listen(8080);
// end::routerAndServer[]
}
// tag::main[]
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new ServerVerticle()).await();
System.out.println("Server started, browse to http://localhost:8080");
}
// end::main[]
}