Download database_conn.py from WildOjisan/embeddinggemma_300m_fastapi: direct link, hf CLI and curl.
- Browser
- Download file 1.74 kB
-
https://huggingface.co/spaces/WildOjisan/embeddinggemma_300m_fastapi/resolve/main/database_conn.py
- Command line
-
hf download hf://spaces/WildOjisan/embeddinggemma_300m_fastapi/database_conn.py
-
curl -L -o database_conn.py https://huggingface.co/spaces/WildOjisan/embeddinggemma_300m_fastapi/resolve/main/database_conn.py
1.74 kB
| # database_conn.py | |
| import asyncpg | |
| from typing import AsyncGenerator | |
| # [μ£Όμ] μ΄ μ 보λ μ€μ μλΉμ€μμλ νκ²½ λ³μλ‘ κ΄λ¦¬ν΄μΌ ν©λλ€. | |
| DB_USER = "neondb_owner" | |
| DB_PASSWORD = "npg_rFA9DgloybO0" # μ€μ λΉλ°λ²νΈ μ¬μ© | |
| DB_HOST = "ep-rapid-truth-ad2e6rnq-pooler.c-2.us-east-1.aws.neon.tech" | |
| DB_NAME = "neondb" | |
| DB_PORT = 5432 | |
| DB_SSL = 'require' | |
| # κΈλ‘λ² DB μ°κ²° ν λ³μ | |
| pool: asyncpg.Pool = None | |
| # μλ² μμ μ νΈμΆ (FastAPI @app.on_event("startup")μμ μ¬μ©) | |
| async def connect_to_db(): | |
| global pool | |
| print("DB μ°κ²° ν μ΄κΈ°ν μ€...") | |
| try: | |
| pool = await asyncpg.create_pool( | |
| user=DB_USER, | |
| password=DB_PASSWORD, | |
| host=DB_HOST, | |
| database=DB_NAME, | |
| port=DB_PORT, | |
| ssl=DB_SSL | |
| ) | |
| print("DB μ°κ²° μ±κ³΅.") | |
| except Exception as e: | |
| print(f"DB μ°κ²° μ€ν¨: {e}") | |
| raise e | |
| # μλ² μ’ λ£ μ νΈμΆ (FastAPI @app.on_event("shutdown")μμ μ¬μ©) | |
| async def close_db_connection(): | |
| global pool | |
| if pool: | |
| print("DB μ°κ²° ν μ’ λ£ μ€...") | |
| await pool.close() | |
| print("DB μ°κ²° ν μ’ λ£ μλ£.") | |
| # API μλν¬μΈνΈμ DB μ°κ²° κ°μ²΄λ₯Ό μ£Όμ νλ ν¨μ (FastAPI Dependsμμ μ¬μ©) | |
| async def get_db_connection() -> AsyncGenerator[asyncpg.Connection, None]: | |
| """DB μ°κ²° νμμ 컀λ₯μ μ κ°μ Έμ μλν¬μΈνΈμ μ 곡ν©λλ€.""" | |
| global pool | |
| if not pool: | |
| raise ConnectionError("DB μ°κ²° νμ΄ μ΄κΈ°νλμ§ μμμ΅λλ€.") | |
| # async with ꡬ문μΌλ‘ μ°κ²°μ κ°μ Έμ yield ν, μλμΌλ‘ νμ λ°νν©λλ€. | |
| async with pool.acquire() as connection: | |
| yield connection |