use of io.vertx.core.http.HttpServer in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("server-key.pem").setCertPath("server-cert.pem")));
server.requestHandler(req -> {
String path = req.path();
HttpServerResponse resp = req.response();
if ("/".equals(path)) {
resp.push(HttpMethod.GET, "/script.js", ar -> {
if (ar.succeeded()) {
System.out.println("sending push");
HttpServerResponse pushedResp = ar.result();
pushedResp.sendFile("script.js");
} else {
// Sometimes Safari forbids push : "Server push not allowed to opposite endpoint."
}
});
resp.sendFile("index.html");
} else if ("/script.js".equals(path)) {
resp.sendFile("script.js");
} else {
System.out.println("Not found " + path);
resp.setStatusCode(404).end();
}
});
server.listen(8443, "localhost", ar -> {
if (ar.succeeded()) {
System.out.println("Server started");
} else {
ar.cause().printStackTrace();
}
});
}
use of io.vertx.core.http.HttpServer in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("server-key.pem").setCertPath("server-cert.pem")));
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
req.customFrameHandler(frame -> {
System.out.println("Received client frame " + frame.payload().toString("UTF-8"));
// Write the sam
resp.writeCustomFrame(10, 0, Buffer.buffer("pong"));
});
}).listen(8443);
}
use of io.vertx.core.http.HttpServer in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
HttpServer server = vertx.createHttpServer(new HttpServerOptions());
server.requestHandler(req -> {
req.response().putHeader("content-type", "text/html").end("<html><body>" + "<h1>Hello from vert.x!</h1>" + "<p>version = " + req.version() + "</p>" + "</body></html>");
}).listen(8080);
}
use of io.vertx.core.http.HttpServer in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setSsl(true).setUseAlpn(true).setPemKeyCertOptions(new PemKeyCertOptions().setCertPath("server-cert.pem").setKeyPath("server-key.pem")));
server.requestHandler(req -> {
req.response().putHeader("content-type", "text/html").end("Hello using HTTP " + req.version());
});
server.listen(8443);
}
use of io.vertx.core.http.HttpServer in project vertx-examples by vert-x3.
the class ServerVerticle method start.
@Override
public void start() throws Exception {
super.start();
HttpServer server = vertx.createHttpServer();
server.requestHandler(req -> {
if (req.method() == HttpMethod.GET) {
req.response().setChunked(true);
if (req.path().equals("/products")) {
vertx.eventBus().<String>send(SpringDemoVerticle.ALL_PRODUCTS_ADDRESS, "", result -> {
if (result.succeeded()) {
req.response().setStatusCode(200).write(result.result().body()).end();
} else {
req.response().setStatusCode(500).write(result.cause().toString()).end();
}
});
} else {
req.response().setStatusCode(200).write("Hello from vert.x").end();
}
} else {
// We only support GET for now
req.response().setStatusCode(405).end();
}
});
server.listen(8080);
}
Aggregations