Skip to content
This repository has been archived by the owner on Jan 12, 2025. It is now read-only.

Commit

Permalink
Merge pull request #74 from AlexandreVassard/docker-env-variables
Browse files Browse the repository at this point in the history
Use environment variables instead of server.ini by default
  • Loading branch information
Quackster authored Jan 6, 2025
2 parents c39bd11 + a00bacd commit 9432736
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 90 deletions.
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.

0 comments on commit 9432736

Please sign in to comment.