diff --git a/.env b/.env index 63860b6..acc0e98 100644 --- a/.env +++ b/.env @@ -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 @@ -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 @@ -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 diff --git a/app-entrypoint.sh b/app-entrypoint.sh index bcfcc13..b2ade33 100644 --- a/app-entrypoint.sh +++ b/app-entrypoint.sh @@ -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 \ diff --git a/components/data-support/src/main/kotlin/io/newsanalyzer/datasupport/DatabaseTemplate.kt b/components/data-support/src/main/kotlin/io/newsanalyzer/datasupport/DatabaseTemplate.kt index 12aaf4b..1a14f61 100644 --- a/components/data-support/src/main/kotlin/io/newsanalyzer/datasupport/DatabaseTemplate.kt +++ b/components/data-support/src/main/kotlin/io/newsanalyzer/datasupport/DatabaseTemplate.kt @@ -8,18 +8,15 @@ open class DatabaseTemplate(dbName: String, tables: List) { 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" diff --git a/components/http-support/src/main/kotlin/io/newsanalyzer/httpsupport/HostPaths.kt b/components/http-support/src/main/kotlin/io/newsanalyzer/httpsupport/HostPaths.kt index 7f5703f..750e47f 100644 --- a/components/http-support/src/main/kotlin/io/newsanalyzer/httpsupport/HostPaths.kt +++ b/components/http-support/src/main/kotlin/io/newsanalyzer/httpsupport/HostPaths.kt @@ -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" } } \ No newline at end of file diff --git a/components/mq-support/src/main/kotlin/io/newsanalyzer/mqsupport/Exchange.kt b/components/mq-support/src/main/kotlin/io/newsanalyzer/mqsupport/Exchange.kt index 608aa26..dfe41b2 100644 --- a/components/mq-support/src/main/kotlin/io/newsanalyzer/mqsupport/Exchange.kt +++ b/components/mq-support/src/main/kotlin/io/newsanalyzer/mqsupport/Exchange.kt @@ -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" } } diff --git a/compose.yaml b/compose.yaml index 525e365..cb14bda 100644 --- a/compose.yaml +++ b/compose.yaml @@ -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} @@ -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} @@ -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} @@ -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} @@ -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 @@ -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} @@ -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}