FIN
This commit is contained in:
@@ -55,11 +55,11 @@
|
|||||||
<version>2.12.1</version>
|
<version>2.12.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/io.quarkus/quarkus-agroal -->
|
<!-- https://mvnrepository.com/artifact/io.quarkus/quarkus-agroal -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.quarkus</groupId>
|
<groupId>org.jboss.logmanager</groupId>
|
||||||
<artifactId>quarkus-agroal</artifactId>
|
<artifactId>jboss-logmanager</artifactId>
|
||||||
<version>3.19.3</version>
|
<version>3.1.1.Final</version> <!-- O la versión más reciente -->
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ import io.vertx.core.impl.logging.LoggerFactory;
|
|||||||
|
|
||||||
public class Constants {
|
public class Constants {
|
||||||
public static final String APP_NAME = "ContaminUS";
|
public static final String APP_NAME = "ContaminUS";
|
||||||
public static final int API_VERSION = 1;
|
public static final String API_PREFIX = "/api/v1";
|
||||||
public static final String API_PREFIX = "/api/v" + Constants.API_VERSION;
|
|
||||||
public static Logger LOGGER = LoggerFactory.getLogger(Constants.APP_NAME);
|
public static Logger LOGGER = LoggerFactory.getLogger(Constants.APP_NAME);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,34 +22,20 @@ import net.miarma.contaminus.database.DatabaseManager;
|
|||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class DataLayerAPIVerticle extends AbstractVerticle {
|
public class DataLayerAPIVerticle extends AbstractVerticle {
|
||||||
private JDBCPool pool;
|
private JDBCPool pool;
|
||||||
private DatabaseManager dbManager;
|
private DatabaseManager dbManager;
|
||||||
private ConfigManager configManager;
|
private ConfigManager configManager;
|
||||||
|
|
||||||
|
public DataLayerAPIVerticle(JDBCPool pool) {
|
||||||
public DataLayerAPIVerticle() {
|
this.pool = pool;
|
||||||
JDBCConnectOptions connectOptions = new JDBCConnectOptions()
|
this.configManager = ConfigManager.getInstance();
|
||||||
.setJdbcUrl(
|
|
||||||
"jdbc:mariadb://" + configManager.getStringProperty("db.host") +
|
|
||||||
":" + configManager.getStringProperty("db.port") + "/"
|
|
||||||
)
|
|
||||||
.setDatabase(configManager.getStringProperty("db.name"))
|
|
||||||
.setUser(configManager.getStringProperty("db.user"))
|
|
||||||
.setPassword(configManager.getStringProperty("db.pwd"));
|
|
||||||
|
|
||||||
PoolOptions poolOptions = new PoolOptions().setMaxSize(5);
|
|
||||||
|
|
||||||
pool = JDBCPool.pool(vertx, connectOptions, poolOptions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) {
|
public void start(Promise<Void> startPromise) {
|
||||||
dbManager = DatabaseManager.getInstance(pool);
|
|
||||||
configManager = ConfigManager.getInstance();
|
|
||||||
|
|
||||||
Constants.LOGGER.info("📡 Iniciando DataLayerAPIVerticle...");
|
Constants.LOGGER.info("📡 Iniciando DataLayerAPIVerticle...");
|
||||||
|
dbManager = DatabaseManager.getInstance(pool);
|
||||||
|
|
||||||
|
|
||||||
Router router = Router.router(vertx);
|
Router router = Router.router(vertx);
|
||||||
Set<HttpMethod> allowedMethods = new HashSet<>(
|
Set<HttpMethod> allowedMethods = new HashSet<>(
|
||||||
Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.OPTIONS)); // Por ejemplo
|
Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.OPTIONS)); // Por ejemplo
|
||||||
@@ -85,21 +71,6 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
|
|||||||
router.route(HttpMethod.POST, Constants.POST_ACTUATORS).handler(this::addActuator);
|
router.route(HttpMethod.POST, Constants.POST_ACTUATORS).handler(this::addActuator);
|
||||||
router.route(HttpMethod.PUT, Constants.PUT_ACTUATOR_BY_ID).handler(this::updateActuator);
|
router.route(HttpMethod.PUT, Constants.PUT_ACTUATOR_BY_ID).handler(this::updateActuator);
|
||||||
|
|
||||||
dbManager.testConnection()
|
|
||||||
.onSuccess(_rows -> {
|
|
||||||
Constants.LOGGER.info("✅ Database connection ok");
|
|
||||||
vertx.createHttpServer()
|
|
||||||
.requestHandler(router)
|
|
||||||
.listen(configManager.getDataApiPort(), configManager.getHost());
|
|
||||||
startPromise.complete();
|
|
||||||
})
|
|
||||||
.onFailure(onFailure -> {
|
|
||||||
Constants.LOGGER.error("❌ Database connection failed");
|
|
||||||
Throwable t = onFailure.getCause();
|
|
||||||
t.printStackTrace();
|
|
||||||
startPromise.fail(onFailure);
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getAllGroups(RoutingContext context) {
|
private void getAllGroups(RoutingContext context) {
|
||||||
|
|||||||
@@ -17,10 +17,12 @@ import net.miarma.contaminus.common.Constants;
|
|||||||
public class LogicLayerAPIVerticle extends AbstractVerticle {
|
public class LogicLayerAPIVerticle extends AbstractVerticle {
|
||||||
private ConfigManager configManager;
|
private ConfigManager configManager;
|
||||||
|
|
||||||
|
public LogicLayerAPIVerticle() {
|
||||||
|
this.configManager = ConfigManager.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) {
|
public void start(Promise<Void> startPromise) {
|
||||||
configManager = ConfigManager.getInstance();
|
|
||||||
|
|
||||||
Constants.LOGGER.info("📡 Iniciando LogicApiVerticle...");
|
Constants.LOGGER.info("📡 Iniciando LogicApiVerticle...");
|
||||||
|
|
||||||
Router router = Router.router(vertx);
|
Router router = Router.router(vertx);
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import java.io.InputStream;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
|
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
import io.vertx.jdbcclient.JDBCPool;
|
||||||
import io.vertx.core.AbstractVerticle;
|
import io.vertx.core.AbstractVerticle;
|
||||||
import io.vertx.core.DeploymentOptions;
|
import io.vertx.core.DeploymentOptions;
|
||||||
import io.vertx.core.Launcher;
|
import io.vertx.core.Launcher;
|
||||||
@@ -15,78 +17,105 @@ import net.miarma.contaminus.common.ConfigManager;
|
|||||||
import net.miarma.contaminus.common.Constants;
|
import net.miarma.contaminus.common.Constants;
|
||||||
|
|
||||||
public class MainVerticle extends AbstractVerticle {
|
public class MainVerticle extends AbstractVerticle {
|
||||||
static ConfigManager configManager;
|
private ConfigManager configManager;
|
||||||
|
private JDBCPool pool;
|
||||||
public static void main(String[] args) {
|
|
||||||
System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
|
public static void main(String[] args) {
|
||||||
init();
|
|
||||||
Launcher.executeCommand("run", MainVerticle.class.getName());
|
Launcher.executeCommand("run", MainVerticle.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void init() {
|
@SuppressWarnings("deprecation")
|
||||||
configManager = ConfigManager.getInstance();
|
private void init() {
|
||||||
|
configManager = ConfigManager.getInstance();
|
||||||
|
|
||||||
|
String jdbcUrl = configManager.getJdbcUrl();
|
||||||
|
String dbUser = configManager.getStringProperty("db.user");
|
||||||
|
String dbPwd = configManager.getStringProperty("db.pwd");
|
||||||
|
Integer poolSize = configManager.getIntProperty("db.poolSize");
|
||||||
|
|
||||||
|
JsonObject dbConfig = new JsonObject()
|
||||||
|
.put("url", jdbcUrl)
|
||||||
|
.put("user", dbUser)
|
||||||
|
.put("password", dbPwd)
|
||||||
|
.put("max_pool_size", poolSize != null ? poolSize : 10);
|
||||||
|
|
||||||
|
pool = JDBCPool.pool(vertx, dbConfig);
|
||||||
|
|
||||||
initializeDirectories();
|
initializeDirectories();
|
||||||
copyDefaultConfig();
|
copyDefaultConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void initializeDirectories() {
|
private void initializeDirectories() {
|
||||||
File baseDir = new File(configManager.getBaseDir());
|
File baseDir = new File(this.configManager.getBaseDir());
|
||||||
if (!baseDir.exists()) {
|
if (!baseDir.exists()) {
|
||||||
baseDir.mkdirs();
|
baseDir.mkdirs();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void copyDefaultConfig() {
|
private void copyDefaultConfig() {
|
||||||
File configFile = new File(configManager.getConfigFile().getAbsolutePath());
|
File configFile = new File(configManager.getConfigFile().getAbsolutePath());
|
||||||
if (!configFile.exists()) {
|
if (!configFile.exists()) {
|
||||||
try (InputStream in = MainVerticle.class.getClassLoader().getResourceAsStream("default.properties")) {
|
try (InputStream in = MainVerticle.class.getClassLoader().getResourceAsStream("default.properties")) {
|
||||||
if (in != null) {
|
if (in != null) {
|
||||||
Files.copy(in, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(in, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||||
} else {
|
} else {
|
||||||
Constants.LOGGER.error("Default config file not found in resources");
|
Constants.LOGGER.error("🔴 Default config file not found in resources");
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Constants.LOGGER.error("Failed to copy default config file", e);
|
Constants.LOGGER.error("🔴 Failed to copy default config file", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) {
|
public void start(Promise<Void> startPromise) {
|
||||||
|
try {
|
||||||
|
System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
|
||||||
|
init();
|
||||||
|
pool.query("SELECT 1").execute(ar -> {
|
||||||
|
if (ar.succeeded()) {
|
||||||
|
Constants.LOGGER.info("🟢 Connected to DB");
|
||||||
|
deployVerticles(startPromise);
|
||||||
|
} else {
|
||||||
|
Constants.LOGGER.error("🔴 Failed to connect to DB: " + ar.cause());
|
||||||
|
startPromise.fail(ar.cause());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("🔴 Error starting the application: " + e);
|
||||||
|
startPromise.fail(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deployVerticles(Promise<Void> startPromise) {
|
||||||
final DeploymentOptions options = new DeploymentOptions();
|
final DeploymentOptions options = new DeploymentOptions();
|
||||||
options.setThreadingModel(ThreadingModel.WORKER);
|
options.setThreadingModel(ThreadingModel.WORKER);
|
||||||
|
|
||||||
vertx.deployVerticle(new DataLayerAPIVerticle(), options, result -> {
|
vertx.deployVerticle(new DataLayerAPIVerticle(pool), options, result -> { // Pasa el pool
|
||||||
if(result.succeeded()) {
|
if (result.succeeded()) {
|
||||||
Constants.LOGGER.info(String.format(
|
Constants.LOGGER.info("🟢 DatabaseVerticle desplegado");
|
||||||
"🟢 DataLayerAPIVerticle desplegado. (http://%s:%d)",
|
} else {
|
||||||
configManager.getHost(), configManager.getDataApiPort()
|
Constants.LOGGER.error("🔴 Error deploying DataLayerAPIVerticle: " + result.cause());
|
||||||
));
|
}
|
||||||
} else {
|
|
||||||
Constants.LOGGER.error("🔴 Error al desplegar DataLayerAPIVerticle", result.cause());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
vertx.deployVerticle(new LogicLayerAPIVerticle(), options, result -> {
|
vertx.deployVerticle(new LogicLayerAPIVerticle(), options, result -> {
|
||||||
if(result.succeeded()) {
|
if (result.succeeded()) {
|
||||||
Constants.LOGGER.info(String.format(
|
Constants.LOGGER.info("🟢 LogicLayerAPIVerticle desplegado");
|
||||||
"🟢 ApiVerticle desplegado. (http://%s:%d)",
|
} else {
|
||||||
configManager.getHost(), configManager.getLogicApiPort()
|
Constants.LOGGER.error("🔴 Error deploying LogicLayerAPIVerticle: " + result.cause());
|
||||||
));
|
}
|
||||||
} else {
|
|
||||||
Constants.LOGGER.error("🔴 Error al desplegar LogicApiVerticle", result.cause());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
vertx.deployVerticle(new WebServerVerticle(), result -> {
|
vertx.deployVerticle(new WebServerVerticle(), result -> {
|
||||||
if(result.succeeded()) {
|
if (result.succeeded()) {
|
||||||
Constants.LOGGER.info(String.format(
|
Constants.LOGGER.info("🟢 WebServerVerticle desplegado");
|
||||||
"🟢 WebServerVerticle desplegado. (http://%s:%d)",
|
} else {
|
||||||
configManager.getHost(), configManager.getWebserverPort()));
|
Constants.LOGGER.error("🔴 Error deploying WebServerVerticle: " + result.cause());
|
||||||
} else {
|
}
|
||||||
Constants.LOGGER.error("🔴 Error al desplegar WebServerVerticle", result.cause());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
startPromise.complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -10,10 +10,12 @@ import net.miarma.contaminus.common.Constants;
|
|||||||
public class WebServerVerticle extends AbstractVerticle {
|
public class WebServerVerticle extends AbstractVerticle {
|
||||||
private ConfigManager configManager;
|
private ConfigManager configManager;
|
||||||
|
|
||||||
|
public WebServerVerticle() {
|
||||||
|
configManager = ConfigManager.getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) {
|
public void start(Promise<Void> startPromise) {
|
||||||
configManager = ConfigManager.getInstance();
|
|
||||||
|
|
||||||
Constants.LOGGER.info("📡 Iniciando WebServerVerticle...");
|
Constants.LOGGER.info("📡 Iniciando WebServerVerticle...");
|
||||||
|
|
||||||
Router router = Router.router(vertx);
|
Router router = Router.router(vertx);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# DB Configuration
|
# DB Configuration
|
||||||
db.protocol=jdbc:mariadb:
|
db.protocol=jdbc:mariadb
|
||||||
db.host=localhost
|
db.host=localhost
|
||||||
db.port=3306
|
db.port=3306
|
||||||
db.name=dad
|
db.name=dad
|
||||||
|
|||||||
Reference in New Issue
Block a user