Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Database Configuration
# Copy this file to .env and fill in your actual database credentials
# Defaults to config.yaml if not set, remove lines to use config.yaml's values
DB_HOST=localhost
DB_USER=root
DB_PASS=your_password_here
DB_URL_FORMAT=jdbc:mysql://%s:3306/cosmic
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ database/docker-pg-db-data

# macOS files
.DS_Store

# Environment variables
.env
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<liquibase-core.version>4.32.0</liquibase-core.version> <!-- Database migrations -->
<junit.version>5.13.1</junit.version> <!-- Unit test -->
<mockito.version>5.18.0</mockito.version> <!-- Unit test -->
<dotenv-java.version>3.0.0</dotenv-java.version> <!-- Environment variables from .env file -->
</properties>

<dependencies>
Expand All @@ -82,6 +83,11 @@
<artifactId>jcip-annotations</artifactId>
<version>${jcip-annotations.version}</version>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>${dotenv-java.version}</version>
</dependency>

<!-- Database -->
<dependency>
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/tools/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ public static Handle getHandle() {
private static String getDbUrl() {
// Environment variables override what's defined in the config file
// This feature is used for the Docker support
String hostOverride = System.getenv("DB_HOST");
String host = hostOverride != null ? hostOverride : YamlConfig.config.server.DB_HOST;
String dbUrl = String.format(YamlConfig.config.server.DB_URL_FORMAT, host);
return dbUrl;
String host = EnvironmentConfig.get("DB_HOST", YamlConfig.config.server.DB_HOST);
String urlFormat = EnvironmentConfig.get("DB_URL_FORMAT", YamlConfig.config.server.DB_URL_FORMAT);
return String.format(urlFormat, host);
}

private static HikariConfig getConfig() {
HikariConfig config = new HikariConfig();

config.setJdbcUrl(getDbUrl());
config.setUsername(YamlConfig.config.server.DB_USER);
config.setPassword(YamlConfig.config.server.DB_PASS);
config.setUsername(EnvironmentConfig.get("DB_USER", YamlConfig.config.server.DB_USER));
config.setPassword(EnvironmentConfig.get("DB_PASS", YamlConfig.config.server.DB_PASS));

final int initFailTimeoutSeconds = YamlConfig.config.server.INIT_CONNECTION_POOL_TIMEOUT;
config.setInitializationFailTimeout(SECONDS.toMillis(initFailTimeoutSeconds));
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/tools/EnvironmentConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tools;

import io.github.cdimascio.dotenv.Dotenv;

/**
* Provides access to configuration values from .env files and system environment variables.
* Priority order:
* 1. .env file values
* 2. System environment variables
* 3. Provided default value
*/
public class EnvironmentConfig {
private static final Dotenv dotenv;

static {
dotenv = Dotenv.configure()
.ignoreIfMissing()
.load();
}

/**
* Get an environment variable value with fallback to a default.
*
* @param key the environment variable key
* @param defaultValue the default value to use if the variable is not found
* @return the environment variable value, or the default if not found
*/
public static String get(String key, String defaultValue) {
if (key == null || key.isEmpty()) {
return defaultValue;
}

// First try .env file
if (dotenv != null) {
String value = dotenv.get(key);
if (value != null && !value.isEmpty()) {
return value;
}
}

// Then try system environment
String value = System.getenv(key);
if (value != null && !value.isEmpty()) {
return value;
}

// Finally use the provided default
return defaultValue;
}
}
14 changes: 14 additions & 0 deletions src/test/java/tools/EnvironmentConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tools;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class EnvironmentConfigTest {

@Test
void getShouldReturnDefaultWhenVariableNotFound() {
String value = EnvironmentConfig.get("NONEXISTENT_VARIABLE_XYZ", "my_default");
assertEquals("my_default", value);
}
}