Project Setup:
Gradle + basic Helo World
This commit is contained in:
43
src/main/java/cavecomm/MainVerticle.java
Normal file
43
src/main/java/cavecomm/MainVerticle.java
Normal 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()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
30
src/test/java/cavecomm/TestMainVerticle.java
Normal file
30
src/test/java/cavecomm/TestMainVerticle.java
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user