
- Fix code linting issues - Update README with detailed documentation - Configure database paths for the current environment - Create necessary directory structure The News Aggregation Service is now ready to use with FastAPI and SQLite.
82 lines
1.9 KiB
Python
82 lines
1.9 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.openapi.utils import get_openapi
|
|
import uvicorn
|
|
import logging
|
|
from app.api.v1.api import api_router
|
|
from app.core.config import settings
|
|
from app.core.utils import configure_logging
|
|
from app.models import base # noqa: F401
|
|
from app.services.background_tasks import start_background_tasks
|
|
|
|
# Configure logging
|
|
configure_logging()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
openapi_url="/openapi.json",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
)
|
|
|
|
# Set all CORS enabled origins
|
|
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=["*"],
|
|
)
|
|
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
|
|
@app.get("/health", tags=["health"])
|
|
async def health_check():
|
|
"""
|
|
Health check endpoint
|
|
"""
|
|
return {"status": "ok"}
|
|
|
|
|
|
def custom_openapi():
|
|
if app.openapi_schema:
|
|
return app.openapi_schema
|
|
openapi_schema = get_openapi(
|
|
title=settings.PROJECT_NAME,
|
|
version=settings.VERSION,
|
|
description=settings.DESCRIPTION,
|
|
routes=app.routes,
|
|
)
|
|
app.openapi_schema = openapi_schema
|
|
return app.openapi_schema
|
|
|
|
|
|
app.openapi = custom_openapi
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
"""
|
|
Function that runs on application startup.
|
|
"""
|
|
logger.info("Starting up application...")
|
|
|
|
# Start background tasks
|
|
await start_background_tasks()
|
|
|
|
logger.info("Application startup complete.")
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
async def shutdown_event():
|
|
"""
|
|
Function that runs on application shutdown.
|
|
"""
|
|
logger.info("Shutting down application...")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |