Skip to content

Commit

Permalink
refactor: move service hostname config to env
Browse files Browse the repository at this point in the history
  • Loading branch information
tyknkd committed May 13, 2024
1 parent ff6f71d commit a960b4f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 43 deletions.
8 changes: 6 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export PROJECT_VERSION=0.0.1
export COLLECTOR_HOST=data-collector
export ANALYZER_HOST=data-analyzer
export WEBSERVER_HOST=web-server
export COLLECTOR_DB=collector_db
export ANALYZER_DB=analyzer_db
export WEBSERVER_DB=webserver_db
Expand All @@ -7,8 +10,8 @@ export ANALYZER_TEST_DB=analyzer_test_db
export WEBSERVER_TEST_DB=webserver_test_db
export POSTGRES_DB=${COLLECTOR_DB}
export ADDITIONAL_POSTGRES_DB=${ANALYZER_DB},${WEBSERVER_DB},${COLLECTOR_TEST_DB},${ANALYZER_TEST_DB},${WEBSERVER_TEST_DB}
export POSTGRES_HOST_PORT=5433
export POSTGRES_CONTAINER_PORT=5432
export POSTGRES_HOST=db
export POSTGRES_PORT=5432
export POSTGRES_USER=newsanalyzer
export COLLECTOR_EXCHANGE=collector_exchange
export ANALYZER_EXCHANGE=analyzer_exchange
Expand All @@ -18,6 +21,7 @@ export COLLECTOR_QUEUE=collector_queue
export ANALYZER_QUEUE=analyzer_queue
export COLLECTOR_ROUTING_KEY=collector_key
export ANALYZER_ROUTING_KEY=analyzer_key
export RABBITMQ_HOST=mq
export RABBITMQ_PORT=5672
export RABBITMQ_METRICS_PORT=15692
export PROMETHEUS_PORT=9090
Expand Down
10 changes: 6 additions & 4 deletions app-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/bin/sh

# Extract Docker secret file to app environment
export POSTGRES_PASSWORD=${POSTGRES_PASSWORD:=$(cat "${POSTGRES_PASSWORD_FILE}")}
if [ "${POSTGRES_PASSWORD}" = "" ]; then
# Extract Docker secret file to app environment
export POSTGRES_PASSWORD=${POSTGRES_PASSWORD:=$(cat "${POSTGRES_PASSWORD_FILE}")}
fi

