Skip to content

Refactor application profiles to follow the Spring Boot convention #1744

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -72,11 +72,11 @@ SteVe is designed to run standalone, a java servlet container / web server (e.g.

3. Configure SteVe **before** building:

The basic configuration is defined in [main.properties](src/main/resources/config/prod/main.properties):
- You _must_ change [database configuration](src/main/resources/config/prod/main.properties#L9-L13)
- You _must_ change [the host](src/main/resources/config/prod/main.properties#L22) to the correct IP address of your server
- You _must_ change [web interface credentials](src/main/resources/config/prod/main.properties#L17-L18)
- You _can_ access the application via HTTPS, by [enabling it and setting the keystore properties](src/main/resources/config/prod/main.properties#L32-L35)
The basic configuration is defined in [application.properties](src/main/resources/application.properties):
- You _must_ change [database configuration](src/main/resources/application.properties)
- You _must_ change [the host](src/main/resources/application.properties) to the correct IP address of your server
- You _must_ change [web interface credentials](src/main/resources/application.properties)
- You _can_ access the application via HTTPS, by [enabling it and setting the keystore properties](src/main/resources/application.properties)

For advanced configuration please see the [Configuration wiki](https://github.com/steve-community/steve/wiki/Configuration)

@@ -101,7 +101,7 @@ SteVe is designed to run standalone, a java servlet container / web server (e.g.
If you prefer to build and start this project via docker (you can skip the steps 1, 4 and 5 from above), this can be done as follows: `docker compose up -d`

Because the docker compose file is written to build the project for you, you still have to change the project configuration settings from step 3.
Instead of changing the [main.properties in the prod directory](src/main/resources/config/prod/main.properties), you have to change the [main.properties in the docker directory](src/main/resources/config/docker/main.properties). There you have to change all configurations which are described in step 3.
Instead of changing the [application.properties](src/main/resources/application.properties), you have to change the [application-docker.properties](src/main/resources/application-docker.properties). There you have to change all configurations which are described in step 3.
The database password for the user "steve" has to be the same as you have configured it in the docker compose file.

With the default docker compose configuration, the web interface will be accessible at: `http://localhost:8180`
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -96,8 +96,13 @@
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources/config/${envName}</directory>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
<include>application-${envName}.properties</include>
<include>logback-spring-${envName}.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
@@ -266,7 +271,8 @@
</goals>
<configuration>
<files>
<file>src/main/resources/config/${envName}/main.properties</file>
<file>src/main/resources/application.properties</file>
<file>src/main/resources/application-${envName}.properties</file>
</files>
</configuration>
</execution>
10 changes: 4 additions & 6 deletions src/main/java/de/rwth/idsg/steve/ApplicationProfile.java
Original file line number Diff line number Diff line change
@@ -24,8 +24,10 @@
*/
public enum ApplicationProfile {
DEV,
TEST,
PROD;
DOCKER,
KUBERNETES,
PROD,
TEST;

public static ApplicationProfile fromName(String v) {
for (ApplicationProfile ap : ApplicationProfile.values()) {
@@ -35,8 +37,4 @@ public static ApplicationProfile fromName(String v) {
}
throw new IllegalArgumentException(v);
}

public boolean isProd() {
return this == PROD;
}
}
2 changes: 1 addition & 1 deletion src/main/java/de/rwth/idsg/steve/JettyServer.java
Original file line number Diff line number Diff line change
@@ -290,7 +290,7 @@ private static List<String> getPossibleIpAddresses() {
ips.removeIf("0.0.0.0"::equals);

if (ips.isEmpty()) {
// Well, we failed to read from system, fall back to main.properties.
// Well, we failed to read from system, fall back to application.properties.
// Better than nothing
ips.add(CONFIG.getJetty().getServerHost());
}
96 changes: 69 additions & 27 deletions src/main/java/de/rwth/idsg/steve/SteveConfiguration.java
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@
private final String timeZoneId = "UTC"; // or ZoneId.systemDefault().getId();

// -------------------------------------------------------------------------
// main.properties
// application.properties
// -------------------------------------------------------------------------

private final String contextPath;
@@ -62,58 +62,100 @@
private final Jetty jetty;

SteveConfiguration() {
PropertiesFileLoader p = new PropertiesFileLoader("main.properties");
PropertiesFileLoader p = new PropertiesFileLoader("application.properties");
profile = ApplicationProfile.fromName(p.getString("profile"));
PropertiesFileLoader overriden = new PropertiesFileLoader("application-" + profile.name().toLowerCase() + ".properties");

Check failure on line 67 in src/main/java/de/rwth/idsg/steve/SteveConfiguration.java

GitHub Actions / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 129). Raw Output: /github/workspace/./src/main/java/de/rwth/idsg/steve/SteveConfiguration.java:67:0: error: Line is longer than 120 characters (found 129). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)

contextPath = sanitizeContextPath(p.getOptionalString("context.path"));
steveVersion = p.getString("steve.version");
gitDescribe = useFallbackIfNotSet(p.getOptionalString("git.describe"), null);
contextPath = sanitizeContextPath(getOptionalString("context.path", p, overriden));
steveVersion = getString("steve.version", p, overriden);
gitDescribe = useFallbackIfNotSet(getOptionalString("git.describe", p, overriden), null);

profile = ApplicationProfile.fromName(p.getString("profile"));
System.setProperty("spring.profiles.active", profile.name().toLowerCase());
System.setProperty("logging.config", "classpath:logback-" + profile.name().toLowerCase() + ".xml");

jetty = Jetty.builder()
.serverHost(p.getString("server.host"))
.gzipEnabled(p.getBoolean("server.gzip.enabled"))
.httpEnabled(p.getBoolean("http.enabled"))
.httpPort(p.getInt("http.port"))
.httpsEnabled(p.getBoolean("https.enabled"))
.httpsPort(p.getInt("https.port"))
.keyStorePath(p.getOptionalString("keystore.path"))
.keyStorePassword(p.getOptionalString("keystore.password"))
.serverHost(getString("server.host", p, overriden))
.gzipEnabled(getBoolean("server.gzip.enabled", p, overriden))
.httpEnabled(getBoolean("http.enabled", p, overriden))
.httpPort(getInt("http.port", p, overriden))
.httpsEnabled(getBoolean("https.enabled", p, overriden))
.httpsPort(getInt("https.port", p, overriden))
.keyStorePath(getOptionalString("keystore.path", p, overriden))
.keyStorePassword(getOptionalString("keystore.password", p, overriden))
.build();

db = DB.builder()
.ip(p.getString("db.ip"))
.port(p.getInt("db.port"))
.schema(p.getString("db.schema"))
.userName(p.getString("db.user"))
.password(p.getString("db.password"))
.sqlLogging(p.getBoolean("db.sql.logging"))
.ip(getString("db.ip", p, overriden))
.port(getInt("db.port", p, overriden))
.schema(getString("db.schema", p, overriden))
.userName(getString("db.user", p, overriden))
.password(getString("db.password", p, overriden))
.sqlLogging(getBoolean("db.sql.logging", p, overriden))
.build();

PasswordEncoder encoder = new BCryptPasswordEncoder();

auth = Auth.builder()
.passwordEncoder(encoder)
.userName(p.getString("auth.user"))
.encodedPassword(encoder.encode(p.getString("auth.password")))
.userName(getString("auth.user", p, overriden))
.encodedPassword(encoder.encode(getString("auth.password", p, overriden)))
.build();

webApi = WebApi.builder()
.headerKey(p.getOptionalString("webapi.key"))
.headerValue(p.getOptionalString("webapi.value"))
.headerKey(getOptionalString("webapi.key", p, overriden))
.headerValue(getOptionalString("webapi.value", p, overriden))
.build();

ocpp = Ocpp.builder()
.autoRegisterUnknownStations(p.getOptionalBoolean("auto.register.unknown.stations"))
.chargeBoxIdValidationRegex(p.getOptionalString("charge-box-id.validation.regex"))
.autoRegisterUnknownStations(getOptionalBoolean("auto.register.unknown.stations", p, overriden))
.chargeBoxIdValidationRegex(getOptionalString("charge-box-id.validation.regex", p, overriden))
.wsSessionSelectStrategy(
WsSessionSelectStrategyEnum.fromName(p.getString("ws.session.select.strategy")))
WsSessionSelectStrategyEnum.fromName(getString("ws.session.select.strategy", p, overriden)))
.build();

validate();
}

private static String getOptionalString(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
String value = overriden.getOptionalString(key);
if (value == null) {
return p.getOptionalString(key);
}
return value;
}

private static String getString(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
String value = overriden.getOptionalString(key);
if (value == null) {
return p.getString(key);
}
return value;
}

private static boolean getOptionalBoolean(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
if (overriden.getOptionalString(key) == null) {
return p.getOptionalBoolean(key);
} else {
return overriden.getOptionalBoolean(key);
}
}

private static boolean getBoolean(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
if (overriden.getOptionalString(key) == null) {
return p.getBoolean(key);
} else {
return overriden.getOptionalBoolean(key);
}
}

private static int getInt(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
if (overriden.getOptionalString(key) == null) {
return p.getInt(key);
} else {
return overriden.getInt(key);
}
}

public String getSteveCompositeVersion() {
if (gitDescribe == null) {
return steveVersion;
24 changes: 0 additions & 24 deletions src/main/java/de/rwth/idsg/steve/utils/PropertiesFileLoader.java
Original file line number Diff line number Diff line change
@@ -18,16 +18,13 @@
*/
package de.rwth.idsg.steve.utils;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
@@ -96,18 +93,6 @@ public String getOptionalString(String key) {
return trim(key, s);
}

public List<String> getStringList(String key) {
String s = prop.getProperty(key);
if (Strings.isNullOrEmpty(s)) {
return Collections.emptyList();
}
s = resolveIfSystemEnv(s);
return Splitter.on(",")
.trimResults()
.omitEmptyStrings()
.splitToList(s);
}

public boolean getOptionalBoolean(String key) {
String s = getOptionalString(key);
if (s == null) {
@@ -119,15 +104,6 @@ public boolean getOptionalBoolean(String key) {
}
}

public Integer getOptionalInt(String key) {
String s = getOptionalString(key);
if (s == null) {
return null;
} else {
return Integer.parseInt(s);
}
}

// -------------------------------------------------------------------------
// Private helpers
// -------------------------------------------------------------------------
2 changes: 2 additions & 0 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### DO NOT MODIFY ###
db.sql.logging = true
11 changes: 11 additions & 0 deletions src/main/resources/application-docker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Database configuration
#
db.ip = mariadb

# Jetty configuration
#
server.host = 0.0.0.0

# Jetty HTTP configuration
#
http.port = 8180
26 changes: 26 additions & 0 deletions src/main/resources/application-kubernetes.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Database configuration
#
db.ip=$DB_HOST
db.port=$DB_PORT
db.schema=$DB_DATABASE
db.user=$DB_USERNAME
db.password=$DB_PASSWORD

# Credentials for Web interface access
#
auth.user=$ADMIN_USERNAME
auth.password=$ADMIN_PASSWORD

# The header key and value for Web API access using API key authorization.
# Both must be set for Web APIs to be enabled. Otherwise, we will block all calls.
#
webapi.key=$WEBAPI_KEY
webapi.value=$WEBAPI_VALUE

# Jetty configuration
#
server.host = 0.0.0.0

# Jetty HTTP configuration
#
http.port = 8180
Empty file.
6 changes: 6 additions & 0 deletions src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Database configuration
#
db.schema = stevedb_test_2aa6a783d47d

### DO NOT MODIFY ###
db.sql.logging = true
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ webapi.value =
# Jetty configuration
#
server.host = 127.0.0.1
server.gzip.enabled = true
server.gzip.enabled = false

# Jetty HTTP configuration
#
@@ -65,4 +65,4 @@ charge-box-id.validation.regex =
steve.version = ${project.version}
git.describe = ${git.commit.id.describe}
db.sql.logging = false
profile = prod
profile = ${envName}
68 changes: 0 additions & 68 deletions src/main/resources/config/dev/main.properties

This file was deleted.

68 changes: 0 additions & 68 deletions src/main/resources/config/docker/main.properties

This file was deleted.

68 changes: 0 additions & 68 deletions src/main/resources/config/kubernetes/main.properties

This file was deleted.

68 changes: 0 additions & 68 deletions src/main/resources/config/test/main.properties

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<configuration>

<springProfile name="dev">
<include resource="logback-spring-dev.xml"/>
</springProfile>

<springProfile name="docker">
<include resource="logback-spring-docker.xml"/>
</springProfile>

<springProfile name="kubernetes">
<include resource="logback-spring-kubernetes.xml"/>
</springProfile>

<springProfile name="prod">
<include resource="logback-spring-prod.xml"/>
</springProfile>

<springProfile name="test">
<include resource="logback-spring-test.xml"/>
</springProfile>

</configuration>