Skip to content

Commit

Permalink
Move to contants and settings.py
Browse files Browse the repository at this point in the history
  • Loading branch information
addu390 committed Sep 26, 2020
1 parent 8dfc6c2 commit 129e349
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ DEBUG=True
DATABASE_NAME = 'django-kafka'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
HOST_ENDPOINT = '127.0.0.1'
HOST_ENDPOINT = '127.0.0.1'
SCHEMA_REGISTRY_URL = 'http://localhost:8081'
SECRET_KEY = 'gc97l0(yyw(^)bwezc)#^3ir5+!@z1%x18&fs9r%e-q^+ziq0e'
18 changes: 4 additions & 14 deletions django_kafka/settings.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import os
from decouple import config

CELERY_TIMEZONE = 'Europe/Warsaw'
# Let's make things happen
CELERY_BEAT_SCHEDULE = {
'poll-every-second': {
'task': 'receive',
'schedule': 1.0,
}
}

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = 'gc97l0(yyw(^)bwezc)#^3ir5+!@z1%x18&fs9r%e-q^+ziq0e'

DEBUG = True
SECRET_KEY = config('SECRET_KEY')

ALLOWED_HOSTS = []
DEBUG = config('DEBUG', default=False, cast=bool)

INSTALLED_APPS = [
'django.contrib.admin',
Expand All @@ -30,7 +19,8 @@
]
ALLOWED_HOSTS = ['*']

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_BROKER_URL = 'amqp://127.0.0.1:5672'
SCHEMA_REGISTRY_URL = config('SCHEMA_REGISTRY_URL')

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
Expand Down
4 changes: 3 additions & 1 deletion publish_subscribe/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
},
"required": [ "username", "data" ]
}
"""
"""

USER_TOPIC = 'leon'
4 changes: 2 additions & 2 deletions publish_subscribe/consumer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from confluent_kafka import DeserializingConsumer
from confluent_kafka.schema_registry.json_schema import JSONDeserializer
from confluent_kafka.serialization import StringDeserializer
from .constants import USER_SCHEMA
from .constants import USER_SCHEMA, USER_TOPIC
from .transformers import dict_to_user
import logging
import traceback
Expand All @@ -17,7 +17,7 @@ def receive():
'auto.offset.reset': "earliest"}

consumer = DeserializingConsumer(consumer_conf)
consumer.subscribe(['leon'])
consumer.subscribe([USER_TOPIC])

"""
The idea is to start the Kafka consumer when the message is sent to the Kafka producer.
Expand Down
8 changes: 5 additions & 3 deletions publish_subscribe/producers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from confluent_kafka.serialization import StringSerializer
from confluent_kafka.schema_registry import SchemaRegistryClient
from confluent_kafka.schema_registry.json_schema import JSONSerializer
from .constants import USER_SCHEMA

from django_kafka.settings import SCHEMA_REGISTRY_URL
from .constants import USER_SCHEMA, USER_TOPIC
from .transformers import user_to_dict
from .models import UserProducer

schema_registry_conf = {'url': 'http://localhost:8081'}
schema_registry_conf = {'url': SCHEMA_REGISTRY_URL}
schema_registry_client = SchemaRegistryClient(schema_registry_conf)


Expand All @@ -31,5 +33,5 @@ def send(username, data, token):
data=data,
token=token)

producer.produce(topic='leon', key=str(uuid4()), value=user,
producer.produce(topic=USER_TOPIC, key=str(uuid4()), value=user,
on_delivery=delivery_report)

0 comments on commit 129e349

Please sign in to comment.