1
0

api changes cors

This commit is contained in:
Jose
2025-03-12 03:52:11 +01:00
parent 2d31866e0e
commit 803851e9b7
6 changed files with 30 additions and 12 deletions

View File

@@ -8,7 +8,8 @@ public class Constants {
public static final String API_PREFIX = "/api/v1"; public static final String API_PREFIX = "/api/v1";
public static final String HOME_DIR = SystemInfo.getOS() == OSType.WINDOWS ? public static final String HOME_DIR = SystemInfo.getOS() == OSType.WINDOWS ?
"C:/Users/" + System.getProperty("user.name") + "/" : "C:/Users/" + System.getProperty("user.name") + "/" :
System.getProperty("user.home") + "/"; System.getProperty("user.home").contains("root") ? "/root/" :
"/home/" + System.getProperty("user.name") + "/";
public static final String BASE_DIR = HOME_DIR + public static final String BASE_DIR = HOME_DIR +
(SystemInfo.getOS() == OSType.WINDOWS ? ".contaminus" : (SystemInfo.getOS() == OSType.WINDOWS ? ".contaminus" :
SystemInfo.getOS() == OSType.LINUX ? ".config" + "/" + SystemInfo.getOS() == OSType.LINUX ? ".config" + "/" +

View File

@@ -1,8 +1,11 @@
package net.miarma.contaminus.common; package net.miarma.contaminus.common;
import java.util.Set;
public class Host { public class Host {
static ConfigManager configManager = ConfigManager.getInstance(); static ConfigManager configManager = ConfigManager.getInstance();
static String host = configManager.getStringProperty("inet.host"); static String host = configManager.getStringProperty("inet.host");
static String origin = configManager.getStringProperty("inet.origin");
static int apiPort = configManager.getIntProperty("api.port"); static int apiPort = configManager.getIntProperty("api.port");
static int webserverPort = configManager.getIntProperty("webserver.port"); static int webserverPort = configManager.getIntProperty("webserver.port");
@@ -18,8 +21,10 @@ public class Host {
return webserverPort; return webserverPort;
} }
public static String getOrigin() { public static Set<String> getOrigins() {
return "http://" + host + ":" + webserverPort; return Set.of("http://" + origin + ":" + webserverPort,
"https://" + origin + ":" + webserverPort);
} }
} }

View File

@@ -25,11 +25,12 @@ public class ApiVerticle extends AbstractVerticle {
Constants.LOGGER.info("🟢 Iniciando ApiVerticle..."); Constants.LOGGER.info("🟢 Iniciando ApiVerticle...");
Router router = Router.router(vertx); Router router = Router.router(vertx);
Set<HttpMethod> allowedMethods = new HashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT)); // Por ejemplo Set<HttpMethod> allowedMethods = new HashSet<>(
Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.OPTIONS)); // Por ejemplo
Set<String> allowedHeaders = new HashSet<>(Arrays.asList("Content-Type", "Authorization")); Set<String> allowedHeaders = new HashSet<>(Arrays.asList("Content-Type", "Authorization"));
router.route().handler(CorsHandler.create() router.route().handler(CorsHandler.create()
.addOrigin(Host.getOrigin()) .addOrigin("*")
.allowCredentials(true) .allowCredentials(true)
.allowedHeaders(allowedHeaders) .allowedHeaders(allowedHeaders)
.allowedMethods(allowedMethods)); .allowedMethods(allowedMethods));

View File

@@ -11,6 +11,7 @@ public class HttpServerVerticle extends AbstractVerticle {
@Override @Override
public void start() { public void start() {
Constants.LOGGER.info("🟢 Iniciando HttpServerVerticle..."); Constants.LOGGER.info("🟢 Iniciando HttpServerVerticle...");
Router router = Router.router(vertx); Router router = Router.router(vertx);
router.route("/*").handler(StaticHandler.create(Constants.BASE_DIR + "/webroot") router.route("/*").handler(StaticHandler.create(Constants.BASE_DIR + "/webroot")

View File

@@ -9,12 +9,20 @@ public class MainVerticle extends AbstractVerticle {
@Override @Override
public void start(Promise<Void> startPromise) { public void start(Promise<Void> startPromise) {
final DeploymentOptions options = new DeploymentOptions(); final DeploymentOptions options = new DeploymentOptions();
options.setThreadingModel(ThreadingModel.WORKER); options.setThreadingModel(ThreadingModel.WORKER);
getVertx().deployVerticle(new DatabaseVerticle(), options); String enabledVerticles = System.getProperty("vertx.options", "");
getVertx().deployVerticle(new ApiVerticle(), options);
getVertx().deployVerticle(new HttpServerVerticle()); if (enabledVerticles.contains("db")) {
getVertx().deployVerticle(new DatabaseVerticle(), options);
}
if (enabledVerticles.contains("api")) {
getVertx().deployVerticle(new ApiVerticle(), options);
}
if (enabledVerticles.contains("http")) {
getVertx().deployVerticle(new HttpServerVerticle());
}
} }
@Override @Override
@@ -24,7 +32,8 @@ public class MainVerticle extends AbstractVerticle {
} }
public static void main(String[] args) { public static void main(String[] args) {
io.vertx.core.Launcher.executeCommand("run", MainVerticle.class.getName()); System.setProperty("vertx.options", String.join(",", args));
io.vertx.core.Launcher.executeCommand("run", MainVerticle.class.getName());
} }
} }

View File

@@ -9,5 +9,6 @@ dp.poolSize=5
# Server Configuration # Server Configuration
inet.host=localhost inet.host=localhost
inet.origin=localhost
webserver.port=8080 webserver.port=8080
api.port=8081 api.port=8081