# c4lopsitta/cat-backend architecture

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

- Interactive diagram: https://gitdiagram.com/c4lopsitta/cat-backend
- Repository: https://github.com/c4lopsitta/cat-backend

## Overview

##### Major components
- Docker Containers: The project runs using Docker, so start by representing the three main containers:
- Nginx container: Acts as the web server and single page router for all /api/v1/... paths.
- PHP-FPM container: Hosts the PHP application, including the API handler and all the backend logic.
- MariaDB container: Provides the database backend.

- Application Layers within the PHP-FPM container:
- API Handler: Represent the central entry point (apiHandler.php) that processes incoming API requests.
- Routing and Handlers: The “app/BaseHandlers” folder contains specialized handlers (like Cats.php and Users.php) for different API endpoints.
- Data Access: The “app/DAO” directory includes various DAO classes (e.g., CatDAO, UserDAO, etc.) for interacting with the database and also Redis components.
- Domain Models: Identify “app/Model” which holds the core domain entities such as Cat, Token, and User.
- Utilities & Services: The “app/Utilities” directory, including email utilities and password/regex helpers, encapsulates common functionality.
- Testing/JSON Support: Additional components (e.g., app/Test and app/Jsons) support testing and facilitate JSON operations.

##### Relationships and data flows
- Clients send API requests that first hit the Nginx container.
- Nginx forwards these requests to the PHP-FPM container where apiHandler.php dispatches them to the appropriate BaseHandler (e.g., Cats or Users handler).
- The BaseHandlers work with the DAO layer to query or update data in the MariaDB (and possibly cache or additional data sources like Redis as indicated by app/DAO/Redis).
- Utility components (email sender, password management, etc.) support the business logic.

##### Architectural patterns and design principles
- Separation of Concerns: The architecture distinguishes clear responsibilities across different layers (routing, data access, domain modelling, and utility services).
- Containerization: The Docker-compose setup demonstrates micro-container architecture, with each container serving a dedicated purpose.

##### Relevant technologies and frameworks
- PHP with PHP-FPM for backend logic.
- Nginx serving as the reverse proxy and router for API requests.
- MariaDB for persistent storage.
- Use of Docker and docker-compose to orchestrate the containers.
- Additionally, utility libraries for emails, regular expressions, and unique ID generation show your use of helper functions for robust functionality.

- API Entrypoint (apiHandler.php)
- Base Handlers (with examples e.g., Cats.php, Users.php)
- DAO Layer (CatDAO, UserDAO, etc.)
- Models (Cat, Token, User)
- Utilities (emails, password, regex, etc.)
- API requests from clients to Nginx.
- Request forwarding from Nginx to PHP-FPM.
- Interactions from PHP-FPM’s API components to the MariaDB container.
- Any caching or secondary interactions (e.g., with Redis as indicated by the DAO classes).

## Diagram (Mermaid)

```mermaid
graph TD
    Client["Client Request"]:::client
    Nginx["Nginx Container"]:::nginx
    PHPFPM["PHP-FPM Container"]:::php
    MariaDB["MariaDB Container"]:::database

    Client -->|"HTTP_Request"| Nginx
    Nginx -->|"Forwards_Request"| PHPFPM
    PHPFPM -->|"DB_Interaction"| MariaDB

    subgraph "PHP-FPM Application Layers"
        A["API Entrypoint (apiHandler.php)"]:::handler
        B["Base Handlers (Cats, Users)"]:::handler
        C["DAO Layer (CatDAO, UserDAO, Redis)"]:::dao
        D["Domain Models (Cat, Token, User)"]:::model
        E["Utilities & Services"]:::util
        F["Testing"]:::test
        G["JSON Support"]:::test
    end

    PHPFPM --> A
    A -->|"Dispatches_to"| B
    B -->|"Uses"| C
    C -->|"Maps_Data_to"| D
    C -->|"Queries"| MariaDB
    E -.-> B
    E -.-> C
    F -.-> A
    G -.-> A

    click Nginx "https://github.com/c4lopsitta/cat-backend/blob/main/nginx/default.conf"
    click PHPFPM "https://github.com/c4lopsitta/cat-backend/blob/main/app/apiHandler.php"
    click MariaDB "https://github.com/c4lopsitta/cat-backend/blob/main/db.env.sample"
    click A "https://github.com/c4lopsitta/cat-backend/blob/main/app/apiHandler.php"
    click B "https://github.com/c4lopsitta/cat-backend/tree/main/app/BaseHandlers"
    click C "https://github.com/c4lopsitta/cat-backend/tree/main/app/DAO"
    click D "https://github.com/c4lopsitta/cat-backend/tree/main/app/Model"
    click E "https://github.com/c4lopsitta/cat-backend/tree/main/app/Utilities"
    click F "https://github.com/c4lopsitta/cat-backend/tree/main/app/Test"
    click G "https://github.com/c4lopsitta/cat-backend/tree/main/app/Jsons"

    classDef client fill:#f9e79f,stroke:#333,stroke-width:2px;
    classDef nginx fill:#a9cce3,stroke:#333,stroke-width:2px;
    classDef php fill:#a3e4d7,stroke:#333,stroke-width:2px;
    classDef database fill:#f7dc6f,stroke:#333,stroke-width:2px;
    classDef handler fill:#f5b7b1,stroke:#333,stroke-width:2px;
    classDef dao fill:#aed6f1,stroke:#333,stroke-width:2px;
    classDef model fill:#f9e79f,stroke:#333,stroke-width:2px;
    classDef util fill:#d2dbdd,stroke:#333,stroke-width:2px;
    classDef test fill:#d5f5e3,stroke:#333,stroke-width:2px;
```
