Spaces:
Sleeping
Sleeping
Download src/main.py from hashir-qt/tasklert-api: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/spaces/hashir-qt/tasklert-api/resolve/main/src/main.py
- Command line
-
hf download hf://spaces/hashir-qt/tasklert-api/src/main.py
-
curl -L -o main.py https://huggingface.co/spaces/hashir-qt/tasklert-api/resolve/main/src/main.py
2.96 kB
| """Main FastAPI application for the Phase II Full-Stack Todo App.""" | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from .api.todos import router as todos_router | |
| from .api.chat import router as chat_router | |
| from .api.dapr_handlers import router as dapr_router | |
| from .api.stats import router as stats_router | |
| from .api.websocket import router as ws_router | |
| from .core.config import settings | |
| from .core.database import create_db_and_tables | |
| from .models.conversation import Conversation, Message # noqa: F401 — imported to register with SQLModel metadata | |
| from .models.stats import DailyCompletion, UserStats, Achievement # noqa: F401 | |
| from .models.events import TaskEvent # noqa: F401 | |
| # Create FastAPI app instance | |
| app = FastAPI( | |
| title=settings.app_name, | |
| description="REST API for Todo App (Better Auth on frontend)", | |
| version=settings.version, | |
| debug=settings.debug, | |
| ) | |
| # Add CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=[settings.frontend_url, "http://localhost:3000", "http://localhost:3001"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| expose_headers=["Access-Control-Allow-Origin", "Authorization"] | |
| ) | |
| # Include API routers (auth handled by Better Auth on frontend) | |
| app.include_router(todos_router) | |
| app.include_router(chat_router) | |
| app.include_router(dapr_router) | |
| app.include_router(stats_router) | |
| app.include_router(ws_router) | |
| async def on_startup(): | |
| """Create database tables and propagate env vars needed by third-party SDKs.""" | |
| import os | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| # Validate and propagate API keys | |
| if settings.openai_api_key: | |
| os.environ.setdefault("OPENAI_API_KEY", settings.openai_api_key) | |
| logger.info("Provider 'openai' configured with model gpt-4o") | |
| else: | |
| logger.warning("OPENAI_API_KEY not set - OpenAI provider disabled") | |
| if settings.gemini_api_key: | |
| os.environ.setdefault("GEMINI_API_KEY", settings.gemini_api_key) | |
| logger.info("Provider 'gemini' configured with model gemini-2.5-flash") | |
| else: | |
| logger.warning("GEMINI_API_KEY not set - Gemini provider disabled") | |
| # Log routing configuration | |
| logger.info(f"Routing policy: {settings.routing_policy}") | |
| logger.info(f"Default provider: {settings.default_provider}") | |
| await create_db_and_tables() | |
| async def root(): | |
| """Root endpoint for health check.""" | |
| return {"message": "Welcome to Phase II Full-Stack Todo App API"} | |
| async def health_check(): | |
| """Health check endpoint.""" | |
| from .core.database import db_health_check | |
| db_ok = await db_health_check() | |
| return { | |
| "status": "healthy", | |
| "database": "ok" if db_ok else "unavailable" | |
| } | |
| # For running with uvicorn | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) |