Files
works/oauth/backend/app/core/database.py
Claude f53d55e712 Initial commit: OAuth 2.0 인증 시스템 with APISIX API Gateway
- FastAPI 백엔드 + MongoDB + Redis 구성
- React + Vite + TypeScript + shadcn/ui 프론트엔드
- Apache APISIX API Gateway 통합
- Docker Compose 기반 개발 환경
- 3단계 권한 체계 (System Admin, Group Admin, User)
- 동적 테마 지원
- 환경별 설정 (dev/vei/prod)

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-31 10:16:41 +09:00

38 lines
1.2 KiB
Python

from motor.motor_asyncio import AsyncIOMotorClient
from app.core.config import settings
import redis.asyncio as redis
from typing import Optional
class Database:
client: Optional[AsyncIOMotorClient] = None
database = None
redis_client: Optional[redis.Redis] = None
db = Database()
async def init_db():
db.client = AsyncIOMotorClient(settings.MONGODB_URL)
db.database = db.client[settings.DATABASE_NAME]
db.redis_client = await redis.from_url(settings.REDIS_URL, decode_responses=True)
await create_indexes()
async def close_db():
if db.client:
db.client.close()
if db.redis_client:
await db.redis_client.close()
async def create_indexes():
await db.database.users.create_index("email", unique=True)
await db.database.users.create_index("username", unique=True)
await db.database.applications.create_index("client_id", unique=True)
await db.database.applications.create_index("app_name", unique=True)
await db.database.auth_history.create_index([("user_id", 1), ("created_at", -1)])
await db.database.auth_history.create_index("created_at")
def get_database():
return db.database
def get_redis():
return db.redis_client