init
This commit is contained in:
0
backend/app/core/__init__.py
Normal file
0
backend/app/core/__init__.py
Normal file
43
backend/app/core/config.py
Normal file
43
backend/app/core/config.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""Application configuration"""
|
||||
from pydantic_settings import BaseSettings
|
||||
from typing import Optional
|
||||
import os
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Application settings"""
|
||||
|
||||
# Server
|
||||
port: int = int(os.getenv("PORT", "8080"))
|
||||
host: str = "127.0.0.1"
|
||||
|
||||
# Database
|
||||
postgres_socket_path: str = os.getenv("POSTGRES_SOCKET_PATH", "/run/postgresql")
|
||||
postgres_db: str = os.getenv("POSTGRES_DB", "jawz")
|
||||
postgres_user: str = os.getenv("POSTGRES_USER", os.getenv("USER", "jawz"))
|
||||
|
||||
# *arr API keys
|
||||
sonarr_api_key: str = os.getenv("SONARR_API_KEY", "")
|
||||
radarr_api_key: str = os.getenv("RADARR_API_KEY", "")
|
||||
lidarr_api_key: str = os.getenv("LIDARR_API_KEY", "")
|
||||
|
||||
# *arr base URLs
|
||||
sonarr_url: str = "http://127.0.0.1:8989"
|
||||
radarr_url: str = "http://127.0.0.1:7878"
|
||||
lidarr_url: str = "http://127.0.0.1:8686"
|
||||
|
||||
# Admin
|
||||
admin_token: Optional[str] = os.getenv("MOVIEMAP_ADMIN_TOKEN")
|
||||
|
||||
@property
|
||||
def database_url(self) -> str:
|
||||
"""Build PostgreSQL connection string using Unix socket"""
|
||||
return f"postgresql://{self.postgres_user}@/{self.postgres_db}?host={self.postgres_socket_path}"
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
case_sensitive = False
|
||||
|
||||
|
||||
settings = Settings()
|
||||
|
||||
50
backend/app/core/database.py
Normal file
50
backend/app/core/database.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""Database connection and session management"""
|
||||
from psycopg import AsyncConnection
|
||||
from psycopg_pool import AsyncConnectionPool
|
||||
from app.core.config import settings
|
||||
from typing import Optional
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Connection pool
|
||||
pool: Optional[AsyncConnectionPool] = None
|
||||
|
||||
|
||||
async def init_db():
|
||||
"""Initialize database connection pool"""
|
||||
global pool
|
||||
try:
|
||||
pool = AsyncConnectionPool(
|
||||
conninfo=settings.database_url,
|
||||
min_size=1,
|
||||
max_size=10,
|
||||
open=False,
|
||||
)
|
||||
await pool.open()
|
||||
logger.info("Database connection pool initialized")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize database pool: {e}")
|
||||
raise
|
||||
|
||||
|
||||
async def close_db():
|
||||
"""Close database connection pool"""
|
||||
global pool
|
||||
if pool:
|
||||
await pool.close()
|
||||
logger.info("Database connection pool closed")
|
||||
|
||||
|
||||
async def get_db() -> AsyncConnection:
|
||||
"""Get database connection from pool"""
|
||||
if not pool:
|
||||
await init_db()
|
||||
return await pool.getconn()
|
||||
|
||||
|
||||
async def return_conn(conn: AsyncConnection):
|
||||
"""Return connection to pool"""
|
||||
if pool:
|
||||
await pool.putconn(conn)
|
||||
|
||||
Reference in New Issue
Block a user