Skip to content

Commit 49887c4

Browse files
authored
Fixed Kafka integration. (#214)
1 parent 5540ffc commit 49887c4

File tree

6 files changed

+23
-30
lines changed

6 files changed

+23
-30
lines changed

fastapi_template/input_model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import abc
2-
import enum
32
from collections import UserDict
43
from typing import Any, Callable, List, Optional
54

fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@
122122
"tests/test_echo.py",
123123
"tests/test_dummy.py",
124124
"tests/test_redis.py",
125-
"tests/test_rabbit.py"
125+
"tests/test_rabbit.py",
126+
"tests/test_kafka.py"
126127
]
127128
},
128129
"Users model": {

fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -222,35 +222,28 @@ services:
222222

223223
{%- if cookiecutter.enable_kafka == "True" %}
224224

225-
zookeeper:
226-
image: "bitnami/zookeeper:3.7.1"
227-
hostname: "{{cookiecutter.project_name}}-zookeeper"
228-
environment:
229-
ALLOW_ANONYMOUS_LOGIN: "yes"
230-
ZOO_LOG_LEVEL: "ERROR"
231-
healthcheck:
232-
test: zkServer.sh status
233-
interval: 1s
234-
timeout: 3s
235-
retries: 30
236-
237225
kafka:
238-
image: "bitnami/kafka:3.2.0"
226+
image: bitnami/kafka:3.7.1-debian-12-r0
239227
hostname: "{{cookiecutter.project_name}}-kafka"
240228
environment:
241-
KAFKA_BROKER_ID: "1"
242-
ALLOW_PLAINTEXT_LISTENER: "yes"
243-
KAFKA_CFG_LISTENERS: "PLAINTEXT://0.0.0.0:9092"
244-
KAFKA_CFG_ADVERTISED_LISTENERS: "PLAINTEXT://{{cookiecutter.project_name}}-kafka:9092"
245-
KAFKA_CFG_ZOOKEEPER_CONNECT: "{{cookiecutter.project_name}}-zookeeper:2181"
229+
KAFKA_CFG_NODE_ID: "0"
230+
KAFKA_CFG_PROCESS_ROLES: "controller,broker"
231+
KAFKA_CFG_LISTENERS: "PLAINTEXT://:9092,CONTROLLER://:9093,EXTERNAL://:9094"
232+
KAFKA_CFG_ADVERTISED_LISTENERS: "PLAINTEXT://kafka:9092,EXTERNAL://localhost:9094"
233+
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: "CONTROLLER:PLAINTEXT,EXTERNAL:PLAINTEXT,PLAINTEXT:PLAINTEXT"
234+
KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: "0@{{cookiecutter.project_name}}-kafka:9093"
235+
KAFKA_CFG_CONTROLLER_LISTENER_NAMES: "CONTROLLER"
236+
KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE: "true"
237+
KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR: "1"
238+
# Uncomment it to connect from localhost.
239+
# ports:
240+
# - 9094:9094
246241
healthcheck:
247242
test: kafka-topics.sh --list --bootstrap-server localhost:9092
248243
interval: 1s
249244
timeout: 3s
250245
retries: 30
251-
depends_on:
252-
zookeeper:
253-
condition: service_healthy
246+
254247
{%- endif %}
255248

256249
{% if cookiecutter.db_info.name != 'none' %}

fastapi_template/template/{{cookiecutter.project_name}}/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,5 +523,5 @@ async def client(
523523
:param fastapi_app: the application.
524524
:yield: client for the app.
525525
"""
526-
async with AsyncClient(app=fastapi_app, base_url="http://test") as ac:
526+
async with AsyncClient(app=fastapi_app, base_url="http://test", timeout=2.0) as ac:
527527
yield ac

fastapi_template/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def generator_start_dir() -> Generator[str, None, None]:
4444

4545

4646
@pytest.fixture()
47-
def default_context(project_name: str) -> None:
47+
def default_context(project_name: str) -> BuilderContext:
4848
"""
4949
Default builder context without features.
5050

fastapi_template/tests/test_generator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ def test_gunicorn(default_context: BuilderContext, worker_id: str):
225225
run_default_check(default_context, worker_id, without_pytest=True)
226226

227227

228-
# @pytest.mark.parametrize("api", ["rest", "graphql"])
229-
# def test_kafka(default_context: BuilderContext, api: str):
230-
# default_context.enable_kafka = True
231-
# default_context.api_type = api
232-
# run_default_check(default_context)
228+
@pytest.mark.parametrize("api", ["rest", "graphql"])
229+
def test_kafka(default_context: BuilderContext, api: str, worker_id: str):
230+
default_context.enable_kafka = True
231+
default_context.api_type = api
232+
run_default_check(default_context, worker_id)

0 commit comments

Comments
 (0)