# sanchit-sherawat/cardapi architecture

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

- Interactive diagram: https://gitdiagram.com/sanchit-sherawat/cardapi
- Repository: https://github.com/sanchit-sherawat/cardapi

## Overview

##### Main Components
- API Entry Point: The main.go file serves as the entry point. This component initializes the application, sets up routing and middleware, and starts the server.
- Authentication Module: The “Auth” directory (e.g., Auth/Auth.go) handles user authentication processes likely involving JWT tokens.
- Controllers: The “controllers” directory (e.g., controllers/controllers.go) contains logic to handle incoming API requests, processing them before interacting with the business logic.
- Models: The “models” directory (e.g., models/models.go) defines the data models for flashcards, decks, and possibly users.
- Database Integration: The “db” folder holds the connection and factory logic (e.g., db/DbFactory.go) along with a specific MySQL implementation (e.g., db/mysql/mysqldb.go). The connection.go file further suggests that there’s a dedicated module for establishing database connections.
- Configuration & Dependencies: Files such as go.mod, go.sum, and config (referred in the README) ensure that the correct Go modules and dependencies are loaded.
- Auxiliary Components: GitHub templates and policies (e.g., CODE_OF_CONDUCT.md, CONTRIBUTING.md, SECURITY.md) indicate a well-structured project with attention to community contributions and security.

##### Relationships and Interactions
- The API Entry Point (main.go) initializes all other components, setting up routes that direct API calls to the corresponding controllers.
- Controllers serve as the mediator between incoming HTTP requests and business logic, often invoking methods in the Models for data manipulation and then calling the Database connector to persist data.
- The Auth module works hand-in-hand with controllers to secure endpoints by validating JWT tokens and managing user sessions.
- The Database layer (db directory) abstracts the actual MySQL operations (using a factory pattern as seen in DbFactory.go) and interacts directly with the Models.
- The configuration and dependency management (go.mod and go.sum) support modularity and smooth integration of external libraries.

##### Architectural Patterns and Design Principles
- The project employs a layered architecture, separating concerns into controllers (presentation layer), models (domain/business logic), and database connectors (data access layer).
- It appears to use a factory pattern in the DB layer (DbFactory.go) to create consistent database connections.
- JWT-based Authentication separates cross-cutting concerns (security) from business logic.

##### Technologies and Frameworks
- Language: Go (with dependency management via go.mod/go.sum)
- Database: MySQL (as implemented under db/mysql/mysqldb.go)
- API: REST endpoints plus Server-Sent Events (SSE) for real-time communication.
- Authentication: JWT for managing secure user sessions.
- CORS enabled for integration with frontend applications.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Client
    client["Client (API Consumer)"]:::client

    %% API/Controller Layer
    subgraph "API/Controller Layer"
        mainEntry["Main Entry (main.go)"]:::entry
        controllers["Controllers"]:::controller
        sse["SSE Endpoint (Real-time Updates)"]:::sse
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        auth["Auth Module"]:::auth
        models["Models"]:::model
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        dbFactory["DB Factory"]:::db
        mySQL["MySQL Implementation"]:::db
        dbConnection["Database Connection Module"]:::db
    end

    %% Configuration & Dependencies
    subgraph "Configuration & Dependencies"
        configMod["go.mod"]:::config
        configSum["go.sum"]:::config
    end

    %% Connections between components
    client -->|"HTTPRequest"| mainEntry
    mainEntry -->|"RouteSetup"| controllers
    mainEntry -->|"SSEInit"| sse
    configMod -->|"DepMgmt"| mainEntry
    controllers -->|"AuthCheck"| auth
    controllers -->|"RequestHandling"| models
    models -->|"DataPersistence"| dbFactory
    dbFactory -->|"MySQLOps"| mySQL
    dbFactory -->|"DBConn"| dbConnection

    %% Click Events
    click mainEntry "https://github.com/sanchit-sherawat/cardapi/blob/master/main.go"
    click auth "https://github.com/sanchit-sherawat/cardapi/blob/master/Auth/Auth.go"
    click controllers "https://github.com/sanchit-sherawat/cardapi/blob/master/controllers/controllers.go"
    click models "https://github.com/sanchit-sherawat/cardapi/blob/master/models/models.go"
    click dbFactory "https://github.com/sanchit-sherawat/cardapi/blob/master/db/DbFactory.go"
    click mySQL "https://github.com/sanchit-sherawat/cardapi/blob/master/db/mysql/mysqldb.go"
    click dbConnection "https://github.com/sanchit-sherawat/cardapi/blob/master/connection.go"
    click configMod "https://github.com/sanchit-sherawat/cardapi/blob/master/go.mod"
    click configSum "https://github.com/sanchit-sherawat/cardapi/blob/master/go.sum"

    %% Styling Classes
    classDef client fill:#B0C4DE,stroke:#333,stroke-width:2px;
    classDef entry fill:#FDD835,stroke:#333,stroke-width:2px;
    classDef controller fill:#81D4FA,stroke:#333,stroke-width:2px;
    classDef sse fill:#FFCC80,stroke:#333,stroke-width:2px;
    classDef auth fill:#EF9A9A,stroke:#333,stroke-width:2px;
    classDef model fill:#A5D6A7,stroke:#333,stroke-width:2px;
    classDef db fill:#CE93D8,stroke:#333,stroke-width:2px;
    classDef config fill:#FFE082,stroke:#333,stroke-width:2px;
```
