Spaces:
Runtime error
Runtime error
Soutrik
commited on
Commit
·
c6e88ba
1
Parent(s):
a3f0b6e
docker-compose added with local testing
Browse files- .env +1 -1
- app/core/__pycache__/__init__.cpython-310.pyc +0 -0
- app/core/__pycache__/config.cpython-310.pyc +0 -0
- app/core/config.py +41 -0
- docker-compose.yaml +5 -0
- src/__init__.py +0 -0
- src/__pycache__/__init__.cpython-310.pyc +0 -0
- src/__pycache__/test_infra.cpython-310.pyc +0 -0
- src/test_infra.py +36 -0
.env
CHANGED
@@ -5,6 +5,6 @@ REDIS_PORT=6379
|
|
5 |
REDIS_HOST=redis
|
6 |
FLOWER_BASIC_AUTH=flower_user:flower_pass
|
7 |
REDIS_URL=redis://redis:6379/0
|
8 |
-
DATABASE_URL=postgresql
|
9 |
BROKER_URL=redis://redis:6379/0
|
10 |
|
|
|
5 |
REDIS_HOST=redis
|
6 |
FLOWER_BASIC_AUTH=flower_user:flower_pass
|
7 |
REDIS_URL=redis://redis:6379/0
|
8 |
+
DATABASE_URL=postgresql//test_user:test_pass@postgres:5432/test_db
|
9 |
BROKER_URL=redis://redis:6379/0
|
10 |
|
app/core/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (226 Bytes). View file
|
|
app/core/__pycache__/config.cpython-310.pyc
ADDED
Binary file (1.81 kB). View file
|
|
app/core/config.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from pydantic.v1 import BaseSettings, Field
|
3 |
+
from loguru import logger
|
4 |
+
|
5 |
+
|
6 |
+
class Settings(BaseSettings):
|
7 |
+
POSTGRES_DB: str = Field("test_db", env="POSTGRES_DB")
|
8 |
+
POSTGRES_USER: str = Field("test_user", env="POSTGRES_USER")
|
9 |
+
POSTGRES_PASSWORD: str = Field("test_pass", env="POSTGRES_PASSWORD")
|
10 |
+
|
11 |
+
is_docker: bool = Field(default_factory=lambda: os.environ.get("DOCKER_ENV") == "1")
|
12 |
+
|
13 |
+
database_url: str = Field(..., env="DATABASE_URL")
|
14 |
+
redis_url: str = Field(..., env="REDIS_URL")
|
15 |
+
flower_basic_auth: str = Field(..., env="FLOWER_BASIC_AUTH")
|
16 |
+
broker_url: str = Field(..., env="BROKER_URL")
|
17 |
+
|
18 |
+
class Config:
|
19 |
+
env_file = ".env"
|
20 |
+
|
21 |
+
@classmethod
|
22 |
+
def create(cls):
|
23 |
+
"""Create instance and dynamically set database and redis URLs."""
|
24 |
+
instance = cls()
|
25 |
+
# Set the correct database URL without the +asyncpg
|
26 |
+
instance.database_url = (
|
27 |
+
f"postgres://{instance.POSTGRES_USER}:{instance.POSTGRES_PASSWORD}@"
|
28 |
+
f"{'localhost' if not instance.is_docker else 'postgres'}:5432/{instance.POSTGRES_DB}"
|
29 |
+
)
|
30 |
+
instance.redis_url = (
|
31 |
+
f"redis://{'localhost' if not instance.is_docker else 'redis'}:6379/0"
|
32 |
+
)
|
33 |
+
instance.broker_url = instance.redis_url
|
34 |
+
return instance
|
35 |
+
|
36 |
+
|
37 |
+
# Instantiate Settings
|
38 |
+
settings = Settings.create()
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
logger.info(f"Settings: {settings.dict()}")
|
docker-compose.yaml
CHANGED
@@ -7,6 +7,7 @@ services:
|
|
7 |
POSTGRES_DB: ${POSTGRES_DB}
|
8 |
POSTGRES_USER: ${POSTGRES_USER}
|
9 |
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
|
|
10 |
ports:
|
11 |
- "5432:5432"
|
12 |
volumes:
|
@@ -22,6 +23,8 @@ services:
|
|
22 |
ports:
|
23 |
- "6379:6379"
|
24 |
- "8001:8001"
|
|
|
|
|
25 |
healthcheck:
|
26 |
test: ["CMD", "redis-cli", "ping"]
|
27 |
interval: 30s
|
@@ -30,6 +33,8 @@ services:
|
|
30 |
|
31 |
flower:
|
32 |
image: mher/flower:0.9.7
|
|
|
|
|
33 |
command: ["flower", "--broker=${BROKER_URL}", "--port=5555"]
|
34 |
ports:
|
35 |
- 5557:5555
|
|
|
7 |
POSTGRES_DB: ${POSTGRES_DB}
|
8 |
POSTGRES_USER: ${POSTGRES_USER}
|
9 |
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
10 |
+
DOCKER_ENV: 1
|
11 |
ports:
|
12 |
- "5432:5432"
|
13 |
volumes:
|
|
|
23 |
ports:
|
24 |
- "6379:6379"
|
25 |
- "8001:8001"
|
26 |
+
environment:
|
27 |
+
DOCKER_ENV: 1
|
28 |
healthcheck:
|
29 |
test: ["CMD", "redis-cli", "ping"]
|
30 |
interval: 30s
|
|
|
33 |
|
34 |
flower:
|
35 |
image: mher/flower:0.9.7
|
36 |
+
environment:
|
37 |
+
DOCKER_ENV: 1
|
38 |
command: ["flower", "--broker=${BROKER_URL}", "--port=5555"]
|
39 |
ports:
|
40 |
- 5557:5555
|
src/__init__.py
ADDED
File without changes
|
src/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (221 Bytes). View file
|
|
src/__pycache__/test_infra.cpython-310.pyc
ADDED
Binary file (1.41 kB). View file
|
|
src/test_infra.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncpg
|
2 |
+
import aioredis
|
3 |
+
from loguru import logger
|
4 |
+
from app.core.config import settings
|
5 |
+
|
6 |
+
|
7 |
+
async def test_postgres_connection(database_url: str):
|
8 |
+
try:
|
9 |
+
conn = await asyncpg.connect(database_url)
|
10 |
+
logger.info("Successfully connected to PostgreSQL!")
|
11 |
+
await conn.close()
|
12 |
+
except Exception as e:
|
13 |
+
logger.error(f"Failed to connect to PostgreSQL: {e}")
|
14 |
+
|
15 |
+
|
16 |
+
async def test_redis_connection(redis_url: str):
|
17 |
+
try:
|
18 |
+
redis = await aioredis.from_url(redis_url)
|
19 |
+
await redis.ping() # Send a ping to check connection
|
20 |
+
logger.info("Successfully connected to Redis!")
|
21 |
+
await redis.close()
|
22 |
+
except Exception as e:
|
23 |
+
logger.error(f"Failed to connect to Redis: {e}")
|
24 |
+
|
25 |
+
|
26 |
+
async def main():
|
27 |
+
logger.info(f"Settings: {settings.dict()}")
|
28 |
+
|
29 |
+
await test_postgres_connection(settings.database_url)
|
30 |
+
await test_redis_connection(settings.redis_url)
|
31 |
+
|
32 |
+
|
33 |
+
if __name__ == "__main__":
|
34 |
+
import asyncio
|
35 |
+
|
36 |
+
asyncio.run(main())
|