package io.mainada.server;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.rxjava.core.AbstractVerticle;
import io.vertx.rxjava.core.buffer.Buffer;
import io.vertx.rxjava.core.eventbus.Message;
import io.vertx.rxjava.core.http.HttpServer;
import io.vertx.rxjava.core.http.HttpServerRequest;
import io.vertx.rxjava.core.http.HttpServerResponse;
import io.vertx.rxjava.ext.web.Router;
import io.vertx.rxjava.ext.web.handler.CorsHandler;
import rx.Single;

public class Server extends AbstractVerticle {

    private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
    private static final int PORT = 8080;

    @Override
    public void start() {
        HttpServer server = vertx.createHttpServer();

        final Router router = Router.router(vertx);

        router.route().handler(CorsHandler.create("*"));

        server.requestHandler(router::accept);

        server.requestStream()
                .toObservable()
                .subscribe(this::handleRequest);

        server.rxListen(PORT)
                .subscribe(
                        success -> LOGGER.info("Server started successfully."),
                        error -> LOGGER.error("Server failed to start.", error)
                );
    }

    private void handleRequest(final HttpServerRequest req) {
        LOGGER.info("Received cell.");

        final HttpServerResponse resp = req.response()
                .setChunked(true);

        req.toObservable()
                .map(Buffer::toJsonObject)
                .map(this::sendEventBusMessage)
                .flatMap(Single::toObservable)
                .map(Message::body)
                .map(JsonObject::encode)
                .subscribe(
                        resp::write,
                        error -> LOGGER.error("Failed to handle request.", error),
                        resp::end
                );
    }

    private Single<Message<JsonObject>> sendEventBusMessage(final JsonObject body) {
        final JsonArray parameters = body.getJsonArray("parameters");
        final String function = body.getString("function");
        return vertx.eventBus().rxSend(function, parameters);
    }
}

@@ -402,21 +402,22 @@
 ServerRe
-quest
+sponse
 ;%0Aimport
@@ -437,36 +437,22 @@
 ava.
-core.http.HttpServerResponse
+ext.web.Router
 ;%0Aim
@@ -476,34 +476,42 @@
 ava.ext.web.Rout
-er
+ingContext
 ;%0Aimport io.vert
@@ -943,25 +943,24 @@
 (%22*%22));%0A
-%0A
         
 server.r
@@ -955,169 +955,134 @@
     
-server.requestHandler(router::accept);%0A%0A        server.requestStream()%0A                .toObservable()%0A                .subscribe(this::handleRequest);%0A%0A
+router.route().handler(a -%3E handleRequest(a));%0A%0A        server%0A                .requestHandler(router::accept)%0A       
         
 serv
@@ -1077,22 +1077,17 @@
         
-server
+ 
 .rxListe
@@ -1349,24 +1349,21 @@
 nal 
-HttpServerReques
+RoutingContex
 t re
@@ -1510,16 +1510,26 @@
     req.
+request().
 toObserv

Enable cors with vertx-web