|
| 1 | +"""Setup API handlers.""" |
| 2 | + |
| 3 | +from fastapi import APIRouter, Depends, HTTPException |
| 4 | +from sqlalchemy.orm import Session |
| 5 | + |
| 6 | +from app.shared.database.database import get_db |
| 7 | +from app.setup.core.setup_service import SetupService |
| 8 | +from app.setup.api.setup_dto import ( |
| 9 | + SetupStatus, |
| 10 | + SetupInitialize, |
| 11 | + SetupInitializeResponse, |
| 12 | +) |
| 13 | + |
| 14 | +router = APIRouter(prefix="/setup", tags=["Setup"]) |
| 15 | + |
| 16 | + |
| 17 | +def get_setup_service(db: Session = Depends(get_db)) -> SetupService: |
| 18 | + """Dependency to get SetupService instance.""" |
| 19 | + return SetupService(db) |
| 20 | + |
| 21 | + |
| 22 | +@router.get("/status", response_model=SetupStatus) |
| 23 | +def get_setup_status(service: SetupService = Depends(get_setup_service)): |
| 24 | + """ |
| 25 | + Check if the system has been initialized. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + SetupStatus with initialized flag and message |
| 29 | + """ |
| 30 | + initialized = service.is_initialized() |
| 31 | + skip_setup = service.should_skip_setup() |
| 32 | + |
| 33 | + if skip_setup and not initialized: |
| 34 | + return SetupStatus( |
| 35 | + initialized=False, |
| 36 | + message="Setup skipped via SKIP_SETUP environment variable", |
| 37 | + ) |
| 38 | + |
| 39 | + if initialized: |
| 40 | + return SetupStatus( |
| 41 | + initialized=True, |
| 42 | + message="System is ready", |
| 43 | + ) |
| 44 | + |
| 45 | + return SetupStatus( |
| 46 | + initialized=False, |
| 47 | + message="System requires initial setup", |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +@router.post("/initialize", response_model=SetupInitializeResponse) |
| 52 | +def initialize_setup( |
| 53 | + data: SetupInitialize, |
| 54 | + service: SetupService = Depends(get_setup_service), |
| 55 | +): |
| 56 | + """ |
| 57 | + Initialize the system with the first admin user. |
| 58 | +
|
| 59 | + This endpoint can only be called once, when no admin users exist. |
| 60 | +
|
| 61 | + Args: |
| 62 | + data: SetupInitialize with admin credentials |
| 63 | +
|
| 64 | + Returns: |
| 65 | + SetupInitializeResponse on success |
| 66 | +
|
| 67 | + Raises: |
| 68 | + HTTPException 400: If system is already initialized |
| 69 | + """ |
| 70 | + if service.is_initialized(): |
| 71 | + raise HTTPException( |
| 72 | + status_code=400, |
| 73 | + detail="System is already initialized. Cannot run setup again.", |
| 74 | + ) |
| 75 | + |
| 76 | + try: |
| 77 | + admin_user = service.initialize( |
| 78 | + admin_email=data.admin_email, |
| 79 | + admin_password=data.admin_password, |
| 80 | + admin_name=data.admin_name, |
| 81 | + ) |
| 82 | + |
| 83 | + return SetupInitializeResponse( |
| 84 | + success=True, |
| 85 | + message="System initialized successfully! You can now login with your admin credentials.", |
| 86 | + admin_email=admin_user.email, |
| 87 | + ) |
| 88 | + except ValueError as e: |
| 89 | + raise HTTPException(status_code=400, detail=str(e)) |
| 90 | + except Exception as e: |
| 91 | + raise HTTPException( |
| 92 | + status_code=500, detail=f"Failed to initialize system: {str(e)}" |
| 93 | + ) |
0 commit comments