Skip to content

Commit 5f917b0

Browse files
authored
Merge pull request #32 from G8XSU/dockerize-2
Read application properties from env first and add Docker configuration.
2 parents c21fd71 + fe1fc92 commit 5f917b0

File tree

7 files changed

+101
-39
lines changed

7 files changed

+101
-39
lines changed

.github/workflows/build-and-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
./gradlew --version
6161
./gradlew build
6262
63-
docker cp app/build/libs/app-1.0.war tomcat:/usr/local/tomcat/webapps/vss.war
63+
docker cp app/build/libs/vss-1.0.war tomcat:/usr/local/tomcat/webapps/vss.war
6464
6565
- name: Hit endpoint to verify service is up
6666
run: |

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Use official Tomcat base image
2+
FROM tomcat:jre17
3+
4+
# Copy WAR file
5+
COPY app/build/libs/vss-1.0.war /usr/local/tomcat/webapps/vss.war
6+
7+
ENV vss.jdbc.url="jdbc:postgresql://postgres:5432/postgres"
8+
ENV vss.jdbc.username=postgres
9+
ENV vss.jdbc.password=YOU_MUST_CHANGE_THIS_PASSWORD
10+
11+
EXPOSE 8080
12+
CMD ["catalina.sh", "run"]

app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ idea {
3030
group 'org.vss'
3131
version '1.0'
3232

33+
war {
34+
archiveFileName = "vss-${project.version}.war"
35+
}
36+
3337
dependencies {
3438
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
3539

app/src/main/java/org/vss/guice/BaseModule.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,48 @@ class HikariCPDataSource {
5252

5353
static {
5454
try (InputStream input = HikariCPDataSource.class.getClassLoader()
55-
.getResourceAsStream("hikariJdbc.properties")) {
56-
Properties hikariJdbcProperties = new Properties();
57-
hikariJdbcProperties.load(input);
55+
.getResourceAsStream("application.properties")) {
56+
Properties applicationProperties = new Properties();
57+
applicationProperties.load(input);
5858

59-
config.setJdbcUrl(hikariJdbcProperties.getProperty("jdbc.url"));
60-
config.setUsername(hikariJdbcProperties.getProperty("jdbc.username"));
61-
config.setPassword(hikariJdbcProperties.getProperty("jdbc.password"));
59+
config.setJdbcUrl(getEnvOrConfigProperty("vss.jdbc.url", applicationProperties));
60+
config.setUsername(getEnvOrConfigProperty("vss.jdbc.username", applicationProperties));
61+
config.setPassword(getEnvOrConfigProperty("vss.jdbc.password", applicationProperties));
6262

6363
config.setMaximumPoolSize(
64-
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.maxPoolSize")));
64+
Integer.parseInt(getEnvOrConfigProperty("vss.hikaricp.maxPoolSize", applicationProperties)));
6565
config.setMinimumIdle(
66-
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.minimumIdle")));
66+
Integer.parseInt(getEnvOrConfigProperty("vss.hikaricp.minimumIdle", applicationProperties)));
6767
config.setConnectionTimeout(
68-
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.connectionTimeout")));
68+
Long.parseLong(getEnvOrConfigProperty("vss.hikaricp.connectionTimeout", applicationProperties)));
6969
config.setIdleTimeout(
70-
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.idleTimeout")));
70+
Long.parseLong(getEnvOrConfigProperty("vss.hikaricp.idleTimeout", applicationProperties)));
7171
config.setMaxLifetime(
72-
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.maxLifetime")));
72+
Long.parseLong(getEnvOrConfigProperty("vss.hikaricp.maxLifetime", applicationProperties)));
7373

7474
config.addDataSourceProperty("cachePrepStmts",
75-
hikariJdbcProperties.getProperty("hikaricp.cachePrepStmts"));
75+
getEnvOrConfigProperty("vss.hikaricp.cachePrepStmts", applicationProperties));
7676
config.addDataSourceProperty("prepStmtCacheSize",
77-
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSize"));
77+
getEnvOrConfigProperty("vss.hikaricp.prepStmtCacheSize", applicationProperties));
7878
config.addDataSourceProperty("prepStmtCacheSqlLimit",
79-
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSqlLimit"));
79+
getEnvOrConfigProperty("vss.hikaricp.prepStmtCacheSqlLimit", applicationProperties));
8080

8181
dataSource = new HikariDataSource(config);
8282
} catch (IOException e) {
83-
throw new RuntimeException("Unable to read hikariJdbcProperties from resources");
83+
throw new RuntimeException("Unable to read application.properties from resources");
8484
}
8585
}
8686

87+
// Retrieves the value of a specified property, first checking environment variables,
88+
// then falling back to provided configuration properties if the environment variable is not set.
89+
private static String getEnvOrConfigProperty(String key, Properties hikariJdbcProperties) {
90+
String propertyValue = System.getenv(key);
91+
if (StringUtils.isBlank(propertyValue)) {
92+
propertyValue = hikariJdbcProperties.getProperty(key);
93+
}
94+
return propertyValue;
95+
}
96+
8797
private HikariCPDataSource() {
8898
}
8999
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Application Properties File
2+
# Contains default application properties, these are meant to be changed according to application needs.
3+
# Each property can be overridden by setting an environment variable with the same name.
4+
# For example, to override 'vss.jdbc.url', set an environment variable 'vss.jdbc.url' with the new value.
5+
6+
vss.jdbc.url=jdbc:postgresql://localhost:5432/postgres
7+
vss.jdbc.username=postgres
8+
vss.jdbc.password=YOU_MUST_CHANGE_THIS_PASSWORD
9+
10+
# Idle Timeout
11+
vss.hikaricp.minimumIdle=10
12+
13+
# Set connectionTimeout to 30 secs
14+
vss.hikaricp.connectionTimeout=30000
15+
16+
# Set idle timeout to 10 minutes
17+
vss.hikaricp.idleTimeout=600000
18+
19+
# Set Maximum lifetime of a connection to 30minutes
20+
vss.hikaricp.maxLifetime=1800000
21+
22+
# Performance Optimizations
23+
vss.hikaricp.maxPoolSize=50
24+
vss.hikaricp.cachePrepStmts=true
25+
vss.hikaricp.prepStmtCacheSize=250
26+
vss.hikaricp.prepStmtCacheSqlLimit=2048

app/src/main/resources/hikariJdbc.properties

Lines changed: 0 additions & 23 deletions
This file was deleted.

docker-compose.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
version: '3.8'
2+
services:
3+
postgres:
4+
image: postgres:15
5+
environment:
6+
POSTGRES_DB: postgres
7+
POSTGRES_USER: postgres
8+
POSTGRES_PASSWORD: YOU_MUST_CHANGE_THIS_PASSWORD
9+
volumes:
10+
- postgres-data:/var/lib/postgresql/data
11+
- ./app/src/main/java/org/vss/impl/postgres/sql/v0_create_vss_db.sql:/docker-entrypoint-initdb.d/init.sql
12+
ports:
13+
- "5432:5432"
14+
networks:
15+
- app-network
16+
17+
tomcat:
18+
build:
19+
context: .
20+
container_name: tomcat
21+
depends_on:
22+
- postgres
23+
ports:
24+
- "8080:8080"
25+
networks:
26+
- app-network
27+
28+
volumes:
29+
postgres-data:
30+
31+
networks:
32+
app-network:
33+
driver: bridge

0 commit comments

Comments
 (0)