-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from vincent4vx/feature-271-use-env-in-config
feat(config): close #271 Handle .env and environment variable
- Loading branch information
Showing
17 changed files
with
793 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
DB_HOST=127.0.0.1 | ||
DB_USER=araknemu | ||
DB_PASSWORD= | ||
DB_NAME=araknemu | ||
|
||
CLIENT_VERSION=1.29.1 | ||
AUTH_PORT=4444 | ||
|
||
GAME_HOST=127.0.0.1 | ||
GAME_PORT=5555 | ||
TIMEZONE=Europe/Paris | ||
PRELOAD=false | ||
|
||
RATE_XP=1.0 | ||
RATE_DROP=1.0 | ||
|
||
ADMIN_COMMAND_SCRIPT_PATH=scripts/commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ config.ini | |
hs_err_* | ||
replay_* | ||
hotspot_* | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/fr/quatrevieux/araknemu/core/config/env/EnvDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* This file is part of Araknemu. | ||
* | ||
* Araknemu is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Araknemu is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* Copyright (c) 2017-2024 Vincent Quatrevieux | ||
*/ | ||
|
||
package fr.quatrevieux.araknemu.core.config.env; | ||
|
||
import fr.quatrevieux.araknemu.core.config.Driver; | ||
import fr.quatrevieux.araknemu.core.config.Pool; | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
|
||
/** | ||
* Decorate a generic driver to handle environment variables | ||
* and bash style string interpolation. | ||
*/ | ||
public final class EnvDriver implements Driver { | ||
private final Driver driver; | ||
private final Dotenv dotenv; | ||
|
||
public EnvDriver(Driver driver, Dotenv dotenv) { | ||
this.driver = driver; | ||
this.dotenv = dotenv; | ||
} | ||
|
||
@Override | ||
public boolean has(String key) { | ||
return driver.has(key); | ||
} | ||
|
||
@Override | ||
public Object get(String key) { | ||
return driver.get(key); | ||
} | ||
|
||
@Override | ||
public Pool pool(String key) { | ||
return new EnvPool( | ||
driver.pool(key), | ||
dotenv | ||
); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/fr/quatrevieux/araknemu/core/config/env/EnvPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* This file is part of Araknemu. | ||
* | ||
* Araknemu is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Araknemu is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* Copyright (c) 2017-2024 Vincent Quatrevieux | ||
*/ | ||
|
||
package fr.quatrevieux.araknemu.core.config.env; | ||
|
||
import fr.quatrevieux.araknemu.core.config.Pool; | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* Handle interpolation of environment variables in a pool | ||
*/ | ||
final class EnvPool implements Pool { | ||
private final Pool pool; | ||
private final Dotenv dotenv; | ||
private final Map<String, String> cache = new HashMap<>(); | ||
|
||
public EnvPool(Pool pool, Dotenv dotenv) { | ||
this.pool = pool; | ||
this.dotenv = dotenv; | ||
} | ||
|
||
@Override | ||
public boolean has(String key) { | ||
return pool.has(key); | ||
} | ||
|
||
@Override | ||
public @Nullable String get(String key) { | ||
final @Nullable String cachedValue = cache.get(key); | ||
|
||
if (cachedValue != null) { | ||
return cachedValue; | ||
} | ||
|
||
final String rawValue = pool.get(key); | ||
|
||
if (rawValue == null) { | ||
return null; | ||
} | ||
|
||
final String value = interpolate(rawValue); | ||
|
||
cache.put(key, value); | ||
|
||
return value; | ||
} | ||
|
||
@Override | ||
public List<String> getAll(String key) { | ||
return pool.getAll(key).stream().map(this::interpolate).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public Iterator<Map.Entry<String, String>> iterator() { | ||
return StreamSupport.stream(pool.spliterator(), false) | ||
.map(entry -> (Map.Entry<String, String>) Pair.of(entry.getKey(), interpolate(entry.getValue()))) | ||
.iterator() | ||
; | ||
} | ||
|
||
private String interpolate(String value) { | ||
return ExpressionParser.evaluate(value, dotenv::get); | ||
} | ||
} |
Oops, something went wrong.