FIN
This commit is contained in:
@@ -57,9 +57,9 @@
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/io.quarkus/quarkus-agroal -->
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-agroal</artifactId>
|
||||
<version>3.19.3</version>
|
||||
<groupId>org.jboss.logmanager</groupId>
|
||||
<artifactId>jboss-logmanager</artifactId>
|
||||
<version>3.1.1.Final</version> <!-- O la versión más reciente -->
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
@@ -5,8 +5,7 @@ import io.vertx.core.impl.logging.LoggerFactory;
|
||||
|
||||
public class Constants {
|
||||
public static final String APP_NAME = "ContaminUS";
|
||||
public static final int API_VERSION = 1;
|
||||
public static final String API_PREFIX = "/api/v" + Constants.API_VERSION;
|
||||
public static final String API_PREFIX = "/api/v1";
|
||||
public static Logger LOGGER = LoggerFactory.getLogger(Constants.APP_NAME);
|
||||
|
||||
|
||||
|
||||
@@ -26,29 +26,15 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
|
||||
private DatabaseManager dbManager;
|
||||
private ConfigManager configManager;
|
||||
|
||||
|
||||
public DataLayerAPIVerticle() {
|
||||
JDBCConnectOptions connectOptions = new JDBCConnectOptions()
|
||||
.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);
|
||||
public DataLayerAPIVerticle(JDBCPool pool) {
|
||||
this.pool = pool;
|
||||
this.configManager = ConfigManager.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Promise<Void> startPromise) {
|
||||
dbManager = DatabaseManager.getInstance(pool);
|
||||
configManager = ConfigManager.getInstance();
|
||||
|
||||
Constants.LOGGER.info("📡 Iniciando DataLayerAPIVerticle...");
|
||||
|
||||
dbManager = DatabaseManager.getInstance(pool);
|
||||
|
||||
Router router = Router.router(vertx);
|
||||
Set<HttpMethod> allowedMethods = new HashSet<>(
|
||||
@@ -85,21 +71,6 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
|
||||
router.route(HttpMethod.POST, Constants.POST_ACTUATORS).handler(this::addActuator);
|
||||
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) {
|
||||
|
||||
@@ -17,10 +17,12 @@ import net.miarma.contaminus.common.Constants;
|
||||
public class LogicLayerAPIVerticle extends AbstractVerticle {
|
||||
private ConfigManager configManager;
|
||||
|
||||
public LogicLayerAPIVerticle() {
|
||||
this.configManager = ConfigManager.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Promise<Void> startPromise) {
|
||||
configManager = ConfigManager.getInstance();
|
||||
|
||||
Constants.LOGGER.info("📡 Iniciando LogicApiVerticle...");
|
||||
|
||||
Router router = Router.router(vertx);
|
||||
|
||||
@@ -6,6 +6,8 @@ import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
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.DeploymentOptions;
|
||||
import io.vertx.core.Launcher;
|
||||
@@ -15,78 +17,105 @@ import net.miarma.contaminus.common.ConfigManager;
|
||||
import net.miarma.contaminus.common.Constants;
|
||||
|
||||
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");
|
||||
init();
|
||||
Launcher.executeCommand("run", MainVerticle.class.getName());
|
||||
}
|
||||
|
||||
private static void init() {
|
||||
@SuppressWarnings("deprecation")
|
||||
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();
|
||||
copyDefaultConfig();
|
||||
}
|
||||
|
||||
private static void initializeDirectories() {
|
||||
File baseDir = new File(configManager.getBaseDir());
|
||||
private void initializeDirectories() {
|
||||
File baseDir = new File(this.configManager.getBaseDir());
|
||||
if (!baseDir.exists()) {
|
||||
baseDir.mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
private static void copyDefaultConfig() {
|
||||
private void copyDefaultConfig() {
|
||||
File configFile = new File(configManager.getConfigFile().getAbsolutePath());
|
||||
if (!configFile.exists()) {
|
||||
try (InputStream in = MainVerticle.class.getClassLoader().getResourceAsStream("default.properties")) {
|
||||
if (in != null) {
|
||||
Files.copy(in, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
} else {
|
||||
Constants.LOGGER.error("Default config file not found in resources");
|
||||
Constants.LOGGER.error("🔴 Default config file not found in resources");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Constants.LOGGER.error("Failed to copy default config file", e);
|
||||
Constants.LOGGER.error("🔴 Failed to copy default config file", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
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();
|
||||
options.setThreadingModel(ThreadingModel.WORKER);
|
||||
|
||||
vertx.deployVerticle(new DataLayerAPIVerticle(), options, result -> {
|
||||
if(result.succeeded()) {
|
||||
Constants.LOGGER.info(String.format(
|
||||
"🟢 DataLayerAPIVerticle desplegado. (http://%s:%d)",
|
||||
configManager.getHost(), configManager.getDataApiPort()
|
||||
));
|
||||
vertx.deployVerticle(new DataLayerAPIVerticle(pool), options, result -> { // Pasa el pool
|
||||
if (result.succeeded()) {
|
||||
Constants.LOGGER.info("🟢 DatabaseVerticle desplegado");
|
||||
} else {
|
||||
Constants.LOGGER.error("🔴 Error al desplegar DataLayerAPIVerticle", result.cause());
|
||||
Constants.LOGGER.error("🔴 Error deploying DataLayerAPIVerticle: " + result.cause());
|
||||
}
|
||||
});
|
||||
|
||||
vertx.deployVerticle(new LogicLayerAPIVerticle(), options, result -> {
|
||||
if(result.succeeded()) {
|
||||
Constants.LOGGER.info(String.format(
|
||||
"🟢 ApiVerticle desplegado. (http://%s:%d)",
|
||||
configManager.getHost(), configManager.getLogicApiPort()
|
||||
));
|
||||
if (result.succeeded()) {
|
||||
Constants.LOGGER.info("🟢 LogicLayerAPIVerticle desplegado");
|
||||
} else {
|
||||
Constants.LOGGER.error("🔴 Error al desplegar LogicApiVerticle", result.cause());
|
||||
Constants.LOGGER.error("🔴 Error deploying LogicLayerAPIVerticle: " + result.cause());
|
||||
}
|
||||
});
|
||||
|
||||
vertx.deployVerticle(new WebServerVerticle(), result -> {
|
||||
if(result.succeeded()) {
|
||||
Constants.LOGGER.info(String.format(
|
||||
"🟢 WebServerVerticle desplegado. (http://%s:%d)",
|
||||
configManager.getHost(), configManager.getWebserverPort()));
|
||||
if (result.succeeded()) {
|
||||
Constants.LOGGER.info("🟢 WebServerVerticle desplegado");
|
||||
} else {
|
||||
Constants.LOGGER.error("🔴 Error al desplegar WebServerVerticle", result.cause());
|
||||
Constants.LOGGER.error("🔴 Error deploying WebServerVerticle: " + result.cause());
|
||||
}
|
||||
});
|
||||
|
||||
startPromise.complete();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,10 +10,12 @@ import net.miarma.contaminus.common.Constants;
|
||||
public class WebServerVerticle extends AbstractVerticle {
|
||||
private ConfigManager configManager;
|
||||
|
||||
public WebServerVerticle() {
|
||||
configManager = ConfigManager.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Promise<Void> startPromise) {
|
||||
configManager = ConfigManager.getInstance();
|
||||
|
||||
Constants.LOGGER.info("📡 Iniciando WebServerVerticle...");
|
||||
|
||||
Router router = Router.router(vertx);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# DB Configuration
|
||||
db.protocol=jdbc:mariadb:
|
||||
db.protocol=jdbc:mariadb
|
||||
db.host=localhost
|
||||
db.port=3306
|
||||
db.name=dad
|
||||
|
||||
Reference in New Issue
Block a user