Search in sources :

Example 1 with Router

use of io.vertx.rxjava.ext.web.Router in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    router.route("/news-feed/*").handler(SockJSHandler.create(vertx).socketHandler(sockJSSocket -> {
        // Consumer the event bus address as an Observable
        Observable<String> msg = vertx.eventBus().<String>consumer("news-feed").bodyStream().toObservable();
        // Send the event to the client
        Subscription subscription = msg.subscribe(sockJSSocket::write);
        // Unsubscribe when the socket closes
        sockJSSocket.endHandler(v -> {
            subscription.unsubscribe();
        });
    }));
    // Serve the static resources
    router.route().handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    // Publish a message to the address "news-feed" every second
    vertx.setPeriodic(1000, t -> vertx.eventBus().publish("news-feed", "news from the server!"));
}
Also used : Router(io.vertx.rxjava.ext.web.Router) StaticHandler(io.vertx.rxjava.ext.web.handler.StaticHandler) SockJSHandler(io.vertx.rxjava.ext.web.handler.sockjs.SockJSHandler) Runner(io.vertx.example.util.Runner) Subscription(rx.Subscription) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) Observable(rx.Observable) Router(io.vertx.rxjava.ext.web.Router) Subscription(rx.Subscription) Observable(rx.Observable)

Example 2 with Router

use of io.vertx.rxjava.ext.web.Router in project vertx-openshift-it by cescoffier.

the class GatewayVerticle method start.

