refactor(bootstrap): replace PostConstruct validator with ApplicationContextInitializer
This commit is contained in:
parent
e0692f7913
commit
ab0e2ffa68
@ -0,0 +1,29 @@
|
||||
package com.pablotj.restemailbridge.bootstrap;
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
public class EnvironmentValidatorInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext ctx) {
|
||||
String[] requiredProperties = {
|
||||
"APP_ENCRYPTION_SECRET",
|
||||
"APP_ALLOWED_ORIGINS",
|
||||
"DB_NAME",
|
||||
"DB_USER",
|
||||
"DB_PASSWORD",
|
||||
"DB_HOST",
|
||||
"DB_PORT",
|
||||
"GMAIL_OAUTH_CLIENT_ID",
|
||||
"GMAIL_OAUTH_CLIENT_SECRET"
|
||||
};
|
||||
|
||||
for (String prop : requiredProperties) {
|
||||
String value = ctx.getEnvironment().getProperty(prop);
|
||||
if (value == null || value.isEmpty()) {
|
||||
throw new IllegalStateException("ERROR: Property '" + prop + "' is not defined or empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -11,6 +11,8 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
public class RestEmailBridgeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RestEmailBridgeApplication.class, args);
|
||||
SpringApplication app = new SpringApplication(RestEmailBridgeApplication.class);
|
||||
app.addInitializers(new EnvironmentValidatorInitializer());
|
||||
app.run(args);
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +0,0 @@
|
||||
package com.pablotj.restemailbridge.infrastructure.config;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AppPropertiesValidator {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AppPropertiesValidator.class);
|
||||
|
||||
@Value("${app.encryption.secret:}")
|
||||
private String encryptionSecret;
|
||||
|
||||
@Value("${app.cors.allowed-origins:}")
|
||||
private String allowedOrigins;
|
||||
|
||||
@Value("${spring.datasource.url:}")
|
||||
private String datasourceUrl;
|
||||
|
||||
@Value("${spring.datasource.username:}")
|
||||
private String datasourceUser;
|
||||
|
||||
@Value("${spring.datasource.password:}")
|
||||
private String datasourcePassword;
|
||||
|
||||
@Value("${gmail.oauth2.clientId:}")
|
||||
private String gmailClientId;
|
||||
|
||||
@Value("${gmail.oauth2.clientSecret:}")
|
||||
private String gmailClientSecret;
|
||||
|
||||
@PostConstruct
|
||||
public void validate() {
|
||||
check("APP_ENCRYPTION_SECRET", encryptionSecret, false);
|
||||
check("APP_ALLOWED_ORIGINS", allowedOrigins, true);
|
||||
check("DB_URL", datasourceUrl, true);
|
||||
check("DB_USER", datasourceUser, true);
|
||||
check("DB_PASSWORD", datasourcePassword, false);
|
||||
check("GMAIL_OAUTH_CLIENT_ID", gmailClientId, true);
|
||||
check("GMAIL_OAUTH_CLIENT_SECRET", gmailClientSecret, false);
|
||||
}
|
||||
|
||||
private void check(String name, String value, boolean logValue) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
throw new IllegalStateException("ERROR: Property '" + name + "' is not defined or empty");
|
||||
} else if (logValue) {
|
||||
log.info("Property '{}' is defined with value: {}", name, value);
|
||||
} else {
|
||||
log.info("Property '{}' is defined", name);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user