# ruslanlesko/brightonum architecture

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

- Interactive diagram: https://gitdiagram.com/ruslanlesko/brightonum
- Repository: https://github.com/ruslanlesko/brightonum

## Overview

##### Main components
- API Gateway/Entry Point: Representing the REST API endpoints exposed on port 2525. (This is where requests such as user creation, invite sending, token issuance, etc., hit the server.)
- Authentication & Authorization Service: The core logic that processes JWT tokens—both issuance (access and refresh tokens) and verification. Include components from “src/auth.go.”
- Service Layer: A central business logic component, likely represented by “src/service.go,” that orchestrates operations between multiple internal services.
- Data Access Layer (DAO): The “src/dao” module, with its specific implementations (e.g., mongodb.go) and mocks. This connects directly to MongoDB.
- Cryptographic Module: Components in “src/crypto” handling RSA key operations and signing of JWT tokens.
- Email Service: Represented by “src/email” (i.e., mailer.go), which is responsible for sending invitation emails, password recovery, and verification emails.
- Shared Data Structures: The “src/structs” directory contains definitions for error handling, user responses, etc.
- External Dependencies: MongoDB (note: the README emphasizes that MongoDB is required), an email server for sending emails (currently geared towards Gmail), and RSA keys (provided via command-line parameters).

##### Relationships and Data Flow
- User requests from clients (e.g., user invite, registration, authentication, password recovery) are received at the API Gateway.
- The API Gateway forwards requests to the Service Layer. For example, when issuing a token, the service might first validate credentials with the Auth component.
- The Authentication Service calls the Cryptographic Module for token signing and verification.
- Wherever persistence is required (e.g., storing user data, invite codes), the Service Layer delegates to the DAO, which connects to the MongoDB database.
- Email-related flows are handled by the Email Service, which communicates with external SMTP servers.
- Testing mocks illustrate that the DAO and Email modules have abstraction layers for easier integration testing.

##### Architectural Patterns & Design Principles
- Separation of Concerns: The code design separates API handling, business logic, persistence (DAO), security (crypto), and notifications (email).
- Dependency Injection/Mocking: Presence of “*_mock.go” files for both DAO and mailer indicates that the system is designed to allow for unit testing and a high degree of testability.
- Modularization: Each function (authentication, emailing, database operations) is encapsulated in dedicated packages, promoting maintainability and extensibility.

##### Relevant Technologies and Frameworks
- Go (as the primary programming language) with dependency management via go.mod and go.sum.
- MongoDB as the data storage system.
- OpenSSL (for generating RSA keys) as described in the README.
- Docker and GitHub Actions (noted by the Dockerfile and GitHub workflow files), indicating that containerization and CI/CD pipelines are part of the project's ecosystem.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["Client Request"]:::external

    %% Internal Components
    subgraph "BrightonUM Internal Components"
        SL["Service Layer (API & Business Logic)"]:::internal
        AS["Authentication Service (JWT)"]:::internal
        DAO["Data Access Layer (DAO)"]:::internal
        CM["Cryptographic Module (RSA)"]:::internal
        Email["Email Service"]:::internal
        SD["Shared Data Structures"]:::internal
    end

    %% External Dependencies
    subgraph "External Systems"
        Mongo["MongoDB Database"]:::externalStyle
        SMTP["SMTP Email Server"]:::externalStyle
    end

    %% Test Mocks
    subgraph "Test Mocks"
        DAO_Mock["DAO Mock Implementation"]:::internal
        Mailer_Mock["Email Mock Implementation"]:::internal
    end

    %% Data Flow
    Client -->|"HTTP Request"| SL
    SL -->|"Calls Auth"| AS
    SL -->|"Delegates Persistence"| DAO
    SL -->|"Triggers Email"| Email
    SL ---|"Uses Shared"| SD

    AS -->|"Uses Crypto"| CM

    DAO -->|"Reads/Writes"| Mongo
    Email -->|"Sends via"| SMTP

    %% Test Mocks Relationships
    DAO_Mock ---|"Mocking"| DAO
    Mailer_Mock ---|"Mocking"| Email

    %% Click Events
    click SL "https://github.com/ruslanlesko/brightonum/blob/master/src/service.go"
    click AS "https://github.com/ruslanlesko/brightonum/blob/master/src/auth.go"
    click DAO "https://github.com/ruslanlesko/brightonum/tree/master/src/dao"
    click CM "https://github.com/ruslanlesko/brightonum/blob/master/src/crypto/crypto.go"
    click Email "https://github.com/ruslanlesko/brightonum/blob/master/src/email/mailer.go"
    click SD "https://github.com/ruslanlesko/brightonum/tree/master/src/structs"
    click DAO_Mock "https://github.com/ruslanlesko/brightonum/blob/master/src/dao/dao_mock.go"
    click Mailer_Mock "https://github.com/ruslanlesko/brightonum/blob/master/src/email/mailer_mock.go"

    %% Styles
    classDef internal fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef external fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef externalStyle fill:#a5d6a7,stroke:#1b5e20,stroke-width:2px;
    
    %% Ensure vertical layout with rank settings
    Client --- SL
    SL --- AS
    SL --- DAO
    SL --- Email
    AS --- CM
    DAO --- Mongo
    Email --- SMTP
```
