# andrescampuzano/golang-dashboard architecture

> Architecture diagram of the GitHub repository andrescampuzano/golang-dashboard, 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/andrescampuzano/golang-dashboard
- Repository: https://github.com/andrescampuzano/golang-dashboard

## Overview

##### Main Components
- API Entry Point & Router: Represented in files like main.go and api.go which configure the HTTP server (using Gorilla Mux).
- Authentication Module: The auth.go file handles JWT-based authentication and authorization.
- Service Layers: There are dedicated service files (for example, customer.service.go, expense.service.go, earnings.service.go, etc.) that encapsulate business logic for users, customers, products, sales, expenses, and earnings.
- Storage/Data Access Layers: For each entity, there are corresponding storage files (e.g., customer.storage.go, product.storage.go, sale.storage.go, etc.) which abstract and handle interactions with the PostgreSQL database. The database.go file sets up database connection and schema initializations, with migrations located in the migrations folder.
- External Integrations:
- AWS S3: s3.go shows integration with AWS’s S3 bucket for file uploads and deletions.
- Email Service: The emails.go and SendGrid dependencies indicate a component responsible for sending emails.
- Environment Configuration: The .env.example file and usage of godotenv in Go indicate a configuration management component.
- Helper/Utility Functions: Files like utils.go support common functionalities such as JSON formatting and HTTP routing.

##### Relationships and Data Flow
- The HTTP Server (main.go) sets up the routing and directs incoming requests to the appropriate service.
- Each service (for example, customer.service.go) handles business logic then interacts with its storage layer (e.g., customer.storage.go) to perform CRUD operations on the PostgreSQL database.
- Authentication (auth.go) intercepts requests to verify JWT tokens before granting access to protected endpoints.
- External communications: The AWS S3 component handles file uploads/downloads, and the email functionality uses SendGrid to deliver emails.
- Database Connection: The database.go file and migrations ensure connectivity and schema setup for PostgreSQL.

##### Architectural Patterns & Design Principles
- Separation of Concerns: The design distinctly separates routing, business logic (services), and data access (storage).
- Modular Design: Each entity (user, customer, product, sale, expense, earnings) has its own service and storage files, ensuring loose coupling and easier maintenance.
- External Service Integration: The project modularly integrates with third-party services (AWS S3, SendGrid, PostgreSQL) through dedicated components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global entities
    client("Client (API Consumer)"):::external
    api("API Gateway / HTTP Server"):::internal
    auth("Authentication Module"):::internal

    %% Subgraph for Service Layers
    subgraph "Service Layers"
        service_user("User Service"):::service
        service_customer("Customer Service"):::service
        service_product("Product Service"):::service
        service_sale("Sale Service"):::service
        service_expense("Expense Service"):::service
        service_earnings("Earnings Service"):::service
    end

    %% Subgraph for Storage Layers
    subgraph "Storage Layers"
        storage_user("User Storage"):::storage
        storage_customer("Customer Storage"):::storage
        storage_product("Product Storage"):::storage
        storage_sale("Sale Storage"):::storage
        storage_expense("Expense Storage"):::storage
        storage_earnings("Earnings Storage"):::storage
    end

    %% Database and Setup
    db_setup("Database Connection & Schema"):::internal
    db("PostgreSQL Database"):::database

    %% External Integrations
    aws("AWS S3 Integration"):::external_service
    email("SendGrid Email Service"):::external_service

    %% Configuration and Utilities
    env(".env Configuration"):::config
    utils("Helper/Utility Functions"):::utility

    %% Connections / Data Flow
    client --> api
    api --> auth
    auth --> service_user
    auth --> service_customer
    auth --> service_product
    auth --> service_sale
    auth --> service_expense
    auth --> service_earnings

    %% Link Service Layers to corresponding Storage Layers
    service_user --> storage_user
    service_customer --> storage_customer
    service_product --> storage_product
    service_sale --> storage_sale
    service_expense --> storage_expense
    service_earnings --> storage_earnings

    %% Link Storage Layers to Database Setup and Database
    storage_user --> db_setup
    storage_customer --> db_setup
    storage_product --> db_setup
    storage_sale --> db_setup
    storage_expense --> db_setup
    storage_earnings --> db_setup
    db_setup --> db

    %% External Integrations from Services (example associations)
    service_product -->|"calls"| aws
    service_user -->|"triggers"| email

    %% Configuration and Utility dependencies
    api --> env
    api --> utils

    %% Click Events for Component Mappings
    click api "https://github.com/andrescampuzano/golang-dashboard/blob/main/main.go"
    click api "https://github.com/andrescampuzano/golang-dashboard/blob/main/api.go"
    click auth "https://github.com/andrescampuzano/golang-dashboard/blob/main/auth.go"
    click service_customer "https://github.com/andrescampuzano/golang-dashboard/blob/main/customer.service.go"
    click storage_customer "https://github.com/andrescampuzano/golang-dashboard/blob/main/customer.storage.go"
    click service_expense "https://github.com/andrescampuzano/golang-dashboard/blob/main/expense.service.go"
    click storage_expense "https://github.com/andrescampuzano/golang-dashboard/blob/main/expense.storage.go"
    click service_earnings "https://github.com/andrescampuzano/golang-dashboard/blob/main/earnings.service.go"
    click storage_earnings "https://github.com/andrescampuzano/golang-dashboard/blob/main/earnings.storage.go"
    click service_product "https://github.com/andrescampuzano/golang-dashboard/blob/main/product.service.go"
    click storage_product "https://github.com/andrescampuzano/golang-dashboard/blob/main/product.storage.go"
    click service_sale "https://github.com/andrescampuzano/golang-dashboard/blob/main/sale.service.go"
    click storage_sale "https://github.com/andrescampuzano/golang-dashboard/blob/main/sale.storage.go"
    click service_user "https://github.com/andrescampuzano/golang-dashboard/blob/main/user.service.go"
    click storage_user "https://github.com/andrescampuzano/golang-dashboard/blob/main/user.storage.go"
    click db_setup "https://github.com/andrescampuzano/golang-dashboard/blob/main/database.go"
    click db_setup "https://github.com/andrescampuzano/golang-dashboard/blob/main/migrations/1.sql"
    click aws "https://github.com/andrescampuzano/golang-dashboard/blob/main/s3.go"
    click email "https://github.com/andrescampuzano/golang-dashboard/blob/main/emails.go"
    click env "https://github.com/andrescampuzano/golang-dashboard/blob/main/.env.example"
    click utils "https://github.com/andrescampuzano/golang-dashboard/blob/main/utils.go"

    %% Styles
    classDef internal fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef external fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef service fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef storage fill:#D5F5E3,stroke:#145A32,stroke-width:2px;
    classDef database fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef external_service fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef config fill:#D6EAF8,stroke:#1B4F72,stroke-width:2px;
    classDef utility fill:#E8DAEF,stroke:#6C3483,stroke-width:2px;
```
