Skip to content

Read application properties from env first and add Docker configuration. #32

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

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
./gradlew --version
./gradlew build

docker cp app/build/libs/app-1.0.war tomcat:/usr/local/tomcat/webapps/vss.war
docker cp app/build/libs/vss-1.0.war tomcat:/usr/local/tomcat/webapps/vss.war

- name: Hit endpoint to verify service is up
run: |
Expand Down
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Use official Tomcat base image
FROM tomcat:jre17

# Copy WAR file
COPY app/build/libs/vss-1.0.war /usr/local/tomcat/webapps/vss.war

ENV vss.jdbc.url="jdbc:postgresql://postgres:5432/postgres"
ENV vss.jdbc.username=postgres
ENV vss.jdbc.password=YOU_MUST_CHANGE_THIS_PASSWORD

EXPOSE 8080
CMD ["catalina.sh", "run"]
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ idea {
group 'org.vss'
version '1.0'

war {
archiveFileName = "vss-${project.version}.war"
}

dependencies {
implementation "com.google.protobuf:protobuf-java:$protobufVersion"

Expand Down
40 changes: 25 additions & 15 deletions app/src/main/java/org/vss/guice/BaseModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,48 @@ class HikariCPDataSource {

static {
try (InputStream input = HikariCPDataSource.class.getClassLoader()
.getResourceAsStream("hikariJdbc.properties")) {
Properties hikariJdbcProperties = new Properties();
hikariJdbcProperties.load(input);
.getResourceAsStream("application.properties")) {
Properties applicationProperties = new Properties();
applicationProperties.load(input);

config.setJdbcUrl(hikariJdbcProperties.getProperty("jdbc.url"));
config.setUsername(hikariJdbcProperties.getProperty("jdbc.username"));
config.setPassword(hikariJdbcProperties.getProperty("jdbc.password"));
config.setJdbcUrl(getEnvOrConfigProperty("vss.jdbc.url", applicationProperties));
config.setUsername(getEnvOrConfigProperty("vss.jdbc.username", applicationProperties));
config.setPassword(getEnvOrConfigProperty("vss.jdbc.password", applicationProperties));

config.setMaximumPoolSize(
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.maxPoolSize")));
Integer.parseInt(getEnvOrConfigProperty("vss.hikaricp.maxPoolSize", applicationProperties)));
config.setMinimumIdle(
Integer.parseInt(hikariJdbcProperties.getProperty("hikaricp.minimumIdle")));
Integer.parseInt(getEnvOrConfigProperty("vss.hikaricp.minimumIdle", applicationProperties)));
config.setConnectionTimeout(
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.connectionTimeout")));
Long.parseLong(getEnvOrConfigProperty("vss.hikaricp.connectionTimeout", applicationProperties)));
config.setIdleTimeout(
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.idleTimeout")));
Long.parseLong(getEnvOrConfigProperty("vss.hikaricp.idleTimeout", applicationProperties)));
config.setMaxLifetime(
Long.parseLong(hikariJdbcProperties.getProperty("hikaricp.maxLifetime")));
Long.parseLong(getEnvOrConfigProperty("vss.hikaricp.maxLifetime", applicationProperties)));

config.addDataSourceProperty("cachePrepStmts",
hikariJdbcProperties.getProperty("hikaricp.cachePrepStmts"));
getEnvOrConfigProperty("vss.hikaricp.cachePrepStmts", applicationProperties));
config.addDataSourceProperty("prepStmtCacheSize",
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSize"));
getEnvOrConfigProperty("vss.hikaricp.prepStmtCacheSize", applicationProperties));
config.addDataSourceProperty("prepStmtCacheSqlLimit",
hikariJdbcProperties.getProperty("hikaricp.prepStmtCacheSqlLimit"));
getEnvOrConfigProperty("vss.hikaricp.prepStmtCacheSqlLimit", applicationProperties));

dataSource = new HikariDataSource(config);
} catch (IOException e) {
throw new RuntimeException("Unable to read hikariJdbcProperties from resources");
throw new RuntimeException("Unable to read application.properties from resources");
}
}

// Retrieves the value of a specified property, first checking environment variables,
// then falling back to provided configuration properties if the environment variable is not set.
private static String getEnvOrConfigProperty(String key, Properties hikariJdbcProperties) {
String propertyValue = System.getenv(key);
if (StringUtils.isBlank(propertyValue)) {
propertyValue = hikariJdbcProperties.getProperty(key);
}
return propertyValue;
}

private HikariCPDataSource() {
}
}
26 changes: 26 additions & 0 deletions app/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Application Properties File
# Contains default application properties, these are meant to be changed according to application needs.
# Each property can be overridden by setting an environment variable with the same name.
# For example, to override 'vss.jdbc.url', set an environment variable 'vss.jdbc.url' with the new value.

vss.jdbc.url=jdbc:postgresql://localhost:5432/postgres
vss.jdbc.username=postgres
vss.jdbc.password=YOU_MUST_CHANGE_THIS_PASSWORD

# Idle Timeout
vss.hikaricp.minimumIdle=10

# Set connectionTimeout to 30 secs
vss.hikaricp.connectionTimeout=30000

# Set idle timeout to 10 minutes
vss.hikaricp.idleTimeout=600000

# Set Maximum lifetime of a connection to 30minutes
vss.hikaricp.maxLifetime=1800000

# Performance Optimizations
vss.hikaricp.maxPoolSize=50
vss.hikaricp.cachePrepStmts=true
vss.hikaricp.prepStmtCacheSize=250
vss.hikaricp.prepStmtCacheSqlLimit=2048
23 changes: 0 additions & 23 deletions app/src/main/resources/hikariJdbc.properties

This file was deleted.

33 changes: 33 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '3.8'
services:
postgres:
image: postgres:15
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: YOU_MUST_CHANGE_THIS_PASSWORD
volumes:
- postgres-data:/var/lib/postgresql/data
- ./app/src/main/java/org/vss/impl/postgres/sql/v0_create_vss_db.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "5432:5432"
networks:
- app-network

tomcat:
build:
context: .
container_name: tomcat
depends_on:
- postgres
ports:
- "8080:8080"
networks:
- app-network

volumes:
postgres-data:

networks:
app-network:
driver: bridge
Loading