# wpcodevo/fastapi_mongodb architecture

> Architecture diagram of the GitHub repository wpcodevo/fastapi_mongodb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wpcodevo/fastapi_mongodb
- Repository: https://github.com/wpcodevo/fastapi_mongodb

## Overview

##### Project type and purpose
- This is a backend-only, Docker-orchestrated RESTful API service built with Python’s FastAPI framework.
- It provides user registration, email verification, JWT-based authentication, and CRUD operations on “posts,” all persisted in a MongoDB database.

##### File structure
- Top-level files:
- docker-compose.yml: defines containers for the app and MongoDB
- Makefile, requirements.txt, .env: build & environment configuration
- app/ directory layers:
- config.py & .env: application configuration via Pydantic
- database.py: establishes MongoDB connection (PyMongo)
- email.py & templates/: SMTP client + Jinja2 HTML email templates
- oauth2.py & utils.py: JWT signing, token verification, and password utilities
- routers/: FastAPI router modules (auth.py, user.py, post.py)
- schemas.py: Pydantic models for request/response validation
- serializers/: BSON-to-JSON mappers for MongoDB documents
- main.py: initializes FastAPI app, middleware (CORS), mounts routers

##### README for additional insights
- Confirms three main features: JWT auth, email verification, CRUD operations
- Emphasizes Docker Compose to spin up FastAPI + MongoDB
- Uses FastAPI JWT Auth package, PyMongo, Jinja2

a. Components
1. API Container (FastAPI app)
- Configuration module
- Router layer (auth, user, post)
- Service/utility layer (database client, JWT utils, email sender)
- Serialization/schema layer (Pydantic + custom serializers)
2. MongoDB Container (data store)
3. SMTP Server / Email Provider (external)
4. Clients
- Web/mobile clients (Postman, frontend apps) interacting via HTTP
b. Relationships & data flow
- Clients → FastAPI HTTP endpoints
- FastAPI Routers call controllers → utilities
- Database client (PyMongo) ↔ MongoDB
- JWT utils ↔ router dependency for protected routes
- Email sender uses SMTP to dispatch verification emails
c. Architectural patterns & design principles
- Layered architecture: API layer, business logic layer, data access layer
- Dependency injection in FastAPI (router dependencies for DB, auth)
- Separation of concerns: schemas vs. serializers vs. routers vs. utils
- Containerization (Docker Compose) for environment reproducibility
d. Key technologies & frameworks
- FastAPI (ASGI server)
- Pydantic (settings, schemas)
- PyMongo (database driver)
- FastAPI-JWT-Auth (JWT handling)
- Jinja2 (email templates)
- Docker Compose (container orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Docker Compose boundary
    subgraph "Docker Compose"
        direction TB
        subgraph fastapi_app["fastapi_app (Container)"]
            direction TB
            %% Configuration Layer
            Env["Environment Variables"]:::config
            Config["Config Module"]:::config

            %% Application Entry
            Main["Application Entry Point"]:::internal

            %% Routers
            subgraph "Routers"
                direction TB
                AuthR["Auth Router"]:::router
                UserR["User Router"]:::router
                PostR["Post Router"]:::router
            end

            %% Services
            subgraph "Services"
                direction TB
                DBClient["Database Client Service"]:::service
                OAuth2["JWT Utilities"]:::service
                Utils["Password & Utils"]:::service
                EmailSvc["Email Service"]:::service
            end

            %% Templates
            subgraph "Email Templates"
                direction TB
                BaseT["base.html"]:::template
                StylesT["_styles.html"]:::template
                VerifyT["verification.html"]:::template
            end

            %% Schemas & Serializers
            subgraph "Schemas & Serializers"
                direction TB
                Schemas["Schema Definitions"]:::schema
                PostSer["Post Serializer"]:::schema
                UserSer["User Serializer"]:::schema
            end
        end

        subgraph mongodb["mongodb (Container)"]
            direction TB
            MongoDB["MongoDB"]:::database
        end
    end

    %% External systems
    Clients["Clients (Web/Mobile/Postman)"]:::external
    SMTP["SMTP Provider"]:::external

    %% Relationships
    Clients -->|HTTP Requests| AuthR
    Clients -->|HTTP Requests| UserR
    Clients -->|HTTP Requests| PostR

    AuthR -->|"uses DB client"| DBClient
    UserR -->|"uses DB client"| DBClient
    PostR -->|"uses DB client"| DBClient

    AuthR -->|"protects with JWT"| OAuth2
    UserR -->|"protects with JWT"| OAuth2
    PostR -->|"protects with JWT"| OAuth2

    AuthR --\- dashed --> EmailSvc

    DBClient -->|"read/write"| MongoDB
    MongoDB -->|"response"| DBClient

    EmailSvc --\- dashed --> SMTP

    Main --> Config
    Main --> Env
    Main --> AuthR
    Main --> UserR
    Main --> PostR
    Main --> DBClient
    Main --> OAuth2
    Main --> Utils
    Main --> EmailSvc
    Main --> Schemas
    Main --> PostSer
    Main --> UserSer
    Config --> Env

    %% Click events
    click Config "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/config.py"
    click Env "https://github.com/wpcodevo/fastapi_mongodb/blob/master/.env"
    click DBClient "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/database.py"
    click EmailSvc "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/email.py"
    click BaseT "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/templates/base.html"
    click StylesT "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/templates/_styles.html"
    click VerifyT "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/templates/verification.html"
    click OAuth2 "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/oauth2.py"
    click Utils "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/utils.py"
    click AuthR "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/routers/auth.py"
    click UserR "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/routers/user.py"
    click PostR "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/routers/post.py"
    click Schemas "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/schemas.py"
    click PostSer "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/serializers/postSerializers.py"
    click UserSer "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/serializers/userSerializers.py"
    click Main "https://github.com/wpcodevo/fastapi_mongodb/blob/master/app/main.py"
    click DockerCompose "https://github.com/wpcodevo/fastapi_mongodb/blob/master/docker-compose.yml"
    click Makefile "https://github.com/wpcodevo/fastapi_mongodb/tree/master/Makefile"
    click Requirements "https://github.com/wpcodevo/fastapi_mongodb/blob/master/requirements.txt"

    %% Styles
    classDef container fill:#D0E8FF,stroke:#000,color:#000;
    classDef database fill:#BFBFBF,stroke:#333,color:#000;
    classDef external fill:#FFE0B2,stroke:#E67E22,color:#000;
    classDef router fill:#A9D18E,stroke:#2E7D32,color:#000;
    classDef service fill:#FFD966,stroke:#B8860B,color:#000;
    classDef config fill:#C6E0B4,stroke:#558B2F,color:#000;
    classDef schema fill:#D9D2E9,stroke:#7B1FA2,color:#000;
    classDef template fill:#F4CCCC,stroke:#C62828,color:#000;
    classDef internal fill:#E2EFDA,stroke:#388E3C,color:#000;
```
