Update the environment configuration.

main
KKlochko 2 years ago
parent 5170f528cd
commit a8d7dcc69b

@ -2,6 +2,7 @@ package space.kklochko.jdbc_hospital_example.config.env;
import io.github.cdimascio.dotenv.Dotenv; import io.github.cdimascio.dotenv.Dotenv;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull;
import lombok.Setter; import lombok.Setter;
import java.util.Properties; import java.util.Properties;
@ -12,19 +13,31 @@ public class DataBaseEnvConfig {
public String url; public String url;
public Properties properties; public Properties properties;
public DataBaseEnvConfig() { public void load(@NonNull String dir, @NonNull String filename) {
Dotenv dotenv = Dotenv Dotenv dotenv = Dotenv
.configure() .configure()
.directory("config") .directory(dir)
.filename("db") .filename(filename)
.load(); .load();
setUrl(dotenv.get("JDBC_URL")); setUrl(dotenv.get("JDBC_URL"));
Properties properties = new Properties(); Properties properties = new Properties();
properties.put("user", dotenv.get("USER")); properties.put("user", dotenv.get("JDBC_USER"));
properties.put("password", dotenv.get("PASSWORD")); properties.put("password", dotenv.get("JDBC_PASSWORD"));
setProperties(properties); setProperties(properties);
} }
public DataBaseEnvConfig() {
load("config", "db");
}
public DataBaseEnvConfig(@NonNull String filename) {
load("config", filename);
}
public DataBaseEnvConfig(@NonNull String dir, @NonNull String filename) {
load(dir, filename);
}
} }

@ -1,9 +1,15 @@
package space.kklochko.jdbc_hospital_example.config.factories; package space.kklochko.jdbc_hospital_example.config.factories;
import lombok.NonNull;
import space.kklochko.jdbc_hospital_example.config.env.DataBaseEnvConfig; import space.kklochko.jdbc_hospital_example.config.env.DataBaseEnvConfig;
import space.kklochko.jdbc_hospital_example.config.models.DataBaseConfig; import space.kklochko.jdbc_hospital_example.config.models.DataBaseConfig;
public class LoadDataBaseConfigFromEnvFile { public class LoadDataBaseConfigFromEnvFile {
public DataBaseConfig load(@NonNull String filename) {
DataBaseEnvConfig dbEnv = new DataBaseEnvConfig(filename);
return new DataBaseConfig(dbEnv.getUrl(), dbEnv.getProperties());
}
public DataBaseConfig load() { public DataBaseConfig load() {
DataBaseEnvConfig dbEnv = new DataBaseEnvConfig(); DataBaseEnvConfig dbEnv = new DataBaseEnvConfig();
return new DataBaseConfig(dbEnv.getUrl(), dbEnv.getProperties()); return new DataBaseConfig(dbEnv.getUrl(), dbEnv.getProperties());

@ -7,4 +7,9 @@ import java.util.Properties;
public class DataBaseConfig { public class DataBaseConfig {
public String url; public String url;
public Properties properties; public Properties properties;
public DataBaseConfig(String url, Properties properties) {
setUrl(url);
setProperties(properties);
}
} }

Loading…
Cancel
Save