Skip to content

Commit 3d7a832

Browse files
committed
Refactor application profiles to follow the Spring Boot convention
Fix #1735
1 parent dff652c commit 3d7a832

22 files changed

+158
-340
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ SteVe is designed to run standalone, a java servlet container / web server (e.g.
7272
7373
3. Configure SteVe **before** building:
7474
75-
The basic configuration is defined in [main.properties](src/main/resources/config/prod/main.properties):
76-
- You _must_ change [database configuration](src/main/resources/config/prod/main.properties#L9-L13)
77-
- You _must_ change [the host](src/main/resources/config/prod/main.properties#L22) to the correct IP address of your server
78-
- You _must_ change [web interface credentials](src/main/resources/config/prod/main.properties#L17-L18)
79-
- You _can_ access the application via HTTPS, by [enabling it and setting the keystore properties](src/main/resources/config/prod/main.properties#L32-L35)
75+
The basic configuration is defined in [application.properties](src/main/resources/application.properties):
76+
- You _must_ change [database configuration](src/main/resources/application.properties)
77+
- You _must_ change [the host](src/main/resources/application.properties) to the correct IP address of your server
78+
- You _must_ change [web interface credentials](src/main/resources/application.properties)
79+
- You _can_ access the application via HTTPS, by [enabling it and setting the keystore properties](src/main/resources/application.properties)
8080
8181
For advanced configuration please see the [Configuration wiki](https://github.com/steve-community/steve/wiki/Configuration)
8282
@@ -101,7 +101,7 @@ SteVe is designed to run standalone, a java servlet container / web server (e.g.
101101
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`
102102
103103
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.
104-
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.
104+
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.
105105
The database password for the user "steve" has to be the same as you have configured it in the docker compose file.
106106
107107
With the default docker compose configuration, the web interface will be accessible at: `http://localhost:8180`

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,13 @@
9696
<finalName>${project.artifactId}</finalName>
9797
<resources>
9898
<resource>
99-
<directory>src/main/resources/config/${envName}</directory>
99+
<directory>src/main/resources</directory>
100100
<filtering>true</filtering>
101+
<includes>
102+
<include>application.properties</include>
103+
<include>application-${envName}.properties</include>
104+
<include>logback-spring-${envName}.xml</include>
105+
</includes>
101106
</resource>
102107
<resource>
103108
<directory>src/main/resources</directory>
@@ -266,7 +271,8 @@
266271
</goals>
267272
<configuration>
268273
<files>
269-
<file>src/main/resources/config/${envName}/main.properties</file>
274+
<file>src/main/resources/application.properties</file>
275+
<file>src/main/resources/application-${envName}.properties</file>
270276
</files>
271277
</configuration>
272278
</execution>

src/main/java/de/rwth/idsg/steve/ApplicationProfile.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
*/
2525
public enum ApplicationProfile {
2626
DEV,
27-
TEST,
28-
PROD;
27+
DOCKER,
28+
KUBERNETES,
29+
PROD,
30+
TEST;
2931

3032
public static ApplicationProfile fromName(String v) {
3133
for (ApplicationProfile ap : ApplicationProfile.values()) {
@@ -35,8 +37,4 @@ public static ApplicationProfile fromName(String v) {
3537
}
3638
throw new IllegalArgumentException(v);
3739
}
38-
39-
public boolean isProd() {
40-
return this == PROD;
41-
}
4240
}

src/main/java/de/rwth/idsg/steve/JettyServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ private static List<String> getPossibleIpAddresses() {
290290
ips.removeIf("0.0.0.0"::equals);
291291

292292
if (ips.isEmpty()) {
293-
// Well, we failed to read from system, fall back to main.properties.
293+
// Well, we failed to read from system, fall back to application.properties.
294294
// Better than nothing
295295
ips.add(CONFIG.getJetty().getServerHost());
296296
}

src/main/java/de/rwth/idsg/steve/SteveConfiguration.java

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public enum SteveConfiguration {
4848
private final String timeZoneId = "UTC"; // or ZoneId.systemDefault().getId();
4949

5050
// -------------------------------------------------------------------------
51-
// main.properties
51+
// application.properties
5252
// -------------------------------------------------------------------------
5353

5454
private final String contextPath;
@@ -62,58 +62,100 @@ public enum SteveConfiguration {
6262
private final Jetty jetty;
6363

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

67-
contextPath = sanitizeContextPath(p.getOptionalString("context.path"));
68-
steveVersion = p.getString("steve.version");
69-
gitDescribe = useFallbackIfNotSet(p.getOptionalString("git.describe"), null);
69+
contextPath = sanitizeContextPath(getOptionalString("context.path", p, overriden));
70+
steveVersion = getString("steve.version", p, overriden);
71+
gitDescribe = useFallbackIfNotSet(getOptionalString("git.describe", p, overriden), null);
7072

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

7476
jetty = Jetty.builder()
75-
.serverHost(p.getString("server.host"))
76-
.gzipEnabled(p.getBoolean("server.gzip.enabled"))
77-
.httpEnabled(p.getBoolean("http.enabled"))
78-
.httpPort(p.getInt("http.port"))
79-
.httpsEnabled(p.getBoolean("https.enabled"))
80-
.httpsPort(p.getInt("https.port"))
81-
.keyStorePath(p.getOptionalString("keystore.path"))
82-
.keyStorePassword(p.getOptionalString("keystore.password"))
77+
.serverHost(getString("server.host", p, overriden))
78+
.gzipEnabled(getBoolean("server.gzip.enabled", p, overriden))
79+
.httpEnabled(getBoolean("http.enabled", p, overriden))
80+
.httpPort(getInt("http.port", p, overriden))
81+
.httpsEnabled(getBoolean("https.enabled", p, overriden))
82+
.httpsPort(getInt("https.port", p, overriden))
83+
.keyStorePath(getOptionalString("keystore.path", p, overriden))
84+
.keyStorePassword(getOptionalString("keystore.password", p, overriden))
8385
.build();
8486

8587
db = DB.builder()
86-
.ip(p.getString("db.ip"))
87-
.port(p.getInt("db.port"))
88-
.schema(p.getString("db.schema"))
89-
.userName(p.getString("db.user"))
90-
.password(p.getString("db.password"))
91-
.sqlLogging(p.getBoolean("db.sql.logging"))
88+
.ip(getString("db.ip", p, overriden))
89+
.port(getInt("db.port", p, overriden))
90+
.schema(getString("db.schema", p, overriden))
91+
.userName(getString("db.user", p, overriden))
92+
.password(getString("db.password", p, overriden))
93+
.sqlLogging(getBoolean("db.sql.logging", p, overriden))
9294
.build();
9395

9496
PasswordEncoder encoder = new BCryptPasswordEncoder();
9597

9698
auth = Auth.builder()
9799
.passwordEncoder(encoder)
98-
.userName(p.getString("auth.user"))
99-
.encodedPassword(encoder.encode(p.getString("auth.password")))
100+
.userName(getString("auth.user", p, overriden))
101+
.encodedPassword(encoder.encode(getString("auth.password", p, overriden)))
100102
.build();
101103

102104
webApi = WebApi.builder()
103-
.headerKey(p.getOptionalString("webapi.key"))
104-
.headerValue(p.getOptionalString("webapi.value"))
105+
.headerKey(getOptionalString("webapi.key", p, overriden))
106+
.headerValue(getOptionalString("webapi.value", p, overriden))
105107
.build();
106108

107109
ocpp = Ocpp.builder()
108-
.autoRegisterUnknownStations(p.getOptionalBoolean("auto.register.unknown.stations"))
109-
.chargeBoxIdValidationRegex(p.getOptionalString("charge-box-id.validation.regex"))
110+
.autoRegisterUnknownStations(getOptionalBoolean("auto.register.unknown.stations", p, overriden))
111+
.chargeBoxIdValidationRegex(getOptionalString("charge-box-id.validation.regex", p, overriden))
110112
.wsSessionSelectStrategy(
111-
WsSessionSelectStrategyEnum.fromName(p.getString("ws.session.select.strategy")))
113+
WsSessionSelectStrategyEnum.fromName(getString("ws.session.select.strategy", p, overriden)))
112114
.build();
113115

114116
validate();
115117
}
116118

119+
private static String getOptionalString(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
120+
String value = overriden.getOptionalString(key);
121+
if (value == null) {
122+
return p.getOptionalString(key);
123+
}
124+
return value;
125+
}
126+
127+
private static String getString(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
128+
String value = overriden.getOptionalString(key);
129+
if (value == null) {
130+
return p.getString(key);
131+
}
132+
return value;
133+
}
134+
135+
private static boolean getOptionalBoolean(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
136+
if (overriden.getOptionalString(key) == null) {
137+
return p.getOptionalBoolean(key);
138+
} else {
139+
return overriden.getOptionalBoolean(key);
140+
}
141+
}
142+
143+
private static boolean getBoolean(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
144+
if (overriden.getOptionalString(key) == null) {
145+
return p.getBoolean(key);
146+
} else {
147+
return overriden.getOptionalBoolean(key);
148+
}
149+
}
150+
151+
private static int getInt(String key, PropertiesFileLoader p, PropertiesFileLoader overriden) {
152+
if (overriden.getOptionalString(key) == null) {
153+
return p.getInt(key);
154+
} else {
155+
return overriden.getInt(key);
156+
}
157+
}
158+
117159
public String getSteveCompositeVersion() {
118160
if (gitDescribe == null) {
119161
return steveVersion;

src/main/java/de/rwth/idsg/steve/utils/PropertiesFileLoader.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818
*/
1919
package de.rwth.idsg.steve.utils;
2020

21-
import com.google.common.base.Splitter;
2221
import com.google.common.base.Strings;
2322
import lombok.extern.slf4j.Slf4j;
2423

2524
import java.io.FileInputStream;
2625
import java.io.FileNotFoundException;
2726
import java.io.IOException;
2827
import java.io.InputStream;
29-
import java.util.Collections;
30-
import java.util.List;
3128
import java.util.Properties;
3229

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

99-
public List<String> getStringList(String key) {
100-
String s = prop.getProperty(key);
101-
if (Strings.isNullOrEmpty(s)) {
102-
return Collections.emptyList();
103-
}
104-
s = resolveIfSystemEnv(s);
105-
return Splitter.on(",")
106-
.trimResults()
107-
.omitEmptyStrings()
108-
.splitToList(s);
109-
}
110-
11196
public boolean getOptionalBoolean(String key) {
11297
String s = getOptionalString(key);
11398
if (s == null) {
@@ -119,15 +104,6 @@ public boolean getOptionalBoolean(String key) {
119104
}
120105
}
121106

122-
public Integer getOptionalInt(String key) {
123-
String s = getOptionalString(key);
124-
if (s == null) {
125-
return null;
126-
} else {
127-
return Integer.parseInt(s);
128-
}
129-
}
130-
131107
// -------------------------------------------------------------------------
132108
// Private helpers
133109
// -------------------------------------------------------------------------
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### DO NOT MODIFY ###
2+
db.sql.logging = true
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Database configuration
2+
#
3+
db.ip = mariadb
4+
5+
# Jetty configuration
6+
#
7+
server.host = 0.0.0.0
8+
9+
# Jetty HTTP configuration
10+
#
11+
http.port = 8180
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Database configuration
2+
#
3+
db.ip=$DB_HOST
4+
db.port=$DB_PORT
5+
db.schema=$DB_DATABASE
6+
db.user=$DB_USERNAME
7+
db.password=$DB_PASSWORD
8+
9+
# Credentials for Web interface access
10+
#
11+
auth.user=$ADMIN_USERNAME
12+
auth.password=$ADMIN_PASSWORD
13+
14+
# The header key and value for Web API access using API key authorization.
15+
# Both must be set for Web APIs to be enabled. Otherwise, we will block all calls.
16+
#
17+
webapi.key=$WEBAPI_KEY
18+
webapi.value=$WEBAPI_VALUE
19+
20+
# Jetty configuration
21+
#
22+
server.host = 0.0.0.0
23+
24+
# Jetty HTTP configuration
25+
#
26+
http.port = 8180

src/main/resources/application-prod.properties

Whitespace-only changes.

0 commit comments

Comments
 (0)