From 440f08a79edccb414c74bba8ada592843a596751 Mon Sep 17 00:00:00 2001 From: tyknkd <78797823+tyknkd@users.noreply.github.com> Date: Thu, 16 May 2024 18:42:43 +0900 Subject: [PATCH] build: use mq credentials [skip ci] --- .env | 2 ++ .../io/newsanalyzer/mqsupport/Exchange.kt | 23 ++++++++++--------- compose.yaml | 4 ++++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/.env b/.env index 76db0ea..97a7dd0 100644 --- a/.env +++ b/.env @@ -24,6 +24,8 @@ export PROMETHEUS_PORT=9090 export RABBITMQ_HOST=mq export RABBITMQ_METRICS_PORT=15692 export RABBITMQ_PORT=5672 +export RABBITMQ_DEFAULT_USER=rabbituser +export RABBITMQ_DEFAULT_PASS=rabbitpass export WEBSERVER_DB=webserver_db export WEBSERVER_HOST=web-server export WEBSERVER_PORT=8888 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 dfe41b2..d6baf59 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 @@ -5,8 +5,19 @@ import com.rabbitmq.client.BuiltinExchangeType import com.rabbitmq.client.Channel class Exchange(val name: String, val queue: String, val routingKey: String) { + private val user = System.getenv("RABBITMQ_DEFAULT_USER") + private val pass = System.getenv("RABBITMQ_DEFAULT_PASS") + private val mqport = System.getenv("RABBITMQ_PORT")?.toInt()?:5672 + private val mqhost = if (System.getenv("OS_ENV") == "container") { + System.getenv("RABBITMQ_HOST") + } else { + "localhost" + } private val factory = ConnectionFactory().apply { - setUri(getRabbitMqUri()) + username = user + password = pass + port = mqport + host = mqhost } private val connection = factory.newConnection() private val durable = true @@ -19,14 +30,4 @@ class Exchange(val name: String, val queue: String, val routingKey: String) { queueBind(queue, name, routingKey) confirmSelect() } - - private fun getRabbitMqUri(): String { - val port = System.getenv("RABBITMQ_PORT")?:5672 - val host = if (System.getenv("OS_ENV") == "container") { - System.getenv("RABBITMQ_HOST") - } else { - "localhost" - } - return "amqp://$host:$port" - } } diff --git a/compose.yaml b/compose.yaml index cc17fe0..e6105ab 100644 --- a/compose.yaml +++ b/compose.yaml @@ -122,6 +122,8 @@ services: image: rabbitmq:3.13-management container_name: ${RABBITMQ_HOST} environment: + RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS} + RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER} RABBITMQ_LOGS: "-" ports: - ${RABBITMQ_PORT}:${RABBITMQ_PORT} @@ -190,6 +192,8 @@ services: POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password POSTGRES_PORT: ${POSTGRES_PORT} POSTGRES_USER: ${POSTGRES_USER} + RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS} + RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER} RABBITMQ_HOST: ${RABBITMQ_HOST} RABBITMQ_PORT: ${RABBITMQ_PORT} WEBSERVER_HOST: ${WEBSERVER_HOST}