Skip to content

Commit 966d751

Browse files
committed
feat(config): Add Kafka configuration settings and environment-based service host resolution
1 parent 89e5162 commit 966d751

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

app/adapters/kafka/lifespan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def init_kafka(app: FastAPI) -> None: # pragma: no cover
2020
:param app: current application.
2121
"""
2222
app.state.kafka_producer = AIOKafkaProducer(
23-
bootstrap_servers=settings.KAFKA_ADDR,
23+
bootstrap_servers=settings.kafka_url, client_id=settings.KAFKA_CLIENT_ID, acks="all"
2424
)
2525
await app.state.kafka_producer.start()
2626

app/core/config.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ class Settings(BaseSettings):
7171
ALLOWED_HOSTS: list[str] = ["localhost", "127.0.0.1", "api.localhost"]
7272
CORS_ORIGINS: list[str] = ["localhost", "127.0.0.1"]
7373

74-
APP_LOG_FILE_PATH: str = "logs/app.log"
7574
API_BASE_PATH: str = "/api"
7675
APP_VERSION: str = "latest"
7776
APP_HOST: str = "0.0.0.0"
@@ -177,7 +176,9 @@ def bearer_token_url(self) -> str:
177176
# E.G. http://localhost:4317
178177
OPENTELEMETRY_ENDPOINT: str | None = None
179178

180-
KAFKA_ADDR: list[str] = ["app-kafka:9092"]
179+
# Kafka
180+
KAFKA_CLIENT_ID: str = "api"
181+
KAFKA_PORT: int = 9092
181182

182183
# Additional Project Settings
183184
BASE_API_PATH: str = "/api"
@@ -215,7 +216,7 @@ def redis_url(self) -> URL:
215216
path = f"/{self.REDIS_DATABASE}"
216217
return URL.build(
217218
scheme="redis",
218-
host=self.REDIS_HOST,
219+
host="localhost" if self.ENVIRONMENT in ["debug", "local"] else self.REDIS_HOST,
219220
port=self.REDIS_PORT,
220221
password=self.REDIS_PASS,
221222
path=path,
@@ -230,12 +231,18 @@ def rabbit_url(self) -> URL:
230231
"""
231232
return URL.build(
232233
scheme="amqp",
233-
host=self.RABBITMQ_HOST,
234+
host="localhost" if self.ENVIRONMENT in ["debug", "local"] else self.RABBITMQ_HOST,
234235
port=self.RABBITMQ_PORT,
235236
user=self.RABBITMQ_USER,
236237
password=self.RABBITMQ_PASS,
237238
path=self.RABBITMQ_VHOST,
238239
)
239240

241+
@property
242+
def kafka_url(self) -> str:
243+
"""Assemble Kafka broker URL from settings."""
244+
host = "localhost" if self.ENVIRONMENT in ["debug", "local"] else self.KAFKA_CONTAINER_HOST
245+
return f"{host}:{self.KAFKA_PORT}"
246+
240247

241248
settings = Settings()

app/core/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def configure_logging() -> None: # pragma: no cover
100100
logging.basicConfig(handlers=[intercept_handler], level=logging.NOTSET)
101101

102102
log_level = (
103-
LogLevel.DEBUG if settings.ENVIRONMENT in ["local", "pytest"] else settings.LOG_LEVEL
103+
LogLevel.DEBUG if settings.ENVIRONMENT in ["debug", "pytest"] else settings.LOG_LEVEL
104104
)
105105

106106
for logger_name in logging.root.manager.loggerDict:

0 commit comments

Comments
 (0)