Files
site11/services/users/backend/database.py
jungwoo choi 683305918c Step 3: MongoDB and Redis integration complete
- Added MongoDB and Redis containers to docker-compose
- Integrated Users service with MongoDB using Beanie ODM
- Replaced in-memory storage with persistent MongoDB
- Added proper data models with email validation
- Verified data persistence with MongoDB ObjectIDs

Services running:
- MongoDB: Port 27017 (with health checks)
- Redis: Port 6379 (with health checks)
- Users service: Connected to MongoDB
- Console: API Gateway routing working

Test: Users now stored in MongoDB with persistence

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-10 16:21:32 +09:00

22 lines
619 B
Python

from motor.motor_asyncio import AsyncIOMotorClient
from beanie import init_beanie
import os
from models import User
async def init_db():
"""Initialize database connection"""
# Get MongoDB URL from environment
mongodb_url = os.getenv("MONGODB_URL", "mongodb://mongodb:27017")
db_name = os.getenv("DB_NAME", "users_db")
# Create Motor client
client = AsyncIOMotorClient(mongodb_url)
# Initialize beanie with the User model
await init_beanie(
database=client[db_name],
document_models=[User]
)
print(f"Connected to MongoDB: {mongodb_url}/{db_name}")