70 lines
1.7 KiB
Python
70 lines
1.7 KiB
Python
"""Main FastAPI application."""
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.v1 import assets, auth, shares, uploads
|
|
from app.infra.config import get_settings
|
|
from app.infra.database import init_db
|
|
|
|
settings = get_settings()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Application lifespan handler."""
|
|
# Startup
|
|
await init_db()
|
|
|
|
# TEMPORARY: Clean up old deleted assets (remove this after first run)
|
|
from app.infra.database import get_session
|
|
from sqlalchemy import text
|
|
async for session in get_session():
|
|
await session.execute(text("DELETE FROM assets WHERE status = 'deleted'"))
|
|
await session.commit()
|
|
break
|
|
# END TEMPORARY
|
|
|
|
yield
|
|
# Shutdown
|
|
pass
|
|
|
|
|
|
app = FastAPI(
|
|
title="ITCloud API",
|
|
description="Cloud photo and video storage API",
|
|
version="0.1.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins_list,
|
|
allow_credentials=True,
|
|
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
|
|
allow_headers=["*"],
|
|
expose_headers=["*"],
|
|
max_age=3600,
|
|
)
|
|
|
|
# Include routers
|
|
app.include_router(auth.router, prefix="/api/v1")
|
|
app.include_router(uploads.router, prefix="/api/v1")
|
|
app.include_router(assets.router, prefix="/api/v1")
|
|
app.include_router(shares.router, prefix="/api/v1")
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""Health check endpoint."""
|
|
return {"status": "ok"}
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Root endpoint."""
|
|
return {"message": "ITCloud API", "version": "0.1.0"}
|