changes (still bugs :c)
This commit is contained in:
@@ -1,62 +0,0 @@
|
|||||||
package net.miarma.contaminus;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
|
|
||||||
import io.vertx.core.Launcher;
|
|
||||||
import io.vertx.core.impl.logging.LoggerFactory;
|
|
||||||
import net.miarma.contaminus.common.Constants;
|
|
||||||
import net.miarma.contaminus.common.OSType;
|
|
||||||
import net.miarma.contaminus.server.MainVerticle;
|
|
||||||
import net.miarma.contaminus.util.SystemUtil;
|
|
||||||
|
|
||||||
public class ContaminUS {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
initializeConstants();
|
|
||||||
initializeDirectories();
|
|
||||||
copyDefaultConfig();
|
|
||||||
|
|
||||||
Launcher.executeCommand("run", MainVerticle.class.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void initializeConstants() {
|
|
||||||
Constants.HOME_DIR = SystemUtil.getOS() == OSType.WINDOWS ?
|
|
||||||
"C:/Users/" + System.getProperty("user.name") + "/" :
|
|
||||||
System.getProperty("user.home").contains("root") ? "/root/" :
|
|
||||||
"/home/" + System.getProperty("user.name") + "/";
|
|
||||||
|
|
||||||
Constants.BASE_DIR = Constants.HOME_DIR +
|
|
||||||
(SystemUtil.getOS() == OSType.WINDOWS ? ".contaminus" :
|
|
||||||
SystemUtil.getOS() == OSType.LINUX ? ".config/contaminus" :
|
|
||||||
".contaminus");
|
|
||||||
|
|
||||||
Constants.CONFIG_FILE = Constants.BASE_DIR + "/config.properties";
|
|
||||||
System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
|
|
||||||
Constants.LOGGER = LoggerFactory.getLogger(Constants.APP_NAME);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void initializeDirectories() {
|
|
||||||
File baseDir = new File(Constants.BASE_DIR);
|
|
||||||
if (!baseDir.exists()) {
|
|
||||||
baseDir.mkdirs();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void copyDefaultConfig() {
|
|
||||||
File configFile = new File(Constants.CONFIG_FILE);
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
Constants.LOGGER.error("Failed to copy default config file", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,21 +4,20 @@ import java.io.*;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class ConfigManager {
|
public class ConfigManager {
|
||||||
private static ConfigManager instance;
|
private static final ConfigManager INSTANCE = new ConfigManager();
|
||||||
private final File configFile;
|
private final File configFile;
|
||||||
private final Properties config;
|
private final Properties config;
|
||||||
|
private static final String CONFIG_FILE_NAME = "config.properties";
|
||||||
|
|
||||||
private ConfigManager() {
|
private ConfigManager() {
|
||||||
this.configFile = new File(Constants.CONFIG_FILE);
|
String path = getBaseDir() + CONFIG_FILE_NAME;
|
||||||
|
this.configFile = new File(path);
|
||||||
this.config = new Properties();
|
this.config = new Properties();
|
||||||
loadConfig();
|
loadConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized ConfigManager getInstance() {
|
public static ConfigManager getInstance() {
|
||||||
if (instance == null) {
|
return INSTANCE;
|
||||||
instance = new ConfigManager();
|
|
||||||
}
|
|
||||||
return instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadConfig() {
|
private void loadConfig() {
|
||||||
@@ -29,24 +28,69 @@ public class ConfigManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getJdbcUrl() {
|
public File getConfigFile() {
|
||||||
return String.format("%s://%s:%s/%s",
|
return configFile;
|
||||||
config.getProperty("db.protocol"),
|
|
||||||
config.getProperty("db.host"),
|
|
||||||
config.getProperty("db.port"),
|
|
||||||
config.getProperty("db.name"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getJdbcUrl() {
|
||||||
|
return String.format("%s://%s:%s/%s",
|
||||||
|
config.getProperty("db.protocol"),
|
||||||
|
config.getProperty("db.host"),
|
||||||
|
config.getProperty("db.port"),
|
||||||
|
config.getProperty("db.name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost() {
|
||||||
|
return this.getStringProperty("inet.host");
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDataApiPort() {
|
||||||
|
return this.getIntProperty("data-api.port");
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLogicApiPort() {
|
||||||
|
return this.getIntProperty("logic-api.port");
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWebserverPort() {
|
||||||
|
return this.getIntProperty("web.port");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHomeDir() {
|
||||||
|
return getOS() == OSType.WINDOWS ?
|
||||||
|
"C:/Users/" + System.getProperty("user.name") + "/" :
|
||||||
|
System.getProperty("user.home").contains("root") ? "/root/" :
|
||||||
|
"/home/" + System.getProperty("user.name") + "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBaseDir() {
|
||||||
|
return getHomeDir() +
|
||||||
|
(getOS() == OSType.WINDOWS ? ".contaminus" :
|
||||||
|
getOS() == OSType.LINUX ? ".config/contaminus" :
|
||||||
|
".contaminus");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OSType getOS() {
|
||||||
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
|
if (os.contains("win")) {
|
||||||
|
return OSType.WINDOWS;
|
||||||
|
} else if (os.contains("nix") || os.contains("nux")) {
|
||||||
|
return OSType.LINUX;
|
||||||
|
} else {
|
||||||
|
return OSType.INVALID_OS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public String getStringProperty(String key) {
|
public String getStringProperty(String key) {
|
||||||
return config.getProperty(key);
|
return config.getProperty(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getIntProperty(String key) {
|
public int getIntProperty(String key) {
|
||||||
return Integer.parseInt(config.getProperty(key));
|
return Integer.parseInt(config.getProperty(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getBooleanProperty(String key) {
|
public boolean getBooleanProperty(String key) {
|
||||||
return Boolean.parseBoolean(config.getProperty(key));
|
return Boolean.parseBoolean(config.getProperty(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProperty(String key, String value) {
|
public void setProperty(String key, String value) {
|
||||||
|
|||||||
@@ -1,18 +1,14 @@
|
|||||||
package net.miarma.contaminus.common;
|
package net.miarma.contaminus.common;
|
||||||
|
|
||||||
import io.vertx.core.impl.logging.Logger;
|
import io.vertx.core.impl.logging.Logger;
|
||||||
import net.miarma.contaminus.database.DatabaseManager;
|
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 int API_VERSION = 1;
|
||||||
public static final String API_PREFIX = "/api/v" + Constants.API_VERSION;
|
public static final String API_PREFIX = "/api/v" + Constants.API_VERSION;
|
||||||
public static String HOME_DIR;
|
public static Logger LOGGER = LoggerFactory.getLogger(Constants.APP_NAME);
|
||||||
public static String BASE_DIR;
|
|
||||||
public static String CONFIG_FILE;
|
|
||||||
public static ConfigManager CONFIG;
|
|
||||||
public static DatabaseManager DB;
|
|
||||||
public static Logger LOGGER;
|
|
||||||
|
|
||||||
/* API Endpoints */
|
/* API Endpoints */
|
||||||
public static final String GET_GROUPS = API_PREFIX + "/groups";
|
public static final String GET_GROUPS = API_PREFIX + "/groups";
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
package net.miarma.contaminus.database;
|
package net.miarma.contaminus.database;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import io.vertx.core.Future;
|
import io.vertx.core.Future;
|
||||||
import io.vertx.core.Handler;
|
import io.vertx.core.Handler;
|
||||||
import io.vertx.core.Vertx;
|
import io.vertx.core.Vertx;
|
||||||
@@ -15,52 +10,61 @@ import io.vertx.sqlclient.RowSet;
|
|||||||
import net.miarma.contaminus.common.ConfigManager;
|
import net.miarma.contaminus.common.ConfigManager;
|
||||||
import net.miarma.contaminus.common.Constants;
|
import net.miarma.contaminus.common.Constants;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class DatabaseManager {
|
public class DatabaseManager {
|
||||||
private final JDBCPool pool;
|
private static DatabaseManager instance;
|
||||||
|
private final JDBCPool pool;
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public DatabaseManager(Vertx vertx) {
|
private DatabaseManager(Vertx vertx) {
|
||||||
ConfigManager config = ConfigManager.getInstance();
|
ConfigManager config = ConfigManager.getInstance();
|
||||||
|
|
||||||
JsonObject dbConfig = new JsonObject()
|
JsonObject dbConfig = new JsonObject()
|
||||||
.put("url", config.getStringProperty("db.protocol") + "//" +
|
.put("jdbcUrl", config.getJdbcUrl())
|
||||||
config.getStringProperty("db.host") + ":" +
|
.put("username", config.getStringProperty("db.user"))
|
||||||
config.getStringProperty("db.port") + "/" +
|
.put("password", config.getStringProperty("db.pwd"))
|
||||||
config.getStringProperty("db.name"))
|
.put("max_pool_size", config.getIntProperty("db.poolSize"));
|
||||||
.put("user", config.getStringProperty("db.user"))
|
|
||||||
.put("password", config.getStringProperty("db.pwd"))
|
|
||||||
.put("max_pool_size", config.getStringProperty("db.poolSize"));
|
|
||||||
|
|
||||||
pool = JDBCPool.pool(vertx, dbConfig);
|
pool = JDBCPool.pool(vertx, dbConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Future<RowSet<Row>> testConnection() {
|
public static synchronized DatabaseManager getInstance(Vertx vertx) {
|
||||||
return pool.query("SELECT 1").execute();
|
if (instance == null) {
|
||||||
}
|
instance = new DatabaseManager(vertx);
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
public <T> Future<List<T>> execute(String query, Class<T> clazz,
|
public Future<RowSet<Row>> testConnection() {
|
||||||
Handler<List<T>> onSuccess, Handler<Throwable> onFailure) {
|
return pool.query("SELECT 1").execute();
|
||||||
|
}
|
||||||
|
|
||||||
return pool.query(query).execute()
|
public <T> Future<List<T>> execute(String query, Class<T> clazz,
|
||||||
.map(rows -> {
|
Handler<List<T>> onSuccess, Handler<Throwable> onFailure) {
|
||||||
List<T> results = new ArrayList<>();
|
return pool.query(query).execute()
|
||||||
for (Row row : rows) {
|
.map(rows -> {
|
||||||
try {
|
List<T> results = new ArrayList<>();
|
||||||
Constructor<T> constructor = clazz.getConstructor(Row.class);
|
for (Row row : rows) {
|
||||||
results.add(constructor.newInstance(row));
|
try {
|
||||||
} catch (NoSuchMethodException | InstantiationException |
|
Constructor<T> constructor = clazz.getConstructor(Row.class);
|
||||||
IllegalAccessException | InvocationTargetException e) {
|
results.add(constructor.newInstance(row));
|
||||||
Constants.LOGGER.error("Error instantiating class: " + e.getMessage());
|
} catch (NoSuchMethodException | InstantiationException |
|
||||||
}
|
IllegalAccessException | InvocationTargetException e) {
|
||||||
}
|
Constants.LOGGER.error("Error instantiating class: " + e.getMessage());
|
||||||
return results;
|
}
|
||||||
})
|
}
|
||||||
.onComplete(ar -> {
|
return results;
|
||||||
if (ar.succeeded()) {
|
})
|
||||||
onSuccess.handle(ar.result());
|
.onComplete(ar -> {
|
||||||
} else {
|
if (ar.succeeded()) {
|
||||||
onFailure.handle(ar.cause());
|
onSuccess.handle(ar.result());
|
||||||
}
|
} else {
|
||||||
});
|
onFailure.handle(ar.cause());
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,22 +17,17 @@ import io.vertx.ext.web.handler.CorsHandler;
|
|||||||
import net.miarma.contaminus.common.ConfigManager;
|
import net.miarma.contaminus.common.ConfigManager;
|
||||||
import net.miarma.contaminus.common.Constants;
|
import net.miarma.contaminus.common.Constants;
|
||||||
import net.miarma.contaminus.database.DatabaseManager;
|
import net.miarma.contaminus.database.DatabaseManager;
|
||||||
import net.miarma.contaminus.util.SystemUtil;
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class DataLayerAPIVerticle extends AbstractVerticle {
|
public class DataLayerAPIVerticle extends AbstractVerticle {
|
||||||
private DatabaseManager dbManager;
|
private DatabaseManager dbManager = DatabaseManager.getInstance(vertx);
|
||||||
private Gson gson;
|
private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
|
||||||
private ConfigManager configManager;
|
private ConfigManager configManager = ConfigManager.getInstance();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) {
|
public void start(Promise<Void> startPromise) {
|
||||||
Constants.LOGGER.info("📡 Iniciando DataLayerAPIVerticle...");
|
Constants.LOGGER.info("📡 Iniciando DataLayerAPIVerticle...");
|
||||||
|
|
||||||
configManager = ConfigManager.getInstance();
|
|
||||||
gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
|
|
||||||
dbManager = new DatabaseManager(vertx);
|
|
||||||
|
|
||||||
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
|
||||||
@@ -71,30 +66,18 @@ public class DataLayerAPIVerticle extends AbstractVerticle {
|
|||||||
dbManager.testConnection()
|
dbManager.testConnection()
|
||||||
.onSuccess(rows -> {
|
.onSuccess(rows -> {
|
||||||
Constants.LOGGER.info("✅ Database connection ok");
|
Constants.LOGGER.info("✅ Database connection ok");
|
||||||
Constants.LOGGER.info("🟢 DataAccessVerticle desplegado");
|
vertx.createHttpServer()
|
||||||
|
.requestHandler(router)
|
||||||
|
.listen(configManager.getDataApiPort(), configManager.getHost());
|
||||||
startPromise.complete();
|
startPromise.complete();
|
||||||
})
|
})
|
||||||
.onFailure(onFailure -> {
|
.onFailure(onFailure -> {
|
||||||
Constants.LOGGER.error("❌ Database connection failed");
|
Constants.LOGGER.error("❌ Database connection failed");
|
||||||
Constants.LOGGER.error("🔴 Error al desplegar DataAccessVerticle", onFailure);
|
Throwable t = onFailure.getCause();
|
||||||
|
t.printStackTrace();
|
||||||
startPromise.fail(onFailure);
|
startPromise.fail(onFailure);
|
||||||
});
|
});
|
||||||
|
|
||||||
vertx.createHttpServer()
|
|
||||||
.requestHandler(router)
|
|
||||||
.listen(SystemUtil.getDataApiPort(), result -> {
|
|
||||||
if (result.succeeded()) {
|
|
||||||
Constants.LOGGER.info(String.format(
|
|
||||||
"🟢 DataLayerAPIVerticle desplegado. (http://%s:%d)",
|
|
||||||
SystemUtil.getHost(), SystemUtil.getDataApiPort()
|
|
||||||
));
|
|
||||||
startPromise.complete();
|
|
||||||
} else {
|
|
||||||
Constants.LOGGER.error("🔴 Error al desplegar DataLayerAPIVerticle", result.cause());
|
|
||||||
startPromise.fail(result.cause());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getAllGroups(RoutingContext context) {
|
private void getAllGroups(RoutingContext context) {
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
package net.miarma.contaminus.server;
|
|
||||||
|
|
||||||
import io.vertx.core.AbstractVerticle;
|
|
||||||
import io.vertx.ext.web.Router;
|
|
||||||
import io.vertx.ext.web.handler.StaticHandler;
|
|
||||||
import net.miarma.contaminus.common.Constants;
|
|
||||||
import net.miarma.contaminus.util.SystemUtil;
|
|
||||||
|
|
||||||
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.route("/dashboard/*").handler(ctx -> {
|
|
||||||
ctx.reroute("/index.html");
|
|
||||||
});
|
|
||||||
|
|
||||||
vertx.createHttpServer().requestHandler(router).listen(
|
|
||||||
SystemUtil.getWebserverPort(), SystemUtil.getHost(), result -> {
|
|
||||||
if (result.succeeded()) {
|
|
||||||
Constants.LOGGER.info(String.format("🟢 HttpServerVerticle desplegado. (http://%s:%d)",
|
|
||||||
SystemUtil.getHost(), SystemUtil.getWebserverPort())
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
Constants.LOGGER.error("🔴 Error al desplegar HttpServerVerticle", result.cause());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -18,22 +18,17 @@ import io.vertx.ext.web.handler.CorsHandler;
|
|||||||
import net.miarma.contaminus.common.ConfigManager;
|
import net.miarma.contaminus.common.ConfigManager;
|
||||||
import net.miarma.contaminus.common.Constants;
|
import net.miarma.contaminus.common.Constants;
|
||||||
import net.miarma.contaminus.database.DatabaseManager;
|
import net.miarma.contaminus.database.DatabaseManager;
|
||||||
import net.miarma.contaminus.util.SystemUtil;
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class LogicLayerAPIVerticle extends AbstractVerticle {
|
public class LogicLayerAPIVerticle extends AbstractVerticle {
|
||||||
private DatabaseManager dbManager;
|
private DatabaseManager dbManager = DatabaseManager.getInstance(vertx);
|
||||||
private Gson gson;
|
private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
|
||||||
private ConfigManager configManager;
|
private ConfigManager configManager = ConfigManager.getInstance();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) {
|
public void start(Promise<Void> startPromise) {
|
||||||
Constants.LOGGER.info("📡 Iniciando LogicApiVerticle...");
|
Constants.LOGGER.info("📡 Iniciando LogicApiVerticle...");
|
||||||
|
|
||||||
configManager = ConfigManager.getInstance();
|
|
||||||
gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
|
|
||||||
dbManager = new DatabaseManager(vertx);
|
|
||||||
|
|
||||||
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
|
||||||
@@ -60,20 +55,9 @@ public class LogicLayerAPIVerticle extends AbstractVerticle {
|
|||||||
|
|
||||||
vertx.createHttpServer()
|
vertx.createHttpServer()
|
||||||
.requestHandler(router)
|
.requestHandler(router)
|
||||||
.listen(
|
.listen(configManager.getLogicApiPort(), configManager.getHost());
|
||||||
SystemUtil.getLogicApiPort(),
|
|
||||||
SystemUtil.getHost(),
|
startPromise.complete();
|
||||||
result -> {
|
|
||||||
if (result.succeeded()) {
|
|
||||||
Constants.LOGGER.info(String.format(
|
|
||||||
"🟢 ApiVerticle desplegado. (http://%s:%d)", SystemUtil.getHost(), SystemUtil.getLogicApiPort()
|
|
||||||
));
|
|
||||||
startPromise.complete();
|
|
||||||
} else {
|
|
||||||
Constants.LOGGER.error("🔴 Error al desplegar LogicApiVerticle", result.cause());
|
|
||||||
startPromise.fail(result.cause());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,87 @@
|
|||||||
package net.miarma.contaminus.server;
|
package net.miarma.contaminus.server;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
|
||||||
import io.vertx.core.AbstractVerticle;
|
import io.vertx.core.AbstractVerticle;
|
||||||
import io.vertx.core.DeploymentOptions;
|
import io.vertx.core.DeploymentOptions;
|
||||||
import io.vertx.core.Promise;
|
import io.vertx.core.Promise;
|
||||||
import io.vertx.core.ThreadingModel;
|
import io.vertx.core.ThreadingModel;
|
||||||
|
import net.miarma.contaminus.common.ConfigManager;
|
||||||
|
import net.miarma.contaminus.common.Constants;
|
||||||
|
|
||||||
public class MainVerticle extends AbstractVerticle {
|
public class MainVerticle extends AbstractVerticle {
|
||||||
|
static ConfigManager configManager = ConfigManager.getInstance();
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
initializeDirectories();
|
||||||
|
copyDefaultConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initializeDirectories() {
|
||||||
|
File baseDir = new File(configManager.getBaseDir());
|
||||||
|
if (!baseDir.exists()) {
|
||||||
|
baseDir.mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static 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");
|
||||||
|
}
|
||||||
|
} catch (IOException 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) {
|
||||||
|
System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
|
||||||
|
init();
|
||||||
|
|
||||||
final DeploymentOptions options = new DeploymentOptions();
|
final DeploymentOptions options = new DeploymentOptions();
|
||||||
options.setThreadingModel(ThreadingModel.WORKER);
|
options.setThreadingModel(ThreadingModel.WORKER);
|
||||||
|
|
||||||
getVertx().deployVerticle(new DataLayerAPIVerticle(), options);
|
vertx.deployVerticle(new DataLayerAPIVerticle(), options, result -> {
|
||||||
getVertx().deployVerticle(new LogicLayerAPIVerticle(), options);
|
if(result.succeeded()) {
|
||||||
getVertx().deployVerticle(new HttpServerVerticle());
|
Constants.LOGGER.info(String.format(
|
||||||
|
"🟢 DataLayerAPIVerticle desplegado. (http://%s:%d)",
|
||||||
|
configManager.getHost(), configManager.getDataApiPort()
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
Constants.LOGGER.error("🔴 Error al desplegar 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()
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
Constants.LOGGER.error("🔴 Error al desplegar LogicApiVerticle", result.cause());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
vertx.deployVerticle(new WebServerVerticle(), result -> {
|
||||||
|
if(result.succeeded()) {
|
||||||
|
Constants.LOGGER.info(String.format(
|
||||||
|
"🟢 WebServerVerticle desplegado. (http://%s:%d)",
|
||||||
|
configManager.getHost(), configManager.getWebserverPort()));
|
||||||
|
} else {
|
||||||
|
Constants.LOGGER.error("🔴 Error al desplegar WebServerVerticle", result.cause());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package net.miarma.contaminus.server;
|
||||||
|
|
||||||
|
import io.vertx.core.AbstractVerticle;
|
||||||
|
import io.vertx.core.Promise;
|
||||||
|
import io.vertx.ext.web.Router;
|
||||||
|
import io.vertx.ext.web.handler.StaticHandler;
|
||||||
|
import net.miarma.contaminus.common.ConfigManager;
|
||||||
|
import net.miarma.contaminus.common.Constants;
|
||||||
|
|
||||||
|
public class WebServerVerticle extends AbstractVerticle {
|
||||||
|
private final ConfigManager configManager = ConfigManager.getInstance();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Promise<Void> startPromise) {
|
||||||
|
Constants.LOGGER.info("📡 Iniciando WebServerVerticle...");
|
||||||
|
|
||||||
|
Router router = Router.router(vertx);
|
||||||
|
|
||||||
|
router.route("/*")
|
||||||
|
.handler(
|
||||||
|
StaticHandler.create("webroot")
|
||||||
|
.setDefaultContentEncoding("UTF-8")
|
||||||
|
);
|
||||||
|
|
||||||
|
router.route("/dashboard/*").handler(ctx -> {
|
||||||
|
ctx.reroute("/index.html");
|
||||||
|
});
|
||||||
|
|
||||||
|
vertx.createHttpServer()
|
||||||
|
.requestHandler(router)
|
||||||
|
.listen(configManager.getWebserverPort(), configManager.getHost());
|
||||||
|
|
||||||
|
startPromise.complete();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package net.miarma.contaminus.util;
|
|
||||||
|
|
||||||
import net.miarma.contaminus.common.ConfigManager;
|
|
||||||
import net.miarma.contaminus.common.OSType;
|
|
||||||
|
|
||||||
public class SystemUtil {
|
|
||||||
private static ConfigManager configManager;
|
|
||||||
|
|
||||||
private static String host;
|
|
||||||
private static int dataApiPort;
|
|
||||||
private static int logicApiPort;
|
|
||||||
private static int webserverPort;
|
|
||||||
|
|
||||||
public static void init() {
|
|
||||||
configManager = ConfigManager.getInstance();
|
|
||||||
|
|
||||||
host = configManager.getStringProperty("inet.host");
|
|
||||||
dataApiPort = configManager.getIntProperty("data-api.port");
|
|
||||||
logicApiPort = configManager.getIntProperty("logic-api.port");
|
|
||||||
webserverPort = configManager.getIntProperty("web.port");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getHost() {
|
|
||||||
return host;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getDataApiPort() {
|
|
||||||
return dataApiPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getLogicApiPort() {
|
|
||||||
return logicApiPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getWebserverPort() {
|
|
||||||
return webserverPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static OSType getOS() {
|
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
|
||||||
if (os.contains("win")) {
|
|
||||||
return OSType.WINDOWS;
|
|
||||||
} else if (os.contains("nix") || os.contains("nux")) {
|
|
||||||
return OSType.LINUX;
|
|
||||||
} else {
|
|
||||||
return OSType.INVALID_OS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user