Exemple #1
0
  /**
   * Handles starting the server listening on all interfaces
   *
   * @return a callback to hit when ready for the server to open up listening
   */
  private Handler<Message<Object>> getStartupHandler() {
    return message -> {
      server = vertx.createHttpServer();
      Router router = Router.router(vertx);

      GraphApi graphApi = new GraphApi(session, vertx, registry);

      if (isInitializerThread()) {
        graphApi.outputApi(logger);
      }
      graphApi.init(router);

      server.requestHandler(router::accept);

      server.listen(
          svrConfig.port,
          "0.0.0.0",
          event -> {
            if (event.failed()) {
              logger.error("Failed to start server, error:", event.cause());
              stop();
            } else {
              logger.info("Thread: {} starting to handle request", Thread.currentThread().getId());
            }
          });
    };
  }
 private void createServer() {
   server = vertx.createHttpServer();
   Router router = Router.router(vertx);
   router.route().handler(BodyHandler.create());
   configureBasicRouter(router);
   server.requestHandler(router::accept).listen(PORT);
   System.out.println("Server " + this.getClass().getTypeName() + " is started.");
 }
  @Override
  public void start() throws Exception {

    HttpServer server = vertx.createHttpServer();
    server.requestHandler(
        new Handler<HttpServerRequest>() {

          @Override
          public void handle(HttpServerRequest request) {
            request
                .response()
                .sendFile(
                    '.' + (request.path().equals("/") ? "/resource/index.html" : request.path()));
          }
        });

    server.websocketHandler(
        new Handler<ServerWebSocket>() {

          @Override
          public void handle(final ServerWebSocket ws) {
            if (!ws.path().equals("/chat")) {
              ws.reject();
              return;
            }
            final LocalMap<Object, String> wsSessions =
                vertx.sharedData().getLocalMap("websocket.chat.sessions");
            if (wsSessions == null) {
              System.out.println("Deu erro!");
            }
            wsSessions.put(ws.textHandlerID(), ws.textHandlerID());

            ws.handler(
                new Handler<Buffer>() {

                  @Override
                  public void handle(Buffer buffer) {
                    for (String handlerId : wsSessions.values()) {
                      System.err.println("Sending " + handlerId);
                      vertx.eventBus().send(handlerId, buffer.toString());
                    }
                  }
                });

            ws.closeHandler(
                new Handler<Void>() {

                  @Override
                  public void handle(Void v) {
                    System.err.println("Socket closed. Removing" + ws.binaryHandlerID());
                    wsSessions.remove(ws.textHandlerID());
                  }
                });
          }
        });

    server.listen(8080);
  }
  @Override
  public void start() throws Exception {

    HttpServer server = vertx.createHttpServer();

    router = Router.router(vertx);

    // So we can use getBodyAsJson() and/or getBodyAsString() in our handling methods
    router.route().handler(BodyHandler.create());

    //		  router.post("/api/timeTest/namez").handler(this::PostChoir);

    RegisterRoutes();

    System.out.println("Now listening on port 3030");

    server.requestHandler(router::accept).listen(3030);
  }
  @Override
  public void start() throws Exception {
    // Creating web server and its router
    HttpServerOptions httpServerOptions =
        new HttpServerOptions()
            .setCompressionSupported(true) // enabling gzip and deflate compression
            .setPort(80); // web port
    HttpServer server = vertx.createHttpServer(httpServerOptions);
    Router router = Router.router(vertx);

    // Logging web requests
    router.route("/*").handler(LoggerHandler.create());

    // Serving static files under the webroot folder
    router.route("/*").handler(StaticHandler.create());

    // Binding the web port
    server.requestHandler(router::accept).listen();
  }
  // ---- run vertx server ---
  @Override
  public void start() throws Exception {
    // TODO Auto-generated method stub
    Set<String> scanPackages = new LinkedHashSet<String>();
    scanPackages.add("org.vsg");

    Router router = Router.router(vertx);

    /*
    Injector injector = Guice.createInjector(
    		new JsrRestControllerModule(router , runtimeClassLoader , scanPackages));
    		*/

    // JsrRestControllerModule  modInst =  injector.getInstance(JsrRestControllerModule.class);

    HttpServer httpServer = vertx.createHttpServer();

    httpServer.requestHandler(router::accept).listen(8090);
    logger.info("Startuped server on port : [8090]");
  }
Exemple #7
0
  public static void main(String[] args) {
    Vertx vertx = null;
    HttpServer server = Vertx.vertx().createHttpServer();

    Router router = Router.router(vertx);
    Route route = router.route().path("/some/path/");

    route.handler(
        routingContext -> {
          // This handler will be called for the following request paths:

          // `/some/path`
          // `/some/path/`
          // `/some/path//`
          //
          // but not:
          // `/some/path/subdir`

          // This handler will be called for every request
          HttpServerResponse response = routingContext.response();
          response.putHeader("content-type", "text/plain");

          // Write to the response and end it
          response.end("Hello World from Vert.x-Web! Some path, Simple REST");
        });
    router
        .route()
        .handler(
            routingContext -> {

              // This handler will be called for every request
              HttpServerResponse response = routingContext.response();
              response.putHeader("content-type", "text/plain");

              // Write to the response and end it
              response.end("Hello World from Vert.x-Web!");
            });

    server.requestHandler(router::accept).listen(8080);
  }
Exemple #8
0
  private Twinkle() {
    final Logger logger = LoggerFactory.getLogger(Twinkle.class);

    final Vertx vertx = Vertx.vertx(vertxOptions());
    final HttpServer httpServer = vertx.createHttpServer(httpServerOptions());
    final Router router = Router.router(vertx);

    router.get("/ping").handler(context -> context.response().end("pong"));
    router.mountSubRouter("/stats", new Metrics(vertx, httpServer).router());

    httpServer
        .requestHandler(router::accept)
        .listen(
            8080,
            result -> {
              if (result.succeeded()) {
                logger.info("Twinkle started");
              } else {
                logger.error("Twinkle failed to start", result.cause());
                vertx.close(shutdown -> logger.info("Twinkle shut down"));
              }
            });
  }