# keploy/orgchartapi architecture

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

- Interactive diagram: https://gitdiagram.com/keploy/orgchartapi
- Repository: https://github.com/keploy/orgchartapi

## Overview

##### Project Type and Purpose
- This is a **backend-only, RESTful API** written in C++ using the **Drogon** framework.
- It manages organizational data—**persons**, **departments**, and **jobs**—with **JWT-based authentication**.
- It runs in a Dockerized environment alongside a **PostgreSQL** database.

##### High-Level File Structure Analysis
- Top-level:
- `main.cc`, `CMakeLists.txt`, build scripts (`build.sh`, `run.sh`), Docker files (`Dockerfile`, `docker-compose.yml`).
- Configuration: `config.json`.
- Third-party libraries under `third_party` (Drogon, jsoncpp, jwt-cpp, libbcrypt).
- Source code:
- `controllers/` (AuthController, PersonsController, DepartmentsController, JobsController)
- `models/` (Person, Department, Job, User, plus JSON schemas)
- `filters/` (LoginFilter for JWT validation)
- `plugins/` (JwtPlugin for signing/verifying tokens)
- `utils/` (generic helper functions)
- Database setup: `scripts/` (SQL to create and seed DB).
- Tests under `test/`.

##### Key Architectural Patterns
- **MVC-style layering**:
- **Routing & HTTP layer** (Drogon)
- **Filter/Middleware** (JWT validation)
- **Controller layer** (request handling, input validation)
- **Model/Data access layer** (ORM or raw SQL via JSON models)
- **Database** (PostgreSQL)
- **Plugin-based extension**: JwtPlugin isolates token logic.
- **External dependencies**: jsoncpp for JSON, jwt-cpp for JWT, libbcrypt for password hashing.

##### Main Components
a. **API Gateway / HTTP Server**
- Drogon framework listening on port 3000.
b. **Authentication Filter**
- LoginFilter intercepts requests, validates JWT via JwtPlugin.
c. **Controller Modules**
- AuthController (register/login)
- PersonsController, DepartmentsController, JobsController (CRUD endpoints)
d. **Model Layer**
- C++ model classes representing DB tables.
- JSON schema (`model.json`) if relevant.
e. **Database**
- PostgreSQL instance (container “db”).
- SQL scripts used at startup.
f. **External Libraries**
- jsoncpp, jwt-cpp, libbcrypt.
g. **Infrastructure**
- Docker Compose orchestrating two containers: `app` and `postgres`.
- Build system (CMake).

##### Relationships and Data Flow
1. **Client** sends HTTP request →
2. **Drogon HTTP Server** receives →
3. **LoginFilter** checks Authorization header →
- If JWT missing/invalid, reject.
- If valid, attach user context to request.
4. **Controller** invoked based on route →
5. **Controller** calls **Model/Database** for CRUD →
6. **PostgreSQL** executes SQL → result returns to Controller →
7. **Controller** serializes response via jsoncpp →
8. **HTTP Server** returns JSON to Client.

For **/auth** routes:
- AuthController hashes password (bcrypt), stores/validates user in DB, generates JWT via JwtPlugin.

## Diagram (Mermaid)

