Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use environment variables instead of server.ini by default #74

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
13 changes: 5 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
FROM alpine:3.20 AS base

# Add OpenJDK17
RUN apk add openjdk17=17.0.12_p7-r0
RUN apk add --no-cache openjdk17=17.0.12_p7-r0

# Uses /kepler directory
WORKDIR /kepler
Expand All @@ -17,16 +17,16 @@ WORKDIR /kepler
FROM base AS build

# Add unzip
RUN apk add unzip=6.0-r14
RUN apk add --no-cache unzip=6.0-r14

# Copy every files/folders that are not in .dockerignore
COPY . .

# Convert CRLF to LF executable files (failing build for Windows without this)
RUN sed -i 's/\r$//' gradlew tools/scripts/run.sh entrypoint.sh
RUN sed -i 's/\r$//' gradlew tools/scripts/run.sh

# Make gradlew, entrypoint.sh and run.sh executable
RUN chmod +x gradlew entrypoint.sh tools/scripts/run.sh
# Make gradlew and run.sh executable
RUN chmod +x gradlew tools/scripts/run.sh

# Run gradle build
RUN ./gradlew distZip
Expand All @@ -39,7 +39,6 @@ RUN rm -rf ./release/Kepler-Server/bin && \
mkdir -p ./build/lib && \
mv ./release/Kepler-Server/lib/Kepler-Server.jar ./build/kepler.jar && \
mv ./release/Kepler-Server/lib/* ./build/lib && \
mv ./entrypoint.sh ./build/entrypoint.sh && \
cp tools/scripts/run.sh ./build/

####################
Expand All @@ -51,6 +50,4 @@ FROM base AS production
# Copy builded Kepler server
COPY --from=build /kepler/build ./

ENTRYPOINT [ "sh", "entrypoint.sh" ]

CMD [ "sh", "run.sh" ]
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static boolean connect() {

storage = new Storage(ServerConfiguration.getString("mysql.hostname"),
ServerConfiguration.getInteger("mysql.port"),
ServerConfiguration.getString("mysql.username"),
ServerConfiguration.getString("mysql.user"),
ServerConfiguration.getString("mysql.password"),
ServerConfiguration.getString("mysql.database"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,38 @@ public static boolean getBoolean(String key) {

}

/**
* Helper method to get value from environment variable if exists
* Converts the key by replacing dots with underscores and converting to uppercase
* to match common environment variable naming conventions.
*
* @param key the key to use
* @return value from environment variable or null if not found
*/
private static String getEnvOrNull(String key) {
String envKey = key.replace('.', '_').toUpperCase();
String envValue = System.getenv(envKey);

if(envValue != null && !envValue.isEmpty()) {
return envValue;
}

return null;
}

/**
* Get value from configuration
*
* @param key the key to use
* @return value
*/
public static String getString(String key) {
String envValue = getEnvOrNull(key);

if(envValue != null) {
return envValue;
}

return config.getOrDefault(key, key);
}

Expand All @@ -65,6 +90,12 @@ public static String getString(String key) {
* @return value
*/
public static String getStringOrDefault(String key, String value) {
String envValue = getEnvOrNull(key);

if(envValue != null) {
return envValue;
}

return config.getOrDefault(key, value);
}

Expand All @@ -75,6 +106,18 @@ public static String getStringOrDefault(String key, String value) {
* @return value as int
*/
public static int getInteger(String key) {
String envValue = getEnvOrNull(key);

if(envValue != null) {
try {
return Integer.parseInt(envValue);
} catch (NumberFormatException e) {
// Handle the case where the env value is not a valid integer
System.err.println("Environment variable for key " + key + " is not a valid integer: " + envValue);
// Using default value below
}
}

return Integer.parseInt(config.getOrDefault(key, "0"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Map<String, String> setConfigurationDefaults() {

config.put("mysql.hostname", "127.0.0.1");
config.put("mysql.port", "3306");
config.put("mysql.username", "kepler");
config.put("mysql.user", "kepler");
config.put("mysql.password", "verysecret");
config.put("mysql.database", "kepler");

Expand All @@ -49,7 +49,7 @@ public void setConfigurationData(Map<String, String> config, PrintWriter writer)
writer.println("[Database]");
writer.println("mysql.hostname=" + config.get("mysql.hostname"));
writer.println("mysql.port=" + config.get("mysql.port"));
writer.println("mysql.username=" + config.get("mysql.username"));
writer.println("mysql.user=" + config.get("mysql.user"));
writer.println("mysql.password=" + config.get("mysql.password"));
writer.println("mysql.database=" + config.get("mysql.database"));
writer.println("");
Expand Down
79 changes: 0 additions & 79 deletions entrypoint.sh

This file was deleted.