Add full sortof codebase: API, drain workers, frontend, schema, specs
This commit is contained in:
34
api/db.py
Normal file
34
api/db.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""asyncpg pool factory. DSN is built from /opt/sortof/.env at startup."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
|
||||
import asyncpg
|
||||
from dotenv import load_dotenv
|
||||
|
||||
ENV_PATH = Path(__file__).resolve().parent.parent / ".env"
|
||||
|
||||
|
||||
def _build_dsn() -> str:
|
||||
load_dotenv(ENV_PATH)
|
||||
explicit = os.environ.get("DATABASE_URL")
|
||||
if explicit:
|
||||
return explicit
|
||||
user = os.environ["POSTGRES_USER"]
|
||||
pw = urllib.parse.quote(os.environ["POSTGRES_PASSWORD"], safe="")
|
||||
name = os.environ["POSTGRES_DB"]
|
||||
host = os.environ.get("POSTGRES_HOST", "127.0.0.1")
|
||||
port = os.environ.get("POSTGRES_PORT", "5439")
|
||||
return f"postgresql://{user}:{pw}@{host}:{port}/{name}"
|
||||
|
||||
|
||||
async def create_pool() -> asyncpg.Pool:
|
||||
return await asyncpg.create_pool(
|
||||
dsn=_build_dsn(),
|
||||
min_size=1,
|
||||
max_size=8,
|
||||
command_timeout=15,
|
||||
)
|
||||
Reference in New Issue
Block a user