eksplicit mapping af envs
This commit is contained in:
3
backend/app/core/__init__.py
Normal file
3
backend/app/core/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from app.core.config import settings # noqa: F401
|
||||
from app.core.database import get_db # noqa: F401
|
||||
from app.core.redis import get_client # noqa: F401
|
||||
13
backend/app/core/config.py
Normal file
13
backend/app/core/config.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
redis_url: str = "redis://localhost:6379"
|
||||
database_url: str = "postgresql+asyncpg://sighej:sighej@localhost/sighej"
|
||||
session_ttl_seconds: int = 7200
|
||||
allowed_origins: list[str] = ["http://localhost:3000"]
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8")
|
||||
|
||||
|
||||
settings = Settings()
|
||||
21
backend/app/core/database.py
Normal file
21
backend/app/core/database.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
engine = create_async_engine(settings.database_url, echo=False)
|
||||
AsyncSessionLocal = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
async def create_tables() -> None:
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
|
||||
async def get_db() -> AsyncSession:
|
||||
async with AsyncSessionLocal() as session:
|
||||
yield session
|
||||
16
backend/app/core/redis.py
Normal file
16
backend/app/core/redis.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import redis.asyncio as redis
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
_pool: redis.ConnectionPool | None = None
|
||||
|
||||
|
||||
def get_pool() -> redis.ConnectionPool:
|
||||
global _pool
|
||||
if _pool is None:
|
||||
_pool = redis.ConnectionPool.from_url(settings.redis_url, decode_responses=True)
|
||||
return _pool
|
||||
|
||||
|
||||
def get_client() -> redis.Redis:
|
||||
return redis.Redis(connection_pool=get_pool())
|
||||
Reference in New Issue
Block a user