```mermaid
flowchart TD
    direction TB

    subgraph "Docker Compose"
        direction TB

        subgraph "App Container"
            direction TB
            Client["Client"]:::external

            API["Drogon HTTP Server<br>:3000"]:::framework
            Filter["LoginFilter"]:::framework
            JwtPlugin["JwtPlugin"]:::external

            subgraph "Controllers"
                direction TB
                AuthC["AuthController"]:::business
                PersonsC["PersonsController"]:::business
                DepartmentsC["DepartmentsController"]:::business
                JobsC["JobsController"]:::business
            end

            subgraph "Models"
                direction TB
                PersonM["Person Model"]:::data
                DepartmentM["Department Model"]:::data
                JobM["Job Model"]:::data
                UserM["User Model"]:::data
                Schema["model.json"]:::doc
            end

            Utils["Utils"]:::external
            Infra["Build & Deployment<br>(CMake, Dockerfile, scripts)"]:::infra
        end

        subgraph "DB Container"
            direction TB
            Scripts["SQL Scripts"]:::doc
            PG["PostgreSQL<br>:5432"]:::db
        end
    end

    %% Connections
    Client -->|"HTTP Request"| API
    API -->|"intercepts"| Filter
    Filter -->|"Validate JWT"| JwtPlugin
    Filter -->|"forward"| AuthC
    AuthC -->|"hash/validate user"| UserM
    AuthC -->|"generate token"| JwtPlugin

    API -->|"route to controller"| PersonsC
    API -->|"route to controller"| DepartmentsC
    API -->|"route to controller"| JobsC

    PersonsC -->|"CRUD operations"| PersonM
    DepartmentsC -->|"CRUD operations"| DepartmentM
    JobsC -->|"CRUD operations"| JobM

    AuthC -->|"DB queries"| UserM
    PersonM -->|"Execute SQL"| PG
    DepartmentM -->|"Execute SQL"| PG
    JobM -->|"Execute SQL"| PG
    UserM -->|"Execute SQL"| PG

    Scripts -->|"init/seed DB"| PG

    %% Click Events
    click API "https://github.com/keploy/orgchartapi/blob/master/main.cc"
    click API "https://github.com/keploy/orgchartapi/blob/master/CMakeLists.txt"
    click API "https://github.com/keploy/orgchartapi/tree/master/third_party/drogon/"
    click Filter "https://github.com/keploy/orgchartapi/blob/master/filters/LoginFilter.cc"
    click Filter "https://github.com/keploy/orgchartapi/blob/master/filters/LoginFilter.h"
    click JwtPlugin "https://github.com/keploy/orgchartapi/blob/master/plugins/JwtPlugin.cc"
    click JwtPlugin "https://github.com/keploy/orgchartapi/blob/master/plugins/JwtPlugin.h"
    click JwtPlugin "https://github.com/keploy/orgchartapi/blob/master/plugins/Jwt.cc"
    click JwtPlugin "https://github.com/keploy/orgchartapi/blob/master/plugins/Jwt.h"
    click AuthC "https://github.com/keploy/orgchartapi/blob/master/controllers/AuthController.cc"
    click AuthC "https://github.com/keploy/orgchartapi/blob/master/controllers/AuthController.h"
    click PersonsC "https://github.com/keploy/orgchartapi/blob/master/controllers/PersonsController.cc"
    click PersonsC "https://github.com/keploy/orgchartapi/blob/master/controllers/PersonsController.h"
    click DepartmentsC "https://github.com/keploy/orgchartapi/blob/master/controllers/DepartmentsController.cc"
    click DepartmentsC "https://github.com/keploy/orgchartapi/blob/master/controllers/DepartmentsController.h"
    click JobsC "https://github.com/keploy/orgchartapi/blob/master/controllers/JobsController.cc"
    click JobsC "https://github.com/keploy/orgchartapi/blob/master/controllers/JobsController.h"
    click PersonM "https://github.com/keploy/orgchartapi/blob/master/models/Person.cc"
    click PersonM "https://github.com/keploy/orgchartapi/blob/master/models/Person.h"
    click PersonM "https://github.com/keploy/orgchartapi/blob/master/models/PersonInfo.cc"
    click PersonM "https://github.com/keploy/orgchartapi/blob/master/models/PersonInfo.h"
    click DepartmentM "https://github.com/keploy/orgchartapi/blob/master/models/Department.cc"
    click DepartmentM "https://github.com/keploy/orgchartapi/blob/master/models/Department.h"
    click JobM "https://github.com/keploy/orgchartapi/blob/master/models/Job.cc"
    click JobM "https://github.com/keploy/orgchartapi/blob/master/models/Job.h"
    click UserM "https://github.com/keploy/orgchartapi/blob/master/models/User.cc"
    click UserM "https://github.com/keploy/orgchartapi/blob/master/models/User.h"
    click Schema "https://github.com/keploy/orgchartapi/blob/master/models/model.json"
    click Scripts "https://github.com/keploy/orgchartapi/blob/master/scripts/create_db.sql"
    click Scripts "https://github.com/keploy/orgchartapi/blob/master/scripts/seed_db.sql"
    click Infra "https://github.com/keploy/orgchartapi/blob/master/CMakeLists.txt"
    click Infra "https://github.com/keploy/orgchartapi/blob/master/build.sh"
    click Infra "https://github.com/keploy/orgchartapi/blob/master/run.sh"
    click Infra "https://github.com/keploy/orgchartapi/tree/master/Dockerfile"
    click Infra "https://github.com/keploy/orgchartapi/blob/master/docker-compose.yml"
    click Utils "https://github.com/keploy/orgchartapi/blob/master/utils/utils.cc"
    click Utils "https://github.com/keploy/orgchartapi/blob/master/utils/utils.h"

    %% Styles
    classDef framework fill:#D0E6FA,stroke:#1E70BF,color:#1E70BF;
    classDef business fill:#D5F5D3,stroke:#27AE60,color:#145A32;
    classDef data fill:#F9E79F,stroke:#D68910,color:#6E2C00;
    classDef db fill:#FADBD8,stroke:#C0392B,color:#78281F;
    classDef external fill:#E8E8E8,stroke:#7F8C8D,color:#2C3E50;
    classDef doc fill:#F2F3F4,stroke:#95A5A6,color:#566573;
    classDef infra fill:#E5E7E9,stroke:#85929E,color:#283747;
```
