-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added rabbitmq and .env variable reading!
- Loading branch information
1 parent
f4c9980
commit 1c9790f
Showing
8 changed files
with
134 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
from celery import Celery | ||
|
||
# TODO: read from .env | ||
app = Celery("celery_app/tasks", broker="pyamqp://root:pass@localhost//") | ||
from utils.credentials import load_rabbitmq_credentials | ||
|
||
rabbit_creds = load_rabbitmq_credentials() | ||
user = rabbit_creds['user'] | ||
password = rabbit_creds['password'] | ||
host = rabbit_creds['host'] | ||
port = rabbit_creds['port'] | ||
|
||
app = Celery("celery_app/tasks", broker=f"pyamqp://{user}:{password}@{host}:{port}//") | ||
app.autodiscover_tasks(["celery_app"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import unittest | ||
|
||
from utils.credentials import load_postgres_credentials, load_rabbitmq_credentials | ||
|
||
|
||
class TestCredentialLoadings(unittest.TestCase): | ||
def test_postgresql_envs_check_type(self): | ||
postgres_creds = load_postgres_credentials() | ||
|
||
self.assertIsInstance(postgres_creds, dict) | ||
|
||
def test_postgresql_envs_values(self): | ||
postgres_creds = load_postgres_credentials() | ||
|
||
self.assertNotEqual(postgres_creds["user"], None) | ||
self.assertNotEqual(postgres_creds["password"], None) | ||
self.assertNotEqual(postgres_creds["host"], None) | ||
self.assertNotEqual(postgres_creds["port"], None) | ||
|
||
self.assertIsInstance(postgres_creds["user"], str) | ||
self.assertIsInstance(postgres_creds["password"], str) | ||
self.assertIsInstance(postgres_creds["host"], str) | ||
self.assertIsInstance(postgres_creds["port"], str) | ||
|
||
def test_rabbitmq_envs_check_type(self): | ||
rabbitmq_creds = load_rabbitmq_credentials() | ||
|
||
self.assertIsInstance(rabbitmq_creds, dict) | ||
|
||
def test_rabbitmq_envs_values(self): | ||
rabbitmq_creds = load_postgres_credentials() | ||
|
||
self.assertNotEqual(rabbitmq_creds["user"], None) | ||
self.assertNotEqual(rabbitmq_creds["password"], None) | ||
self.assertNotEqual(rabbitmq_creds["host"], None) | ||
self.assertNotEqual(rabbitmq_creds["port"], None) | ||
|
||
self.assertIsInstance(rabbitmq_creds["user"], str) | ||
self.assertIsInstance(rabbitmq_creds["password"], str) | ||
self.assertIsInstance(rabbitmq_creds["host"], str) | ||
self.assertIsInstance(rabbitmq_creds["port"], str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
|
||
|
||
def load_postgres_credentials() -> dict[str, str]: | ||
""" | ||
load posgresql db credentials from .env | ||
Returns: | ||
--------- | ||
postgres_creds : dict[str, Any] | ||
postgresql credentials | ||
a dictionary representive of | ||
`user`: str | ||
`password` : str | ||
`host` : str | ||
`port` : int | ||
""" | ||
load_dotenv() | ||
|
||
postgres_creds = {} | ||
|
||
postgres_creds["user"] = os.getenv("POSTGRES_USER", "") | ||
postgres_creds["password"] = os.getenv("POSTGRES_PASS", "") | ||
postgres_creds["host"] = os.getenv("POSTGRES_HOST", "") | ||
postgres_creds["port"] = os.getenv("POSTGRES_PORT", "") | ||
|
||
return postgres_creds | ||
|
||
|
||
def load_rabbitmq_credentials() -> dict[str, str]: | ||
""" | ||
load rabbitmq credentials from .env | ||
Returns: | ||
--------- | ||
rabbitmq_creds : dict[str, Any] | ||
rabbitmq credentials | ||
a dictionary representive of | ||
`user`: str | ||
`password` : str | ||
`host` : str | ||
`port` : int | ||
""" | ||
load_dotenv() | ||
|
||
rabbitmq_creds = {} | ||
|
||
rabbitmq_creds["user"] = os.getenv("RABBIT_USER", "") | ||
rabbitmq_creds["password"] = os.getenv("RABBIT_PASSWORD", "") | ||
rabbitmq_creds["host"] = os.getenv("RABBIT_HOST", "") | ||
rabbitmq_creds["port"] = os.getenv("RABBIT_PORT", "") | ||
|
||
return rabbitmq_creds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters