113 lines
4.0 KiB
Java
113 lines
4.0 KiB
Java
package com.seconectai.condocontab.config;
|
|
|
|
import java.util.Map;
|
|
|
|
public final class AppConfig {
|
|
private final String dbHost;
|
|
private final int dbPort;
|
|
private final String dbUser;
|
|
private final String dbPassword;
|
|
private final String dbMasterName;
|
|
private final int httpPort;
|
|
private final String masterPasswordHash;
|
|
private final String jwtSecret;
|
|
private final String abacatePayApiKey;
|
|
private final String abacatePayWebhookSecret;
|
|
private final String appPublicUrl;
|
|
private final String evolutionGlobalApiKey;
|
|
private final String evolutionApiUrl;
|
|
|
|
private AppConfig(String dbHost, int dbPort, String dbUser, String dbPassword, String dbMasterName,
|
|
int httpPort, String masterPasswordHash, String jwtSecret, String abacatePayApiKey,
|
|
String abacatePayWebhookSecret, String appPublicUrl, String evolutionGlobalApiKey, String evolutionApiUrl) {
|
|
this.dbHost = dbHost;
|
|
this.dbPort = dbPort;
|
|
this.dbUser = dbUser;
|
|
this.dbPassword = dbPassword;
|
|
this.dbMasterName = dbMasterName;
|
|
this.httpPort = httpPort;
|
|
this.masterPasswordHash = masterPasswordHash;
|
|
this.jwtSecret = jwtSecret;
|
|
this.abacatePayApiKey = abacatePayApiKey;
|
|
this.abacatePayWebhookSecret = abacatePayWebhookSecret;
|
|
this.appPublicUrl = appPublicUrl;
|
|
this.evolutionGlobalApiKey = evolutionGlobalApiKey;
|
|
this.evolutionApiUrl = evolutionApiUrl;
|
|
}
|
|
|
|
public static AppConfig fromEnv() {
|
|
Map<String, String> env = System.getenv();
|
|
return new AppConfig(
|
|
value(env, "DB_HOST", "localhost"),
|
|
intValue(env, "DB_PORT", 5432),
|
|
required(env, "DB_USER"),
|
|
required(env, "DB_PASSWORD"),
|
|
value(env, "DB_MASTER_NAME", "db_master"),
|
|
intValue(env, "HTTP_PORT", 8080),
|
|
required(env, "MASTER_PASSWORD_HASH"),
|
|
required(env, "JWT_SECRET"),
|
|
value(env, "ABACATEPAY_API_KEY", ""),
|
|
value(env, "ABACATEPAY_WEBHOOK_SECRET", "dev_webhook_secret"),
|
|
value(env, "APP_PUBLIC_URL", "http://localhost:8080"),
|
|
value(env, "EVOLUTION_GLOBAL_API_KEY", ""),
|
|
value(env, "EVOLUTION_API_URL", "mock://evolution")
|
|
);
|
|
}
|
|
|
|
private static String required(Map<String, String> env, String name) {
|
|
String value = env.get(name);
|
|
if (value == null || value.isBlank()) {
|
|
throw new IllegalStateException("Variavel de ambiente obrigatoria ausente: " + name);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
private static String value(Map<String, String> env, String name, String defaultValue) {
|
|
String value = env.get(name);
|
|
return value == null || value.isBlank() ? defaultValue : value;
|
|
}
|
|
|
|
private static int intValue(Map<String, String> env, String name, int defaultValue) {
|
|
String value = env.get(name);
|
|
if (value == null || value.isBlank()) {
|
|
return defaultValue;
|
|
}
|
|
return Integer.parseInt(value);
|
|
}
|
|
|
|
public String masterJdbcUrl() {
|
|
return jdbcUrl(dbMasterName);
|
|
}
|
|
|
|
public String postgresJdbcUrl() {
|
|
return jdbcUrl("postgres");
|
|
}
|
|
|
|
public String jdbcUrl(String databaseName) {
|
|
return "jdbc:postgresql://" + dbHost + ":" + dbPort + "/" + databaseName;
|
|
}
|
|
|
|
public String dbUser() {
|
|
return dbUser;
|
|
}
|
|
|
|
public String dbPassword() {
|
|
return dbPassword;
|
|
}
|
|
|
|
public int httpPort() {
|
|
return httpPort;
|
|
}
|
|
|
|
public String masterPasswordHash() {
|
|
return masterPasswordHash;
|
|
}
|
|
|
|
public String jwtSecret() { return jwtSecret; }
|
|
public String abacatePayApiKey() { return abacatePayApiKey; }
|
|
public String abacatePayWebhookSecret() { return abacatePayWebhookSecret; }
|
|
public String appPublicUrl() { return appPublicUrl; }
|
|
public String evolutionGlobalApiKey() { return evolutionGlobalApiKey; }
|
|
public String evolutionApiUrl() { return evolutionApiUrl; }
|
|
}
|