if [ "${APP}" = "data-collector" ]; then
if [ "${APP}" = "${COLLECTOR_HOST}" ]; then
export NEWS_API_KEY=${NEWS_API_KEY:=$(cat "${NEWS_API_KEY_FILE}")}
java -jar /app/"${APP}".jar
elif [ "${APP}" = "data-analyzer" ]; then
elif [ "${APP}" = "${ANALYZER_HOST}" ]; then
# Java options for Spark-Java17 compatibility
# https://github.com/apache/spark/blob/v3.3.0/launcher/src/main/java/org/apache/spark/launcher/JavaModuleOptions.java
java \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ open class DatabaseTemplate(dbName: String, tables: List<Table>) {
var database: Database
init {
val driverClassName = "org.postgresql.Driver"
lateinit var host: String
lateinit var port: String
if (System.getenv("OS_ENV") == "container") {
host = "db"
port = System.getenv("POSTGRES_CONTAINER_PORT")
val port = System.getenv("POSTGRES_PORT")
val host = if (System.getenv("OS_ENV") == "container") {
System.getenv("POSTGRES_HOST")
} else {
host = "localhost"
port = System.getenv("POSTGRES_HOST_PORT")
"localhost"
}
val password = System.getenv("POSTGRES_PASSWORD")
if (password == null || password == "") {
throw RuntimeException("Database password file (secrets/postgres_password.txt) is empty or missing")
throw RuntimeException("Database password environment variable 'POSTGRES_PASSWORD' is empty or missing")
}
val user = System.getenv("POSTGRES_USER")
val jdbcUrl = "jdbc:postgresql://$host:$port/$dbName?reWriteBatchedInserts=true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ package io.newsanalyzer.httpsupport
class HostPaths {
fun getAnalyzerPath(): String {
val port = System.getenv("ANALYZER_PORT")
return if (System.getenv("OS_ENV") == "container") {
"data-analyzer:$port"
val host = if (System.getenv("OS_ENV") == "container") {
System.getenv("ANALYZER_HOST")
} else {
"localhost:$port"
"localhost"
}
return "$host:$port"
}
fun getCollectorPath(): String {
val port = System.getenv("COLLECTOR_PORT")
return if (System.getenv("OS_ENV") == "container") {
"data-collector:$port"
val host = if (System.getenv("OS_ENV") == "container") {
System.getenv("COLLECTOR_HOST")
} else {
"localhost:$port"
"localhost"
}
return "$host:$port"
}
fun getWebServerPath(): String {
val port = System.getenv("WEBSERVER_PORT")
return if (System.getenv("OS_ENV") == "container") {
"web-server:$port"
val host = if (System.getenv("OS_ENV") == "container") {
System.getenv("COLLECTOR_HOST")
} else {
"localhost:$port"
"localhost"
}
return "$host:$port"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ class Exchange(val name: String, val queue: String, val routingKey: String) {

private fun getRabbitMqUri(): String {
val port = System.getenv("RABBITMQ_PORT")?:5672
return if (System.getenv("OS_ENV") == "container") {
"amqp://mq:$port"
val host = if (System.getenv("OS_ENV") == "container") {
System.getenv("RABBITMQ_HOST")
} else {
"amqp://localhost:$port"
"localhost"
}
return "amqp://$host:$port"
}
}
50 changes: 33 additions & 17 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ services:
context: .
target: app
args:
APP: web-server
APP: ${WEBSERVER_HOST}
image: tyknkd/newsanalyzer-webserver:${PROJECT_VERSION}
container_name: web-server
container_name: ${WEBSERVER_HOST}
environment:
PORT: ${WEBSERVER_PORT}
COLLECTOR_HOST: ${COLLECTOR_HOST}
ANALYZER_HOST: ${ANALYZER_HOST}
WEBSERVER_HOST: ${WEBSERVER_HOST}
WEBSERVER_DB: ${WEBSERVER_DB}
POSTGRES_HOST_PORT: ${POSTGRES_HOST_PORT}
POSTGRES_CONTAINER_PORT: ${POSTGRES_CONTAINER_PORT}
POSTGRES_HOST: ${POSTGRES_HOST}
POSTGRES_PORT: ${POSTGRES_PORT}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
RABBITMQ_HOST: ${RABBITMQ_HOST}
ANALYZER_EXCHANGE: ${ANALYZER_EXCHANGE}
ANALYZER_QUEUE: ${ANALYZER_QUEUE}
ANALYZER_ROUTING_KEY: ${ANALYZER_ROUTING_KEY}
Expand All @@ -32,17 +36,21 @@ services:
context: .
target: spark-app
args:
APP: data-analyzer
APP: ${ANALYZER_HOST}
image: tyknkd/newsanalyzer-dataanalyzer:${PROJECT_VERSION}
container_name: data-analyzer
container_name: ${ANALYZER_HOST}
environment:
PORT: ${ANALYZER_PORT}
COLLECTOR_HOST: ${COLLECTOR_HOST}
ANALYZER_HOST: ${ANALYZER_HOST}
WEBSERVER_HOST: ${WEBSERVER_HOST}
WEBSERVER_PORT: ${WEBSERVER_PORT}
ANALYZER_DB: ${ANALYZER_DB}
POSTGRES_HOST_PORT: ${POSTGRES_HOST_PORT}
POSTGRES_CONTAINER_PORT: ${POSTGRES_CONTAINER_PORT}
POSTGRES_HOST: ${POSTGRES_HOST}
POSTGRES_PORT: ${POSTGRES_PORT}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
RABBITMQ_HOST: ${RABBITMQ_HOST}
ANALYZER_EXCHANGE: ${ANALYZER_EXCHANGE}
ANALYZER_QUEUE: ${ANALYZER_QUEUE}
ANALYZER_ROUTING_KEY: ${ANALYZER_ROUTING_KEY}
Expand All @@ -62,17 +70,21 @@ services:
context: .
target: app
args:
APP: data-collector
APP: ${COLLECTOR_HOST}
image: tyknkd/newsanalyzer-datacollector:${PROJECT_VERSION}
container_name: data-collector
container_name: ${COLLECTOR_HOST}
environment:
PORT: ${COLLECTOR_PORT}
COLLECTOR_HOST: ${COLLECTOR_HOST}
ANALYZER_HOST: ${ANALYZER_HOST}
WEBSERVER_HOST: ${WEBSERVER_HOST}
ANALYZER_PORT: ${ANALYZER_PORT}
COLLECTOR_DB: ${COLLECTOR_DB}
POSTGRES_HOST_PORT: ${POSTGRES_HOST_PORT}
POSTGRES_CONTAINER_PORT: ${POSTGRES_CONTAINER_PORT}
POSTGRES_HOST: ${POSTGRES_HOST}
POSTGRES_PORT: ${POSTGRES_PORT}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
RABBITMQ_HOST: ${RABBITMQ_HOST}
NEWS_API_KEY_FILE: /run/secrets/news_api_key
COLLECTOR_EXCHANGE: ${COLLECTOR_EXCHANGE}
COLLECTOR_QUEUE: ${COLLECTOR_QUEUE}
Expand All @@ -88,7 +100,7 @@ services:

db:
image: postgres:16.2-alpine
container_name: db
container_name: ${POSTGRES_HOST}
environment:
POSTGRES_HOST: ""
POSTGRES_DB: ${POSTGRES_DB}
Expand All @@ -98,7 +110,7 @@ services:
secrets:
- postgres_password
ports:
- ${POSTGRES_HOST_PORT}:${POSTGRES_CONTAINER_PORT}
- ${POSTGRES_PORT}:${POSTGRES_PORT}
volumes:
- ./docker-databases/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./.docker-data/.pg-data:/var/lib/postgresql/data
Expand All @@ -110,7 +122,7 @@ services:

mq:
image: rabbitmq:3.13-management
container_name: mq
container_name: ${RABBITMQ_HOST}
ports:
- ${RABBITMQ_PORT}:${RABBITMQ_PORT}
- ${RABBITMQ_METRICS_PORT}:${RABBITMQ_METRICS_PORT}
Expand Down Expand Up @@ -162,17 +174,21 @@ services:
context: .
target: gradle-test
environment:
COLLECTOR_HOST: ${COLLECTOR_HOST}
ANALYZER_HOST: ${ANALYZER_HOST}
WEBSERVER_HOST: ${WEBSERVER_HOST}
COLLECTOR_TEST_DB: ${COLLECTOR_TEST_DB}
ANALYZER_TEST_DB: ${ANALYZER_TEST_DB}
WEBSERVER_TEST_DB: ${WEBSERVER_TEST_DB}
COLLECTOR_PORT: ${COLLECTOR_PORT}
ANALYZER_PORT: ${ANALYZER_PORT}
WEBSERVER_PORT: ${WEBSERVER_PORT}
POSTGRES_HOST_PORT: ${POSTGRES_HOST_PORT}
POSTGRES_CONTAINER_PORT: ${POSTGRES_CONTAINER_PORT}
POSTGRES_HOST: ${POSTGRES_HOST}
POSTGRES_PORT: ${POSTGRES_PORT}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
NEWS_API_KEY_FILE: /run/secrets/news_api_key
RABBITMQ_HOST: ${RABBITMQ_HOST}
ANALYZER_EXCHANGE: ${ANALYZER_EXCHANGE}
ANALYZER_QUEUE: ${ANALYZER_QUEUE}
ANALYZER_ROUTING_KEY: ${ANALYZER_ROUTING_KEY}
Expand Down

0 comments on commit a960b4f

Please sign in to comment.