# krithick-j/content-moderation architecture

> Architecture diagram of the GitHub repository krithick-j/content-moderation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/krithick-j/content-moderation
- Repository: https://github.com/krithick-j/content-moderation

## Overview

##### Project’s Purpose and Type
- This project is a full-stack API service focused on content moderation using OpenAI’s models. It uses FastAPI for the API layer, Celery+Redis for asynchronous task processing, PostgreSQL as the primary data store, and Prometheus for monitoring. The system is containerized via Docker/Docker Compose and includes database migrations managed by Alembic.

##### File Structure
- The file tree shows clear modular separation:
- The “app” directory contains folders for API endpoints (app/api), models (app/models), database repositories (app/repo), schemas (app/schemas), tasks (app/tasks), middleware (app/middleware), monitoring (app/monitoring), and utility functions (app/utils).
- The “configs” subdirectory under “app” holds configuration details for Celery, database, logs, and Redis.
- “alembic” and its related files indicate management of database migrations.
- The presence of Dockerfile, docker-compose.yml, and additional containerization scripts (init-db.sh, envexample.txt) reinforce the microservices or container-based deployment approach.
- This structure suggests clear separation of concerns between API endpoints, business logic, asynchronous workers, and integrations (monitoring, DB, caching).

##### README for Additional Insights
- The README confirms that asynchronous Celery tasks execute content moderation in the background, with rate limiting, health checks, and integrated logging.
- It highlights core components: FastAPI, Celery with Redis, PostgreSQL, Prometheus, and Alembic.

a. Main Components:
- API Layer: FastAPI endpoints (located in app/api) handling both POST requests (to moderate text) and GET requests (to fetch results).
- Task/Worker System: Celery tasks defined (app/tasks/celery_task.py) processing moderation asynchronously with Redis acting as the message broker.
- Database Layer: PostgreSQL with migration scripts managed by Alembic.
- Monitoring and Logging: Prometheus integration (app/monitoring/prometheus.py) and logging configurations.
- Configuration & Middleware: Repository (app/repo), Models (app/models), Middleware (app/middleware), and utility functions.
- Containerization: Docker and Docker Compose orchestrate the service deployments.

b. Relationships and Interactions:
- The API layer (FastAPI) receives client requests and forwards content for moderation.
- On receiving a moderation request, FastAPI submits a task to the Celery worker.
- The Celery tasks use Redis as a broker for asynchronous communications.
- Processed results may be stored or updated in the PostgreSQL database.
- The repository layer (app/repo) is used to abstract database interactions.
- Monitoring components scrape endpoints (e.g., /metrics) for system health and performance.
- The logging functionality and middleware intercept errors and rate limit issues, providing insights into request-processing flows.

c. Architectural Patterns and Design Principles:
- Use of microservices/containerization allows for horizontal scaling.
- The asynchronous task pattern (Celery+Redis) decouples API responsiveness from heavy processing.
- The configuration segregation (configs folder) ensures environment-specific parameters are maintained cleanly.
- Modularity is key, with separate components for API, tasks, data access, and monitoring.

- Out FastAPI (API layer), Celery & Redis (asynchronous task processing), PostgreSQL (database), and Prometheus (monitoring).
- Integration points like the database migration tool (Alembic) and the use of SQLAlchemy as the ORM.

##### Specific Elements
- FastAPI application (and its endpoints).
- Celery Task Queue and Worker processes.
- PostgreSQL Database along with Alembic for migrations.
- Redis acting as the message broker.
- Prometheus for monitoring metrics.
- Annotations or notes about key configuration files (like alembic.ini for migrations, Dockerfile and docker-compose.yml for container orchestration) that demonstrate the deployment and environment management strategies.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client"]:::external

    subgraph "API Layer"
        API["FastAPI API Endpoints"]:::app
    end

    subgraph "Task Processing"
        CELERY["Celery Task Worker"]:::app
        REDIS["Redis Message Broker"]:::external
    end

    subgraph "Database Layer"
        REPO["Repository Layer"]:::app
        POSTGRES["PostgreSQL Database"]:::external
        ALEM["Alembic Migrations"]:::external
    end

    subgraph "Monitoring & Middleware"
        PROM["Prometheus Monitoring"]:::external
        MIDDLE["Monitoring/Logging Middleware"]:::app
    end

    subgraph "Configurations & Deployment"
        CONFIG["Configuration Files"]:::app
        DOCKER_D["Dockerfile"]:::external
        DOCKER_C["docker-compose.yml"]:::external
    end

    Client -->|"sends_requests"| API
    API -->|"dispatch_task"| CELERY
    API -->|"db_request"| REPO
    CELERY -->|"process_task"| REDIS
    CELERY -->|"update_db"| REPO
    REPO -->|"data_storage"| POSTGRES
    ALEM -->|"db_migrations"| POSTGRES
    API -->|"log"| MIDDLE
    CELERY -->|"log"| MIDDLE
    API -->|"metrics"| PROM
    CELERY -->|"metrics"| PROM
    POSTGRES -->|"metrics"| PROM
    REDIS -->|"metrics"| PROM
    CONFIG --> API
    CONFIG --> CELERY
    CONFIG --> REPO
    CONFIG --> PROM
    DOCKER_D --- API
    DOCKER_D --- CELERY
    DOCKER_D --- POSTGRES
    DOCKER_C --- API
    DOCKER_C --- CELERY
    DOCKER_C --- POSTGRES

    click API "https://github.com/krithick-j/content-moderation/blob/main/app/api/moderation.py"
    click CELERY "https://github.com/krithick-j/content-moderation/blob/main/app/tasks/celery_task.py"
    click REPO "https://github.com/krithick-j/content-moderation/blob/main/app/repo/moderation.py"
    click ALEM "https://github.com/krithick-j/content-moderation/blob/main/alembic.ini"
    click PROM "https://github.com/krithick-j/content-moderation/blob/main/app/monitoring/prometheus.py"
    click CONFIG "https://github.com/krithick-j/content-moderation/tree/main/app/configs/"
    click MIDDLE "https://github.com/krithick-j/content-moderation/blob/main/app/middleware/monitoring.py"
    click DOCKER_D "https://github.com/krithick-j/content-moderation/tree/main/Dockerfile"
    click DOCKER_C "https://github.com/krithick-j/content-moderation/blob/main/docker-compose.yml"

    classDef app fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef external fill:#d4edda,stroke:#155724,stroke-width:2px;
```