@Override
public void start() throws Exception {
    dnsClient = vertx.createHttpClient(new HttpClientOptions().setDefaultHost(ENDPOINT_NAME).setDefaultPort(8080).setKeepAlive(false));
    dnsWeb = WebClient.create(vertx, new WebClientOptions().setDefaultHost(ENDPOINT_NAME).setDefaultPort(8080).setKeepAlive(false));
    dnsDatabase = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:postgresql://my-database:5432/my_data").put("driver_class", "org.postgresql.Driver").put("user", "luke").put("password", "secret"));
    Router router = Router.router(vertx);
    router.get("/health").handler(rc -> rc.response().end("OK"));
    router.get("/services/http").handler(this::invokeHttpService);
    router.get("/services/web").handler(this::invokeWebService);
    router.get("/services/db").handler(this::checkDb);
    router.get("/dns/http").handler(this::invokeHttpServiceWithDns);
    router.get("/dns/web").handler(this::invokeWebServiceWithDns);
    router.get("/dns/db").handler(this::checkDbWithDns);
    router.get("/ref/http").handler(this::invokeHttpServiceWithRef);
    router.get("/ref/web").handler(this::invokeWebServiceWithRef);
    router.put("/webclient").handler(this::webclient);
    ServiceDiscovery.create(vertx, discovery -> {
        this.discovery = discovery;
        Single<WebClient> svc1 = HttpEndpoint.rxGetWebClient(discovery, svc -> svc.getName().equals(ENDPOINT_NAME), new JsonObject().put("keepAlive", false));
        Single<HttpClient> svc2 = HttpEndpoint.rxGetClient(discovery, svc -> svc.getName().equals(ENDPOINT_NAME), new JsonObject().put("keepAlive", false));
        Single<JDBCClient> svc3 = JDBCDataSource.rxGetJDBCClient(discovery, svc -> svc.getName().equals("my-database"), new JsonObject().put("url", "jdbc:postgresql://my-database:5432/my_data").put("driver_class", "org.postgresql.Driver").put("user", "luke").put("password", "secret"));
        Single.zip(svc1, svc2, svc3, (wc, hc, db) -> {
            this.web = wc;
            this.client = hc;
            this.database = db;
            return vertx.createHttpServer().requestHandler(router::accept).listen(8080);
        }).subscribe();
    });
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpResponse(io.vertx.rxjava.ext.web.client.HttpResponse) Router(io.vertx.rxjava.ext.web.Router) RoutingContext(io.vertx.rxjava.ext.web.RoutingContext) HttpEndpoint(io.vertx.rxjava.servicediscovery.types.HttpEndpoint) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) WebClient(io.vertx.rxjava.ext.web.client.WebClient) HttpClient(io.vertx.rxjava.core.http.HttpClient) Single(rx.Single) JDBCDataSource(io.vertx.rxjava.servicediscovery.types.JDBCDataSource) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) BodyCodec(io.vertx.rxjava.ext.web.codec.BodyCodec) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpClient(io.vertx.rxjava.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.rxjava.ext.web.Router) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) WebClient(io.vertx.rxjava.ext.web.client.WebClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 3 with Router

use of io.vertx.rxjava.ext.web.Router in project vertx-openshift-it by cescoffier.

the class MySQLVerticle method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    router.route().handler(BodyHandler.create());
    router.route("/api/vegetables/:id").handler(this::validateId);
    router.get("/api/vegetables").handler(this::getAll);
    router.post("/api/vegetables").handler(this::addOne);
    router.get("/api/vegetables/:id").handler(this::getOne);
    router.put("/api/vegetables/:id").handler(this::updateOne);
    router.delete("/api/vegetables/:id").handler(this::deleteOne);
    router.get("/healthcheck").handler(rc -> rc.response().end("OK"));
    JsonObject config = TestUtils.allocateDatabase("mysql", isExternalDB);
    JDBCClient jdbcClient = JDBCClient.createShared(vertx, config);
    initDatabase(vertx, jdbcClient).andThen(initHttpServer(router, jdbcClient)).subscribe((http) -> System.out.println("Server ready on port " + http.actualPort()), Throwable::printStackTrace);
}
Also used : Router(io.vertx.rxjava.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

Example 4 with Router

use of io.vertx.rxjava.ext.web.Router in project vertx-openshift-it by cescoffier.

the class PostgreSQLVerticle method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    router.route().handler(BodyHandler.create());
    router.route("/api/vegetables/:id").handler(this::validateId);
    router.get("/api/vegetables").handler(this::getAll);
    router.post("/api/vegetables").handler(this::addOne);
    router.get("/api/vegetables/:id").handler(this::getOne);
    router.put("/api/vegetables/:id").handler(this::updateOne);
    router.delete("/api/vegetables/:id").handler(this::deleteOne);
    router.get("/healthcheck").handler(rc -> rc.response().end("OK"));
    JsonObject config = TestUtils.allocateDatabase("postgresql", isExternalDB);
    JDBCClient jdbcClient = JDBCClient.createShared(vertx, config);
    initDatabase(vertx, jdbcClient).andThen(initHttpServer(router, jdbcClient)).subscribe((http) -> System.out.println("Server ready on port " + http.actualPort()), Throwable::printStackTrace);
}
Also used : Router(io.vertx.rxjava.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

Example 5 with Router

use of io.vertx.rxjava.ext.web.Router in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    router.route().handler(routingContext -> {
        routingContext.response().putHeader("content-type", "text/html").end("Hello World!");
    });
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
Also used : Router(io.vertx.rxjava.ext.web.Router)

Aggregations

Router (io.vertx.rxjava.ext.web.Router)8 JsonObject (io.vertx.core.json.JsonObject)5 JDBCClient (io.vertx.rxjava.ext.jdbc.JDBCClient)4 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)2 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)2 CircuitBreakerOptions (io.vertx.circuitbreaker.CircuitBreakerOptions)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 Runner (io.vertx.example.util.Runner)1 HttpClient (io.vertx.rxjava.core.http.HttpClient)1 RoutingContext (io.vertx.rxjava.ext.web.RoutingContext)1 HttpResponse (io.vertx.rxjava.ext.web.client.HttpResponse)1 WebClient (io.vertx.rxjava.ext.web.client.WebClient)1 BodyCodec (io.vertx.rxjava.ext.web.codec.BodyCodec)1 StaticHandler (io.vertx.rxjava.ext.web.handler.StaticHandler)1 SockJSHandler (io.vertx.rxjava.ext.web.handler.sockjs.SockJSHandler)1 ServiceDiscovery (io.vertx.rxjava.servicediscovery.ServiceDiscovery)1 HttpEndpoint (io.vertx.rxjava.servicediscovery.types.HttpEndpoint)1 JDBCDataSource (io.vertx.rxjava.servicediscovery.types.JDBCDataSource)1 Completable (rx.Completable)1 Observable (rx.Observable)1