# bgbiao/gin-jwt-token architecture

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

- Interactive diagram: https://gitdiagram.com/bgbiao/gin-jwt-token
- Repository: https://github.com/bgbiao/gin-jwt-token

## Overview

##### Project type and purpose
- This is a Go-based RESTful API service built with the Gin framework.
- It exposes user-registration and login endpoints, issues JWT tokens, and protects “/auth/” routes behind JWT middleware.
- It persists user data in a MySQL database (via GORM).

##### File structure
- main.go: initializes Gin, loads config, registers routes and middleware, and starts HTTP server on :8081.
- api/config.go (+ test): loads `config/config.ini` (DB host, port, credentials).
- controller/user-token.go: handlers for /register, /login, and /auth/time.
- middleware/jwt.go: Gin middleware to validate and parse JWT on protected routes.
- model/user.go & utils.go (+ tests): defines User model and utility functions for DB operations (create user, authenticate).
- config/config.ini: externalizes DB config, enabling environment-specific overrides.
- Docker command in README spins up MySQL on 13306 to back the service.

##### README for additional insights
- Describes how to start MySQL in Docker, configure the `config.ini`, then run `go run main.go`.
- Lists three HTTP endpoints: POST /apis/v1/register, POST /apis/v1/login, GET /apis/v1/auth/time.
- Shows curl examples illustrating token issuance and protected-route access flow.

##### Main components and relationships
a. Client (Postman/curl/browsers)
- Sends HTTP requests to the API server.
b. API Server (Gin web engine)
- Config Loader (api/config) reads `config.ini`.
- Router:
- Public route group `/apis/v1`:
– POST `/register` → Controller.RegisterUser
– POST `/login` → Controller.Login (issues JWT token)
- Protected route group `/apis/v1/auth`:
– JWT Middleware (middleware/jwt.go) → Controller.GetDataByTime
- Controllers call into Model layer.
c. Model/Data Access Layer (model/*.go)
- Maps `User` struct to MySQL via GORM.
- Provides user-creation and authentication queries.
d. MySQL Database
- Running in Docker container on 127.0.0.1:13306.
- Stores users, credentials, any other persistent data.
e. JWT Token Service (jwt-go library)
- Used by Controller.Login to sign tokens, and by middleware to verify them.

##### Architectural patterns and design principles
- MVC-like separation: Controllers (HTTP handlers) vs. Models (DB operations).
- Layered architecture:
Config → Router & Middleware → Controller → Model → Database.
- Stateless authentication using JWT.
- Externalized configuration for environment flexibility.
- Dependency isolation:
- Gin for HTTP
- GORM for ORM
- jwt-go for token handling
- MySQL for persistence

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    Client["Client\n(Postman/curl)"]:::client

    %% Configuration
    subgraph "Configuration"
        ConfigLoader["Config Loader"]:::config
        ConfigTest["Config Tests"]:::config
        Ini["config.ini"]:::configFile
    end

    %% API Server
    subgraph "API Server"
        Main["main.go"]:::main
        Router["Gin Router\n/apis/v1"]:::http
        Controllers["User Controller"]:::http
        subgraph "Security Layer"
            JWTMW["JWT Middleware"]:::security
            JWTService["jwt-go Service"]:::security
        end
    end

    %% Model Layer
    subgraph "Model Layer"
        UserModel["User Model"]:::data
        Utils["Utils"]:::data
        UtilsTest["Utils Tests"]:::data
    end

    %% Database
    MySQL["MySQL DB\n(Docker:127.0.0.1:13306)"]:::database

    %% Project Files
    subgraph "Project Files"
        GoMod["go.mod / go.sum"]:::docs
        Readme["README.md"]:::docs
        Makefile["Makefile"]:::docs
    end

    %% Connections
    Client -->|HTTP/JSON:8081| Router
    Router -->|POST /apis/v1/register| Controllers
    Router -->|POST /apis/v1/login| Controllers
    Router -->|GET /apis/v1/auth/time| JWTMW
    JWTMW -->|validated| Controllers
    Controllers -->|GORM calls| Utils
    Utils -->|SQL over TCP:13306| MySQL
    UserModel -->|GORM Mapping| MySQL
    Controllers -->|Issue JWT| JWTService
    JWTService -->|Token| Controllers
    Controllers -->|HTTP/JSON response| Client

    Main -->|loads| ConfigLoader
    ConfigLoader -->|reads| Ini
    Main --> Router
    Main --> JWTMW
    Main --> Controllers
    Main --> Utils
    Main --> UserModel

    GoMod --- Main
    Readme --- Main
    Makefile --- Main

    %% Click Events
    click ConfigLoader "https://github.com/bgbiao/gin-jwt-token/blob/master/api/config.go"
    click ConfigTest "https://github.com/bgbiao/gin-jwt-token/blob/master/api/config_test.go"
    click Ini "https://github.com/bgbiao/gin-jwt-token/blob/master/config/config.ini"
    click Main "https://github.com/bgbiao/gin-jwt-token/blob/master/main.go"
    click Controllers "https://github.com/bgbiao/gin-jwt-token/blob/master/controller/user-token.go"
    click JWTMW "https://github.com/bgbiao/gin-jwt-token/blob/master/middleware/jwt.go"
    click UserModel "https://github.com/bgbiao/gin-jwt-token/blob/master/model/user.go"
    click Utils "https://github.com/bgbiao/gin-jwt-token/blob/master/model/utils.go"
    click UtilsTest "https://github.com/bgbiao/gin-jwt-token/blob/master/model/utils_test.go"
    click GoMod "https://github.com/bgbiao/gin-jwt-token/blob/master/go.mod"
    click GoMod "https://github.com/bgbiao/gin-jwt-token/blob/master/go.sum"
    click Readme "https://github.com/bgbiao/gin-jwt-token/blob/master/readme.md"
    click Makefile "https://github.com/bgbiao/gin-jwt-token/tree/master/Makefile"

    %% Styles
    classDef client fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef http fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef main fill:#87CEFA,stroke:#333,stroke-width:1px
    classDef security fill:#FFA500,stroke:#333,stroke-width:1px
    classDef data fill:#90EE90,stroke:#333,stroke-width:1px
    classDef database fill:#32CD32,stroke:#333,stroke-width:1px
    classDef config fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef configFile fill:#DCDCDC,stroke:#333,stroke-width:1px
    classDef docs fill:#E8E8E8,stroke:#333,stroke-width:1px
```
