Skip to content

Commit

Permalink
Varify
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 16, 2023
1 parent 73e6a14 commit 0a8845a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public static void main(String[] args) throws Exception {

@Override
public void start() {
HttpServer server = vertx.createHttpServer();
var server = vertx.createHttpServer();
server.requestHandler(request -> {
request.response().end("Hello World");
});
await(server.listen(8080, "localhost"));

// Make a simple HTTP request
HttpClient client = vertx.createHttpClient();
HttpClientRequest req = await(client.request(HttpMethod.GET, 8080, "localhost", "/"));
HttpClientResponse resp = await(req.send());
int status = resp.statusCode();
Buffer body = await(resp.body());
var client = vertx.createHttpClient();
var req = await(client.request(HttpMethod.GET, 8080, "localhost", "/"));
var resp = await(req.send());
var status = resp.statusCode();
var body = await(resp.body());
System.out.println("Got response status=" + status + " body='" + body + "'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class MovieRatingService extends AbstractVerticle {

public static void main(String[] args) throws Exception {
Vertx vertx = Vertx.vertx();
var vertx = Vertx.vertx();
vertx.deployVerticle(MovieRatingService.class, new DeploymentOptions().setThreadingModel(ThreadingModel.VIRTUAL_THREAD))
.toCompletionStage()
.toCompletableFuture()
Expand Down Expand Up @@ -50,12 +50,12 @@ public void start() {
.put("max_pool_size-loop", 30)
);

for (String statement : DB_STATEMENTS) {
DB_STATEMENTS.forEach(statement -> {
await(client.query(statement).execute());
}
});

// Build Vert.x Web router
Router router = Router.router(vertx);
var router = Router.router(vertx);
router.get("/movie/:id").handler(this::getMovie);
router.get("/rateMovie/:id").handler(this::rateMovie);
router.get("/getRating/:id").handler(this::getRating);
Expand All @@ -68,8 +68,8 @@ public void start() {

// Send info about a movie
private void getMovie(RoutingContext ctx) {
String id = ctx.pathParam("id");
RowSet<Row> rows = await(client.preparedQuery("SELECT TITLE FROM MOVIE WHERE ID=?").execute(Tuple.of(id)));
var id = ctx.pathParam("id");
var rows = await(client.preparedQuery("SELECT TITLE FROM MOVIE WHERE ID=?").execute(Tuple.of(id)));
if (rows.size() == 1) {
ctx.response()
.putHeader("Content-Type", "application/json")
Expand All @@ -84,8 +84,8 @@ private void getMovie(RoutingContext ctx) {

// Rate a movie
private void rateMovie(RoutingContext ctx) {
String movie = ctx.pathParam("id");
int rating = Integer.parseInt(ctx.queryParam("rating").get(0));
var movie = ctx.pathParam("id");
var rating = Integer.parseInt(ctx.queryParam("rating").get(0));
RowSet<Row> rows = await(client.preparedQuery("SELECT TITLE FROM MOVIE WHERE ID=?").execute(Tuple.of(movie)));
if (rows.size() == 1) {
await(client.preparedQuery("INSERT INTO RATING (VALUE, MOVIE_ID) VALUES ?, ?").execute(Tuple.of(rating, movie)));
Expand All @@ -97,8 +97,8 @@ private void rateMovie(RoutingContext ctx) {

// Get the current rating of a movie
private void getRating(RoutingContext ctx) {
String id = ctx.pathParam("id");
RowSet<Row> rows = await(client.preparedQuery("SELECT AVG(VALUE) AS VALUE FROM RATING WHERE MOVIE_ID=?").execute(Tuple.of(id)));
var id = ctx.pathParam("id");
var rows = await(client.preparedQuery("SELECT AVG(VALUE) AS VALUE FROM RATING WHERE MOVIE_ID=?").execute(Tuple.of(id)));
ctx.response()
.putHeader("Content-Type", "application/json")
.end(new JsonObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
public class SqlClientExample extends AbstractVerticle {

public static void main(String[] args) throws Exception {
Vertx vertx = Vertx.vertx();
try (PostgreSQLContainer<?> container = new PostgreSQLContainer<>()) {
var vertx = Vertx.vertx();
try (var container = new PostgreSQLContainer<>()) {
container.start();
vertx.deployVerticle(() -> {
PgConnectOptions connectOptions = new PgConnectOptions()
Expand Down Expand Up @@ -42,7 +42,7 @@ public void start() {
// insert some test data
await(pool.query("insert into test values (1, 'Hello'), (2, 'World')").execute());
// query some data
RowSet<Row> rows = await(pool.query("select * from test").execute());
var rows = await(pool.query("select * from test").execute());
for (Row row : rows) {
System.out.println("row = " + row.toJson());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class WebClientExample extends AbstractVerticle {

public static void main(String[] args) throws Exception {
Vertx vertx = Vertx.vertx();
var vertx = Vertx.vertx();
vertx.deployVerticle(WebClientExample.class, new DeploymentOptions()
.setThreadingModel(ThreadingModel.VIRTUAL_THREAD))
.toCompletionStage()
Expand All @@ -25,20 +25,20 @@ public static void main(String[] args) throws Exception {

@Override
public void start() {
HttpServer server = vertx.createHttpServer();
var server = vertx.createHttpServer();
server.requestHandler(request -> {
request.response().end("Hello World");
});
await(server.listen(8080, "localhost"));

// Make a simple HTTP request
WebClient client = WebClient.create(vertx);
HttpResponse<Buffer> resp = await(client
var client = WebClient.create(vertx);
var resp = await(client
.get(8080, "localhost", "/")
.expect(ResponsePredicate.SC_OK)
.send());
int status = resp.statusCode();
Buffer body = resp.body();
var status = resp.statusCode();
var body = resp.body();
System.out.println("Got response status=" + status + " body='" + body + "'");
}
}

0 comments on commit 0a8845a

Please sign in to comment.