Divided "backend" verticles in api and db
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package net.miarma.contaminus.clase;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.Promise;
|
||||
|
||||
public class BroadcastVerticle extends AbstractVerticle {
|
||||
@Override
|
||||
public void start(Promise<Void> promise) {
|
||||
vertx.setPeriodic(2000, _a -> {
|
||||
vertx.eventBus().publish("broadcast.addr", "Ola");
|
||||
});
|
||||
try {
|
||||
promise.complete();
|
||||
} catch (Exception e) {
|
||||
promise.fail(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package net.miarma.contaminus.clase;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.eventbus.Message;
|
||||
import net.miarma.contaminus.common.Constants;
|
||||
|
||||
public class ConsumerVerticle1 extends AbstractVerticle {
|
||||
@Override
|
||||
public void start(Promise<Void> promise) {
|
||||
vertx.eventBus().consumer("broadcast.addr", this::handleMsg);
|
||||
try {
|
||||
promise.complete();
|
||||
} catch (Exception e) {
|
||||
promise.fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMsg(Message<String> msg) {
|
||||
Constants.LOGGER.info("Ola Broadcast soy Consumer1");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package net.miarma.contaminus.clase;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.eventbus.Message;
|
||||
import net.miarma.contaminus.common.Constants;
|
||||
|
||||
public class ConsumerVerticle2 extends AbstractVerticle {
|
||||
@Override
|
||||
public void start(Promise<Void> promise) {
|
||||
vertx.eventBus().consumer("broadcast.addr", this::handleMsg);
|
||||
try {
|
||||
promise.complete();
|
||||
} catch (Exception e) {
|
||||
promise.fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMsg(Message<String> msg) {
|
||||
Constants.LOGGER.info("Ola Broadcast soy Consumer2");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package net.miarma.contaminus.clase;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.Promise;
|
||||
import net.miarma.contaminus.common.Constants;
|
||||
|
||||
public class MainVerticleClase extends AbstractVerticle {
|
||||
@Override
|
||||
public void start(Promise<Void> promise) {
|
||||
vertx.deployVerticle(new BroadcastVerticle(), result -> {
|
||||
if(result.succeeded()) {
|
||||
Constants.LOGGER.info("📡 BroadcastVerticle desplegado");
|
||||
} else {
|
||||
Constants.LOGGER.error("❌ Error al desplegar BroadcastVerticle", result.cause());
|
||||
}
|
||||
});
|
||||
vertx.deployVerticle(new ConsumerVerticle1(), result -> {
|
||||
if(result.succeeded()) {
|
||||
Constants.LOGGER.info("📡 ConsumerVerticle1 desplegado");
|
||||
} else {
|
||||
Constants.LOGGER.error("❌ Error al desplegar ConsumerVerticle1", result.cause());
|
||||
}
|
||||
});
|
||||
vertx.deployVerticle(new ConsumerVerticle2(), result -> {
|
||||
if(result.succeeded()) {
|
||||
Constants.LOGGER.info("📡 ConsumerVerticle2 desplegado");
|
||||
} else {
|
||||
Constants.LOGGER.error("❌ Error al desplegar ConsumerVerticle2", result.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package net.miarma.contaminus.server;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.eventbus.DeliveryOptions;
|
||||
import io.vertx.core.eventbus.Message;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import net.miarma.contaminus.common.Constants;
|
||||
import net.miarma.contaminus.database.QueryBuilder;
|
||||
|
||||
public class ApiVerticle extends AbstractVerticle {
|
||||
@Override
|
||||
public void start(Promise<Void> startPromise) {
|
||||
Constants.LOGGER.info("🟢 Iniciando ApiVerticle...");
|
||||
Router router = Router.router(vertx);
|
||||
router.get(Constants.API_PREFIX + "/sensors").blockingHandler(this::getAllSensors);
|
||||
router.get(Constants.API_PREFIX + "/sensors/:id").blockingHandler(this::getSensorById);
|
||||
router.get(Constants.API_PREFIX + "/status").handler(ctx ->
|
||||
ctx.json(new JsonObject().put("status", "OK"))
|
||||
);
|
||||
vertx.createHttpServer().requestHandler(router).listen(8081);
|
||||
startPromise.complete();
|
||||
}
|
||||
|
||||
private void getAllSensors(RoutingContext context) {
|
||||
Optional<String> sort = Optional.ofNullable(context.request().getParam("_sort"));
|
||||
Optional<String> order = Optional.ofNullable(context.request().getParam("_order"));
|
||||
// forma tela de rara que me ha dado chatgpt para parsear esto XD
|
||||
Optional<Integer> limit = Optional.ofNullable(context.request().getParam("_limit"))
|
||||
.map(s -> {
|
||||
try {
|
||||
return Integer.parseInt(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
String query = QueryBuilder
|
||||
.select("*")
|
||||
.from("sensor_mq_data")
|
||||
.orderBy(sort, order)
|
||||
.limit(limit)
|
||||
.build();
|
||||
|
||||
vertx.eventBus().request("db.query", query, new DeliveryOptions(), ar -> {
|
||||
if (ar.succeeded()) {
|
||||
Message<Object> result = ar.result();
|
||||
JsonArray jsonArray = (JsonArray) result.body();
|
||||
context.json(jsonArray);
|
||||
} else {
|
||||
context.fail(500, ar.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void getSensorById(RoutingContext context) {
|
||||
String id = context.request().getParam("id");
|
||||
|
||||
String query = QueryBuilder
|
||||
.select("*")
|
||||
.from("sensor_mq_data")
|
||||
.where("id = " + id)
|
||||
.build();
|
||||
|
||||
vertx.eventBus().request("db.query", query, new DeliveryOptions(), ar -> {
|
||||
if (ar.succeeded()) {
|
||||
Message<Object> result = ar.result();
|
||||
JsonArray jsonArray = (JsonArray) result.body();
|
||||
context.json(jsonArray);
|
||||
} else {
|
||||
context.fail(500, ar.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -37,8 +37,6 @@ public class DatabaseVerticle extends AbstractVerticle {
|
||||
});
|
||||
|
||||
eventBus.consumer("db.query", this::handleDatabaseQuery);
|
||||
|
||||
Constants.LOGGER.info("📡 DatabaseVerticle desplegado.");
|
||||
}
|
||||
|
||||
private void handleDatabaseQuery(Message<String> msg) {
|
||||
|
||||
@@ -1,91 +1,18 @@
|
||||
package net.miarma.contaminus.server;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.eventbus.DeliveryOptions;
|
||||
import io.vertx.core.eventbus.Message;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.handler.StaticHandler;
|
||||
import net.miarma.contaminus.common.Constants;
|
||||
import net.miarma.contaminus.database.QueryBuilder;
|
||||
|
||||
public class HttpServerVerticle extends AbstractVerticle {
|
||||
@Override
|
||||
public void start() {
|
||||
Constants.LOGGER.info("🟢 Iniciando HttpServerVerticle...");
|
||||
|
||||
Router router = Router.router(vertx);
|
||||
router.route("/*").handler(StaticHandler.create("webroot").setDefaultContentEncoding("UTF-8"));
|
||||
|
||||
router.get(Constants.API_PREFIX + "/sensors").blockingHandler(this::getAllSensors);
|
||||
router.get(Constants.API_PREFIX + "/sensors/:id").blockingHandler(this::getSensorById);
|
||||
router.get(Constants.API_PREFIX + "/status").handler(ctx ->
|
||||
ctx.json(new JsonObject().put("status", "OK"))
|
||||
);
|
||||
|
||||
vertx.createHttpServer().requestHandler(router).listen(80, response -> {
|
||||
if (response.succeeded()) {
|
||||
Constants.LOGGER.info("🚀 Servidor HTTP desplegado en http://localhost:80");
|
||||
} else {
|
||||
Constants.LOGGER.error("❌ Error al desplegar el servidor HTTP", response.cause());
|
||||
}
|
||||
});
|
||||
vertx.createHttpServer().requestHandler(router).listen(8080);
|
||||
}
|
||||
|
||||
private void getAllSensors(RoutingContext context) {
|
||||
Optional<String> sort = Optional.ofNullable(context.request().getParam("_sort"));
|
||||
Optional<String> order = Optional.ofNullable(context.request().getParam("_order"));
|
||||
// forma tela de rara que me ha dado chatgpt para parsear esto XD
|
||||
Optional<Integer> limit = Optional.ofNullable(context.request().getParam("_limit"))
|
||||
.map(s -> {
|
||||
try {
|
||||
return Integer.parseInt(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
String query = QueryBuilder
|
||||
.select("*")
|
||||
.from("sensor_mq_data")
|
||||
.orderBy(sort, order)
|
||||
.limit(limit)
|
||||
.build();
|
||||
|
||||
vertx.eventBus().request("db.query", query, new DeliveryOptions(), ar -> {
|
||||
if (ar.succeeded()) {
|
||||
Message<Object> result = ar.result();
|
||||
JsonArray jsonArray = (JsonArray) result.body();
|
||||
context.json(jsonArray);
|
||||
} else {
|
||||
context.fail(500, ar.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void getSensorById(RoutingContext context) {
|
||||
String id = context.request().getParam("id");
|
||||
|
||||
String query = QueryBuilder
|
||||
.select("*")
|
||||
.from("sensor_mq_data")
|
||||
.where("id = " + id)
|
||||
.build();
|
||||
|
||||
vertx.eventBus().request("db.query", query, new DeliveryOptions(), ar -> {
|
||||
if (ar.succeeded()) {
|
||||
Message<Object> result = ar.result();
|
||||
JsonArray jsonArray = (JsonArray) result.body();
|
||||
context.json(jsonArray);
|
||||
} else {
|
||||
context.fail(500, ar.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,39 @@
|
||||
package net.miarma.contaminus.server;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.DeploymentOptions;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.ThreadingModel;
|
||||
import net.miarma.contaminus.common.Constants;
|
||||
|
||||
public class MainVerticle extends AbstractVerticle {
|
||||
|
||||
@Override
|
||||
public void start(Promise<Void> startPromise) {
|
||||
getVertx().deployVerticle(new DatabaseVerticle());
|
||||
getVertx().deployVerticle(new HttpServerVerticle());
|
||||
final DeploymentOptions options = new DeploymentOptions();
|
||||
options.setThreadingModel(ThreadingModel.WORKER);
|
||||
|
||||
getVertx().deployVerticle(new DatabaseVerticle(), options, result -> {
|
||||
if(result.succeeded()) {
|
||||
Constants.LOGGER.info("📡 HttpServerVerticle desplegado.(http://localhost:8080)");
|
||||
} else {
|
||||
Constants.LOGGER.error("❌ Error al desplegar HttpServerVerticle", result.cause());
|
||||
}
|
||||
});
|
||||
getVertx().deployVerticle(new ApiVerticle(), options, result -> {
|
||||
if(result.succeeded()) {
|
||||
Constants.LOGGER.info("📡 ApiVerticle desplegado. (http://localhost:8081/api/v1)");
|
||||
} else {
|
||||
Constants.LOGGER.error("❌ Error al desplegar ApiVerticle", result.cause());
|
||||
}
|
||||
});
|
||||
getVertx().deployVerticle(new HttpServerVerticle(), result -> {
|
||||
if(result.succeeded()) {
|
||||
Constants.LOGGER.info("📡 DatabaseVerticle desplegado.");
|
||||
} else {
|
||||
Constants.LOGGER.error("❌ Error al desplegar HttpServerVerticle", result.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
},
|
||||
"appConfig": {
|
||||
"endpoints": {
|
||||
"baseUrl": "http://localhost:80/api/v1",
|
||||
"baseUrl": "http://localhost:8081/api/v1",
|
||||
"sensors": "sensors",
|
||||
"sensor": "sensors/sensor"
|
||||
},
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
},
|
||||
"appConfig": {
|
||||
"endpoints": {
|
||||
"baseUrl": "http://localhost:80/api/v1",
|
||||
"baseUrl": "http://localhost:8081/api/v1",
|
||||
"sensors": "sensors",
|
||||
"sensor": "sensors/sensor"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user