Mod to test the Database functionality

This commit is contained in:
Tina_Azure
2023-03-08 20:22:29 +01:00
parent da76fa6eef
commit 02e5910c43

View File

@ -1,43 +1,72 @@
package cavecomm; package cavecomm;
import io.vertx.core.AbstractVerticle; import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap; import io.vertx.core.MultiMap;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router; import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.SqlClient;
public class MainVerticle extends AbstractVerticle { public class MainVerticle extends AbstractVerticle {
@Override @Override
public void start() throws Exception { public void start() throws Exception {
// Create a Router // Create a Router
Router router = Router.router(vertx); Router router = Router.router(vertx);
// Mount the handler for all incoming requests at every path and HTTP method
// Mount the handler for all incoming requests at every path and HTTP method Database db = new Database(
router.route().handler(context -> { vertx,
// Get the address of the request 5432,
String address = context.request().connection().remoteAddress().toString(); "localhost",
// Get the query parameter "name" "testdb",
MultiMap queryParams = context.queryParams(); "testuser",
String name = queryParams.contains("name") ? queryParams.get("name") : "unknown"; "123");
// Write a json response
context.json(
new JsonObject()
.put("cavecomm", name)
.put("address", address)
.put("message", "Hello World")
);
});
// Create the HTTP server //db.executeQuery("SELECT * FROM testtable");
vertx.createHttpServer()
// Handle every request using the router router.get("/jsonTest").handler(rc -> {
.requestHandler(router) db.executeQuery("SELECT * FROM testtable", rc);
// Start listening }
.listen(8888) );
// Print the port
.onSuccess(server ->
System.out.println(
"HTTP server started on port " + server.actualPort()
) router.route().handler(context -> {
); // Get the address of the request
} String address = context.request().connection().remoteAddress().toString();
} // Get the query parameter "name"
MultiMap queryParams = context.queryParams();
String name = queryParams.contains("name") ? queryParams.get("name") : "unknown";
// Write a json response
context.json(
new JsonObject()
.put("cavecomm", name)
.put("address", address)
.put("message", "Hello World")
);
});
// Create the HTTP server
vertx.createHttpServer()
// Handle every request using the router
.requestHandler(router)
// Start listening
.listen(8888)
// Print the port
.onSuccess(server ->
System.out.println(
"HTTP server started on port " + server.actualPort()
)
);
}
}