Skip to content

Commit b25f098

Browse files
committed
Code comments
1 parent f746353 commit b25f098

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
<artifactId>grpc-web-howto</artifactId>
1010
<version>1.0-SNAPSHOT</version>
1111

12+
<!-- tag::properties[] -->
1213
<properties>
1314
<vertx.version>5.0.0.CR2</vertx.version>
1415
<grpc.version>1.65.1</grpc.version>
1516
<protoc.version>4.27.2</protoc.version>
1617
<protobuf.version>4.27.2</protobuf.version>
1718
</properties>
19+
<!-- end::properties[] -->
1820

21+
<!-- tag::dependencies[] -->
1922
<dependencyManagement>
2023
<dependencies>
2124
<dependency>
@@ -62,16 +65,20 @@
6265
<version>${grpc.version}</version>
6366
</dependency>
6467
</dependencies>
68+
<!-- end::dependencies[] -->
6569

6670
<build>
6771
<extensions>
72+
<!-- tag::os-maven-plugin[] -->
6873
<extension>
6974
<groupId>kr.motd.maven</groupId>
7075
<artifactId>os-maven-plugin</artifactId>
7176
<version>1.7.1</version>
7277
</extension>
78+
<!-- end::os-maven-plugin[] -->
7379
</extensions>
7480
<plugins>
81+
<!-- tag::protobuf-maven-plugin[] -->
7582
<plugin>
7683
<groupId>org.xolstice.maven.plugins</groupId>
7784
<artifactId>protobuf-maven-plugin</artifactId>
@@ -135,6 +142,8 @@
135142
</execution>
136143
</executions>
137144
</plugin>
145+
<!-- end::protobuf-maven-plugin[] -->
146+
<!-- tag::esbuild-maven-plugin[] -->
138147
<plugin>
139148
<groupId>io.mvnpm</groupId>
140149
<artifactId>esbuild-maven-plugin</artifactId>
@@ -164,6 +173,7 @@
164173
</dependency>
165174
</dependencies>
166175
</plugin>
176+
<!-- end::esbuild-maven-plugin[] -->
167177
<plugin>
168178
<groupId>org.apache.maven.plugins</groupId>
169179
<artifactId>maven-compiler-plugin</artifactId>

src/main/java/io/vertx/howtos/grpcweb/ServerVerticle.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public class ServerVerticle extends VerticleBase {
1414

1515
@Override
1616
public Future<?> start() {
17-
GrpcIoServer grpcServer = GrpcIoServer.server(vertx, new GrpcServerOptions().setGrpcWebEnabled(true));
18-
17+
// tag::grpcServer[]
1918
GreeterGrpc.GreeterImplBase service = new GreeterGrpc.GreeterImplBase() {
2019
@Override
2120
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
@@ -24,20 +23,31 @@ public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseOb
2423
}
2524
};
2625

26+
GrpcServerOptions grpcServerOptions = new GrpcServerOptions().setGrpcWebEnabled(true); // <2>
27+
GrpcIoServer grpcServer = GrpcIoServer.server(vertx, grpcServerOptions);
2728
GrpcIoServiceBridge serverStub = GrpcIoServiceBridge.bridge(service);
2829
serverStub.bind(grpcServer);
30+
// end::grpcServer[]
2931

32+
// tag::routerAndServer[]
3033
Router router = Router.router(vertx);
31-
router.route().consumes("application/grpc-web-text").handler(rc -> grpcServer.handle(rc.request()));
34+
router.route()
35+
.consumes("application/grpc-web-text")
36+
.handler(rc -> grpcServer.handle(rc.request()));
37+
3238
router.get().handler(StaticHandler.create());
3339

3440
return vertx.createHttpServer()
3541
.requestHandler(router)
3642
.listen(8080);
43+
// end::routerAndServer[]
3744
}
3845

46+
// tag::main[]
3947
public static void main(String[] args) {
4048
Vertx vertx = Vertx.vertx();
4149
vertx.deployVerticle(new ServerVerticle()).await();
50+
System.out.println("Server started, browse to http://localhost:8080");
4251
}
52+
// end::main[]
4353
}

src/main/proto/service.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package helloworld;
88

99
// The greeting service definition.
1010
service Greeter {
11-
// Sends a greeting
11+
// Ask for a greeting
1212
rpc SayHello (HelloRequest) returns (HelloReply) {}
1313
}
1414

@@ -17,7 +17,7 @@ message HelloRequest {
1717
string name = 1;
1818
}
1919

20-
// The response message containing the greetings
20+
// The response message containing the greeting
2121
message HelloReply {
2222
string message = 1;
2323
}

src/main/resources/webroot/index.html

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Echo Example</title>
6-
<script type="module" src="/js/index.js"></script>
6+
<script type="module">
7+
import {sayHello} from "/js/index.js";
8+
9+
window.sayHello = sayHello;
10+
</script>
711
</head>
812
<body>
913
<div>
10-
<p>Type a name in the input field and click the send button.</p>
14+
<p>Type a name in the input field and press enter, or click the send button.</p>
1115
<div class="input-group">
12-
<input type="text" id="name">
13-
<button type="button" id="send">Send</button>
16+
<!-- Invoke javascript function on submit -->
17+
<form onsubmit="return sayHello();">
18+
<input type="text" id="name">
19+
<input type="submit" value="Send">
20+
</form>
1421
</div>
1522
<p id="msg"></p>
1623
</div>

src/main/web/index.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1+
// Import HelloRequest object from JS generated file
12
const {HelloRequest} = require("./service_pb");
3+
// Import GreeterClient object from gRPC Web (JS) generated file
24
const {GreeterClient} = require("./service_grpc_web_pb");
35

6+
// Configure to client to send requests to the web server
47
const greeterClient = new GreeterClient("http://" + window.location.hostname + ":8080", null, null);
58

6-
function sayHello(name, callback) {
9+
export function sayHello() {
710
const request = new HelloRequest();
8-
request.setName(name);
11+
request.setName(document.getElementById("name").value);
912
greeterClient.sayHello(request, {}, (err, response) => {
13+
const msgElem = document.getElementById("msg");
1014
if (err) {
11-
callback.call(null, `Unexpected error for sayHello: code = ${err.code}` + `, message = "${err.message}"`);
15+
msgElem.innerText = `Unexpected error for sayHello: code = ${err.code}` + `, message = "${err.message}"`;
1216
} else {
13-
callback.call(null, response.getMessage());
17+
msgElem.innerText = response.getMessage();
1418
}
1519
});
20+
return false; // prevent form posting
1621
}
17-
18-
document.addEventListener("DOMContentLoaded", () => {
19-
document.getElementById("send").addEventListener("click", () => {
20-
const name = document.getElementById("name").value;
21-
sayHello(name, msg => {
22-
document.getElementById("msg").innerText = msg;
23-
});
24-
});
25-
});

0 commit comments

Comments
 (0)