Search in sources :

Example 51 with HttpServer

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();
        }
    });
}
Also used : PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Example 52 with HttpServer

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);
}
Also used : OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) Buffer(io.vertx.core.buffer.Buffer) HttpServerResponse(io.vertx.core.http.HttpServerResponse) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Runner(io.vertx.example.util.Runner) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Example 53 with HttpServer

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);
}
Also used : AbstractVerticle(io.vertx.core.AbstractVerticle) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Runner(io.vertx.example.util.Runner) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Example 54 with HttpServer

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);
}
Also used : PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Example 55 with HttpServer

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);
}
Also used : HttpServer(io.vertx.core.http.HttpServer)

Aggregations

HttpServer (io.vertx.core.http.HttpServer)82 Router (io.vertx.ext.web.Router)37 HttpServerOptions (io.vertx.core.http.HttpServerOptions)32 Test (org.junit.Test)24 JsonObject (io.vertx.core.json.JsonObject)17 Buffer (io.vertx.core.buffer.Buffer)14 HttpServerResponse (io.vertx.core.http.HttpServerResponse)14 Future (io.vertx.core.Future)12 HttpClient (io.vertx.core.http.HttpClient)12 Vertx (io.vertx.core.Vertx)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 Handler (io.vertx.core.Handler)9 HttpMethod (io.vertx.core.http.HttpMethod)9 VertxOptions (io.vertx.core.VertxOptions)8 HttpClientOptions (io.vertx.core.http.HttpClientOptions)8 List (java.util.List)8 AbstractVerticle (io.vertx.core.AbstractVerticle)7 PemKeyCertOptions (io.vertx.core.net.PemKeyCertOptions)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 AsyncResult (io.vertx.core.AsyncResult)6