Project Setup:

Gradle + basic Helo World
This commit is contained in:
Tina_Azure
2023-03-02 02:30:55 +01:00
parent 585f4de3a0
commit 1c0bdd23e8
14 changed files with 686 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package cavecomm;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.MultiMap;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
public class MainVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
// Create a Router
Router router = Router.router(vertx);
// Mount the handler for all incoming requests at every path and HTTP method
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()
)
);
}
}

View File

@ -0,0 +1,30 @@
package cavecomm;
import io.vertx.core.Vertx;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.RunTestOnContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(VertxUnitRunner.class)
public class TestMainVerticle {
@Rule
public RunTestOnContext rule = new RunTestOnContext();
@Before
public void deploy_verticle(TestContext testContext) {
Vertx vertx = rule.vertx();
vertx.deployVerticle(new MainVerticle(), testContext.asyncAssertSuccess());
}
@Test
public void verticle_deployed(TestContext testContext) throws Throwable {
Async async = testContext.async();
async.complete();
}
}