# ngtruong624/backendshop_go architecture

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

- Interactive diagram: https://gitdiagram.com/ngtruong624/backendshop_go
- Repository: https://github.com/ngtruong624/backendshop_go

## Overview

##### Project type and purpose
- This is a backend‐only RESTful API service written in Go, using the Gin web framework and PostgreSQL as its data store.
- Its main responsibilities are user authentication (JWT), role‐based access control, and CRUD operations on products with pagination and filtering.

##### File structure
- **cmd/api/main.go**: entry point that initializes Gin, loads environment variables, connects to PostgreSQL, wires up middleware, routes, and starts the HTTP server.
- **internal/handlers/**: HTTP “controllers” (auth_handler.go, product_handler.go) that parse requests, call into the repository layer, and return responses.
- **internal/middleware/auth.go**: JWT authentication and role‐based access checks applied to protected routes.
- **internal/repository/**: data access layer (product_repo.go) that encapsulates SQL queries or ORM operations against PostgreSQL.
- **internal/utils/response.go**: standard response formatting (error/success wrappers).
- **.github/workflows/go.yml**: CI pipeline for Go (linting, testing, building).
- **go.mod / go.sum**: dependency management.

##### Extract additional architecture hints from the README
- The system uses environment variables to configure DB connection and JWT secrets.
- Postgres is the only external dependency at runtime.
- Service is single‐process, layered application (no microservices).

## Diagram (Mermaid)

```mermaid
flowchart TB
    Client["Client"]:::external
    EnvConfig["Env Config"]:::external

    Client -->|"HTTP/JSON"| HTTPAPI["HTTP API Server"]:::runtime
    EnvConfig --> HTTPAPI

    HTTPAPI --> AuthMW["Auth & RBAC Middleware"]:::runtime
    AuthMW --> AuthH["AuthHandler"]:::runtime
    AuthMW --> ProductH["ProductHandler"]:::runtime

    AuthH --> Response["Response Utilities"]:::util
    ProductH --> Repo["ProductRepository"]:::runtime
    Repo --> Postgres["PostgreSQL"]:::external
    ProductH --> Response

    Response --> Client

    subgraph "Domain Models"
        UserM["User Model"]:::util
        ProductM["Product Model"]:::util
    end

    AuthH --> UserM
    ProductH --> ProductM
    Repo --> ProductM

    subgraph "CI/CD"
        CodeRepo["GitHub Repository"]:::external
        CI["CI/CD Pipeline"]:::external
    end

    CodeRepo --> CI

    subgraph "Dependency Management"
        GoiMod["go.mod"]:::util
        GoSum["go.sum"]:::util
    end

    GoiMod --> HTTPAPI
    GoSum --> HTTPAPI

    click HTTPAPI "https://github.com/ngtruong624/backendshop_go/blob/main/cmd/api/main.go"
    click AuthMW "https://github.com/ngtruong624/backendshop_go/blob/main/internal/middleware/auth.go"
    click AuthH "https://github.com/ngtruong624/backendshop_go/blob/main/internal/handlers/auth_handler.go"
    click ProductH "https://github.com/ngtruong624/backendshop_go/blob/main/internal/handlers/product_handler.go"
    click Repo "https://github.com/ngtruong624/backendshop_go/blob/main/internal/repository/product_repo.go"
    click UserM "https://github.com/ngtruong624/backendshop_go/blob/main/internal/models/user.go"
    click ProductM "https://github.com/ngtruong624/backendshop_go/blob/main/internal/models/product.go"
    click Response "https://github.com/ngtruong624/backendshop_go/blob/main/internal/utils/response.go"
    click CI "https://github.com/ngtruong624/backendshop_go/blob/main/.github/workflows/go.yml"
    click GoiMod "https://github.com/ngtruong624/backendshop_go/blob/main/go.mod"
    click GoSum "https://github.com/ngtruong624/backendshop_go/blob/main/go.sum"

    classDef runtime fill:#AED6F1,stroke:#2980B9,color:#1B2631
    classDef external fill:#A9DFBF,stroke:#27AE60,color:#1B2631
    classDef util fill:#F9E79F,stroke:#D4AC0D,color:#1B2631
```
