Example usage for io.vertx.core Vertx createHttpServer

List of usage examples for io.vertx.core Vertx createHttpServer

Introduction

In this page you can find the example usage for io.vertx.core Vertx createHttpServer.

Prototype

HttpServer createHttpServer(HttpServerOptions options);

Source Link

Document

Create an HTTP/HTTPS server using the specified options

Usage

From source file:examples.HTTP2Examples.java

License:Open Source License

public void example0(Vertx vertx) {
    HttpServerOptions options = new HttpServerOptions().setUseAlpn(true).setSsl(true)
            .setKeyStoreOptions(new JksOptions().setPath("/path/to/my/keystore"));

    HttpServer server = vertx.createHttpServer(options);
}

From source file:examples.HTTP2Examples.java

License:Open Source License

public void example17(Vertx vertx, HttpServerOptions http2Options) {
    HttpServer server = vertx.createHttpServer(http2Options);

    server.connectionHandler(connection -> {
        System.out.println("A client connected");
    });/*from   w  ww .ja  va2  s .c o m*/
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example2(Vertx vertx) {

    HttpServerOptions options = new HttpServerOptions().setMaxWebsocketFrameSize(1000000);

    HttpServer server = vertx.createHttpServer(options);
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void exampleServerLogging(Vertx vertx) {

    HttpServerOptions options = new HttpServerOptions().setLogActivity(true);

    HttpServer server = vertx.createHttpServer(options);
}

From source file:examples.NetExamples.java

License:Open Source License

public void example50(Vertx vertx) throws CertificateException {
    SelfSignedCertificate certificate = SelfSignedCertificate.create();

    vertx.createHttpServer(new HttpServerOptions().setSsl(true).setKeyCertOptions(certificate.keyCertOptions())
            .setTrustOptions(certificate.trustOptions())).requestHandler(req -> req.response().end("Hello!"))
            .listen(8080);/* w ww.j a  v  a2s . co  m*/
}

From source file:examples.StompServerExamples.java

License:Open Source License

public void example16(Vertx vertx) {
    StompServer server = StompServer.create(vertx, new StompServerOptions().setPort(-1) // Disable the TCP port, optional
            .setWebsocketBridge(true) // Enable the web socket support
            .setWebsocketPath("/stomp")) // Configure the web socket path, /stomp by default
            .handler(StompServerHandler.create(vertx));

    HttpServer http = vertx
            .createHttpServer(new HttpServerOptions().setWebsocketSubProtocols("v10.stomp, v11.stomp"))
            .websocketHandler(server.webSocketHandler()).listen(8080);
}

From source file:io.github.cdelmas.spike.vertx.Main.java

License:Apache License

public static void main(String[] args) {
    long time = System.currentTimeMillis();
    Json.mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    Vertx vertx = Vertx.vertx();

    Router router = Router.router(vertx);

    HelloResource helloResource = new HelloResource();
    router.get("/vertx/hello").produces("application/json").handler(helloResource::hello);
    router.route("/vertx/hello").method(HttpMethod.POST).handler(BodyHandler.create());
    router.post("/vertx/hello").consumes("application/json").handler(helloResource::createMessage);

    HttpServerOptions serverOptions = new HttpServerOptions().setPort(8085);
    HttpServer server = vertx.createHttpServer(serverOptions);
    server.requestHandler(router::accept).listen();
    System.out.println("started in " + (System.currentTimeMillis() - time) + " ms");
}

From source file:io.nonobot.core.impl.BotImpl.java

License:Apache License

public static Bot createShared(Vertx vertx, BotOptions options, Handler<AsyncResult<Void>> completionHandler) {
    BotImpl bot = bots.computeIfAbsent(new Key(vertx, options.getName()),
            key -> new BotImpl(vertx, options.getName()));
    HttpServer server;// w  ww .j a v  a  2s. c  o  m
    if (options.getHttpServerOptions() != null) {
        server = vertx.createHttpServer(options.getHttpServerOptions());
        server.requestHandler(bot.webRouter::accept);
        server.listen(ar -> {
            if (ar.succeeded()) {
                completionHandler.handle(Future.succeededFuture());
            } else {
                completionHandler.handle(Future.failedFuture(ar.cause()));
            }
        });
    } else {
        server = null;
        completionHandler.handle(Future.succeededFuture());
    }
    return new Bot() {
        @Override
        public Vertx vertx() {
            return bot.vertx();
        }

        @Override
        public ChatRouter chatRouter() {
            return bot.chatRouter();
        }

        @Override
        public Router webRouter() {
            return bot.webRouter();
        }

        @Override
        public String name() {
            return bot.name();
        }

        @Override
        public void close() {
            boolean open = bot.closed;
            bot.close();
            if (open && server != null) {
                server.close();
            }
        }
    };
}

From source file:net.sf.sgsimulator.sgsrest.SGSVertxServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    String configFile = null;//w w  w . j a v  a  2 s  .c o  m
    if (args.length == 0) {
        configFile = "config.json";
    } else {
        configFile = args[0];
    }

    Path configPath = Paths.get(configFile);
    JsonObject config = new JsonObject(new String(Files.readAllBytes(configPath)));

    Random r = new Random(new Date().getTime());
    char c1 = (char) (r.nextInt(26) + 'a');
    char c2 = (char) (r.nextInt(26) + 'a');
    char c3 = (char) (r.nextInt(26) + 'a');
    String admincode = "" + c1 + c2 + c3;
    System.getProperties().put("sgsimulator.admincode", admincode);

    Vertx vertx = Vertx.vertx();
    System.out.println("****************************");
    System.out.println("Starting...");
    System.out.println("To load pages:");
    System.out.println(
            "http://" + config.getString("host") + ":" + config.getInteger("port") + "/sg/pages/panel");
    System.out.println("Admin interface (only allowed from " + config.getString("adminip") + ":");
    System.out.println("http://" + config.getString("host") + ":" + config.getInteger("port")
            + "/sg/pages/admin/" + admincode);
    System.out.println("Projector screen interface (only allowed from " + config.getString("adminip") + ":");
    System.out.println(
            "http://" + config.getString("host") + ":" + config.getInteger("port") + "/sg/pages/screen");

    System.out.println("****************************");

    VertxNubes nubes = new VertxNubes(vertx, config);

    // System.err.println("IP:"+ip.substring(1));

    // config.put("host", ip.substring(1));
    System.getProperties().put("sgsimulator.scenario", config.getJsonObject("gridlab").getString("scenario"));
    System.getProperties().put("sgsimulator.adminip", config.getString("adminip"));
    nubes.bootstrap(res -> {
        if (res.succeeded()) {
            final Router router = res.result();
            final HttpServer server = vertx.createHttpServer(new HttpServerOptions(config));
            server.requestHandler(router::accept);
            server.listen();
            System.out.println("****************************");
            System.out.println("Started...");
            System.out.println("****************************");
        } else {
            res.cause().printStackTrace();
        }
    });
    // nubes.stop((_void) -> {
    // vertx.close();
    // });
}

From source file:net.udidb.server.driver.UdidbServer.java

License:Open Source License

public UdidbServer(String[] args) {
    // TODO process args to configure the server

    initializeLogging();/*from ww w  .ja  v  a 2s .c o  m*/

    String uiPath = System.getProperty("udidb.ui.path", "");
    boolean cors = Boolean.getBoolean("udidb.cors");

    Injector injector = Guice.createInjector(new ServerModule());

    Vertx vertx = injector.getInstance(Vertx.class);

    this.httpServer = vertx.createHttpServer(new HttpServerOptions().setWebsocketSubProtocols("wamp.2.json"));

    // WebSocket events
    this.httpServer.websocketHandler(websocket -> {
        if (!websocket.path().equals("/events")) {
            websocket.reject();
        }

        injector.getInstance(EventsSocket.class).setServerWebSocket(websocket);
    });

    Router router = Router.router(vertx);

    // static content for the UI
    StaticHandler staticHandler = StaticHandler.create();
    if (uiPath != null) {
        staticHandler.setAllowRootFileSystemAccess(true);
        staticHandler.setWebRoot(uiPath);
    } else {
        staticHandler.setWebRoot("webui");
    }
    router.route("/webui/*").handler(staticHandler);

    // API resources
    if (cors) {
        router.route().handler(CorsHandler.create("*").allowedHeader("Content-Type"));
    }

    router.route().handler(BodyHandler.create());

    DebuggeeContexts debuggeeContexts = injector.getInstance(DebuggeeContexts.class);

    router.get("/debuggeeContexts").blockingHandler(noParamHandler(debuggeeContexts::getAll));
    router.post("/debuggeeContexts").blockingHandler(bodyHandler(debuggeeContexts::create));
    router.options("/debuggeeContexts").blockingHandler(ok());
    router.get("/debuggeeContexts/operations")
            .blockingHandler(noParamHandler(debuggeeContexts::getOperationDescriptions));
    router.post("/debuggeeContexts/globalOperation")
            .blockingHandler(bodyHandler(debuggeeContexts::createGlobalOperation));
    router.options("/debuggeeContexts/globalOperation").blockingHandler(ok());
    router.get("/debuggeeContexts/:id").blockingHandler(pathParamHandler("id", debuggeeContexts::get));
    router.get("/debuggeeContexts/:id/process")
            .blockingHandler(pathParamHandler("id", debuggeeContexts::getProcess));
    router.get("/debuggeeContexts/:id/process/threads")
            .blockingHandler(pathParamHandler("id", debuggeeContexts::getThreads));
    router.get("/debuggeeContexts/:id/process/threads/:threadId")
            .blockingHandler(varPathParamHandler(debuggeeContexts::getThread, "id", "threadId"));
    router.post("/debuggeeContexts/:id/process/operation")
            .blockingHandler(bodyHandler("id", debuggeeContexts::createOperation));
    router.options("/debuggeeContexts/:id/process/operation").blockingHandler(ok());
    router.get("/debuggeeContexts/:id/process/operation")
            .blockingHandler(pathParamHandler("id", debuggeeContexts::getOperation));
    router.get("/debuggeeContexts/:id/process/operations")
            .blockingHandler(pathParamHandler("id", debuggeeContexts::getOperationDescriptions));

    httpServer.requestHandler(router::accept);
}