- Updated database configuration to switch from SQLite to PostgreSQL, including changes to alembic.ini, Docker Compose, and environment settings. - Refactored application code to utilize PostgreSQL database adapters, ensuring compatibility with the new database structure. - Enhanced API routes and data handling to support the new database, including adjustments in data models and query logic. - Introduced new job processing mechanisms for full synchronization of AMO CRM entities, leveraging FastStream for background tasks. - Improved logging and error handling across the application to facilitate better monitoring and debugging. - Removed obsolete SQLite adapter files and migrations, streamlining the project structure for PostgreSQL integration.
85 lines
2.3 KiB
Python
85 lines
2.3 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
from typing import AsyncGenerator
|
|
import uvicorn
|
|
import logging
|
|
|
|
from adapters.postgres.database import init_db
|
|
from routers import entities, export, data, amocrm
|
|
from utils.config import settings
|
|
|
|
# FastStream integration
|
|
from workers.broker import broker
|
|
from workers.middleware import setup_middleware
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=settings.LOG_LEVEL)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# Setup FastStream middleware
|
|
setup_middleware(broker)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def app_lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
|
|
# Startup
|
|
logger.info("Starting AMO CRM Data Collection Service")
|
|
await init_db()
|
|
logger.info("Database initialized successfully")
|
|
|
|
# Start broker
|
|
await broker.start()
|
|
logger.info("Redis broker connected successfully")
|
|
|
|
yield
|
|
|
|
# Shutdown
|
|
logger.info("Shutting down AMO CRM Data Collection Service")
|
|
await broker.close()
|
|
logger.info("Redis broker closed")
|
|
|
|
|
|
app = FastAPI(
|
|
title="AMO CRM Data Collection Service",
|
|
description="Service for collecting and exporting AMO CRM data to Google Sheets with FastStream workers",
|
|
version="0.1.0",
|
|
lifespan=app_lifespan,
|
|
)
|
|
|
|
# CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # Configure appropriately for production
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include API routers
|
|
app.include_router(entities.router, prefix=f"{settings.API_V1_STR}/entities", tags=["entities"])
|
|
app.include_router(export.router, prefix=f"{settings.API_V1_STR}/export", tags=["export"])
|
|
app.include_router(data.router, prefix=f"{settings.API_V1_STR}/data", tags=["data"])
|
|
app.include_router(amocrm.router, prefix=f"{settings.API_V1_STR}/amocrm", tags=["amocrm"])
|
|
|
|
|
|
@app.get("/")
|
|
async def root() -> dict[str, str]:
|
|
return {"message": "AMO CRM Data Collection Service", "version": "0.1.0"}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check() -> dict[str, str]:
|
|
return {"status": "healthy"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(
|
|
"app:app",
|
|
host="0.0.0.0",
|
|
port=8000,
|
|
reload=True,
|
|
log_level=settings.LOG_LEVEL.lower(),
|
|
)
|