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

Multi-Profile Support #81

Merged
merged 5 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ Config.onChangeBool("myapp.foo", newBooleanValue -> {

```


## Loading properties

Config loads properties from expected locations as well as via command line arguments.
Expand All @@ -77,11 +76,17 @@ Below is the how it looks for configuration properties.

- loads via system property `props.file` or environment variable `PROPS_FILE` (if defined)

- loads via system property `config.profiles` or environment variable `CONFIG_PROFILES` (if defined).

Setting the `config.profiles` or environment variable `CONFIG_PROFILES` will cause avaje config to load the property files in the form `application-${profile}.properties` (will also work for yml/yaml files).

For example, if you set the `config.profiles` to `dev,docker` it will attempt to load `application-dev.properties` and `application-docker.properties`.

- loads via `load.properties` property.

We can define a `load.properties` property which has name of property file in resource folder, or path locations for other properties/yaml files to load.

`load.properties` is pretty versatile and can even be chained. For example, in your main application properties, you can have `load.properties=application-${profile:local}.properties` to load based on the profile environment variable/-Dprofile JVM arg, and in the loaded properties you can add `load.properties` there to load more properties and so on.
`load.properties` is pretty versatile and can even be chained. For example, in your main application properties, you can have `load.properties=application-${profile:local}.properties` to load based on another property, and in the loaded properties you can add `load.properties` there to load more properties, and so on.

Example application.properties:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void loadEnvironmentVars() {
initSystemProperty(System.getenv("POD_NAMESPACE"), "app.environment");
initSystemProperty(System.getenv("POD_VERSION"), "app.version");
initSystemProperty(System.getenv("POD_IP"), "app.ipAddress");
initSystemProperty(System.getenv("CONFIG_PROFILES"), "config.profiles");
}

private void initSystemProperty(String envValue, String key) {
Expand Down Expand Up @@ -141,6 +142,13 @@ String indirectLocation() {
return indirectLocation == null ? null : indirectLocation.value();
}

String profiles() {
final CoreEntry indirectLocation = map.get("config.profiles");
return indirectLocation == null
? System.getProperty("config.profiles")
: indirectLocation.value();
}

/**
* Return the number of properties resources loaded.
*/
Expand Down
13 changes: 13 additions & 0 deletions avaje-config/src/main/java/io/avaje/config/InitialLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ private void loadViaIndirection() {
}
}

/** Load configuration defined by a <em>config.profiles</em> property. */
private void loadViaProfiles() {
final String paths = loadContext.profiles();
if (paths != null) {
for (final String path : splitPaths(paths)) {
final var profile = loadContext.eval(path);
loadProperties("application-" + profile + ".properties", RESOURCE);
loadYaml("application-" + profile + ".yaml", RESOURCE);
loadYaml("application-" + profile + ".yml", RESOURCE);
}
SentryMan marked this conversation as resolved.
Show resolved Hide resolved
}
}

private void loadViaPaths(String paths) {
for (String path : splitPaths(paths)) {
loadWithExtensionCheck(loadContext.eval(path));
Expand Down