
- Setup project structure and dependencies - Create SQLite database with SQLAlchemy models - Initialize Alembic for database migrations - Implement JWT-based authentication utilities - Create API endpoints for signup, login, and logout - Add health check endpoint - Implement authentication middleware for protected routes - Update README with setup and usage instructions - Add linting with Ruff
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
from fastapi import Depends, FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.endpoints import router as api_router
|
|
from app.core.config import settings
|
|
from app.db.session import get_db
|
|
|
|
# Initialize FastAPI app
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
openapi_url=f"{settings.API_V1_STR}/openapi.json"
|
|
)
|
|
|
|
# Set up CORS middleware
|
|
if settings.BACKEND_CORS_ORIGINS:
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Add JWT authentication middleware
|
|
# Note: This middleware is commented out because we're already using FastAPI's
|
|
# dependency injection for authentication. Uncomment if you want to use middleware approach.
|
|
# app.middleware("http")(jwt_middleware())
|
|
|
|
# Include API router
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
|
|
# Health check endpoint
|
|
@app.get("/health", tags=["health"])
|
|
def health_check(db: Session = Depends(get_db)):
|
|
"""Health check endpoint to verify the service is running and database connection is working
|
|
"""
|
|
# Try to execute a simple query to verify database connection
|
|
try:
|
|
db.execute("SELECT 1")
|
|
db_status = "healthy"
|
|
except Exception as e:
|
|
db_status = f"unhealthy: {str(e)}"
|
|
|
|
return {
|
|
"status": "ok",
|
|
"database": db_status
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|