# yonger0718/go-template architecture

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

- Interactive diagram: https://gitdiagram.com/yonger0718/go-template
- Repository: https://github.com/yonger0718/go-template

## Overview

##### Project Type and Purpose
- This is a Go “template” or boilerplate for a RESTful API server.
- It provides HTTP endpoints (via Swagger docs), user authentication (JWT), data persistence (database), and is wired together with Google Wire for dependency injection.
- It’s intended as a starting point for a typical layered, full-stack back-end service.

##### File Structure
- **cmd/go-template** – the entrypoint containing `main.go` (app bootstrap) and Wire setup.
- **internal/server** – HTTP server instantiation.
- **internal/api/handlers** – route definitions, request/response handling, error handling.
- **internal/services** – business logic (e.g., user service).
- **internal/repository** – data access layer (e.g., user repository).
- **internal/utils** – cross-cutting helpers: database connection, JWT generation/validation, logging.
- **internal/middleware** – HTTP middleware, e.g., auth.
- **internal/configs** – configuration loader (YAML).
- **assets/swagger** – OpenAPI specs.
- **deploy/docker-compose.yml** – service composition (e.g., database container).
- **migration** – DB migration runner.

##### Main Components & Layers
1. **Presentation Layer**
– HTTP Server (listening on port)
– Middleware (auth, logging)
– API Handlers / Routes (user, exception, response)
2. **Application / Business Layer**
– Services (user service interface & implementation)
3. **Data Access Layer**
– Repositories (user repository)
4. **Infrastructure & Utilities**
– Database connector (GORM or raw SQL)
– JWT utility
– Logger
– Configuration loader
– Dependency Injection (Wire)
5. **External Systems**
– PostgreSQL (or other RDBMS)
– Redis / external caches, if any (not present here)

##### Architectural Patterns & Design Principles
- **Layered Architecture** – strict separation of concerns.
- **Dependency Injection** – using Wire to assemble handlers, services, repos.
- **Repository Pattern** – abstract persistence behind interfaces.
- **Middleware Pipeline** – for cross-cutting concerns (authentication, logging, error handling).
- **Configuration-first** – load configs once at startup and inject.

- A note on the side showing your deployment stack (Docker-Compose).
- Optionally: indicate test boundaries (e.g., `internal/utils/logger_test.go`).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entrypoint & Dependency Injection
    subgraph "Entrypoint & DI"
        CMD_MAIN["main.go"]:::infra
        CMD_WIRE["wire.go"]:::infra
        CMD_GEN["wire_gen.go"]:::infra
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        SERVER["HTTP Server\n(internal/server/server.go)"]:::presentation
        MIDDLEWARE["Auth & Logging\n(internal/middleware/auth.go)"]:::presentation
        EXCEPTION["Error Handler\n(internal/api/handlers/exception/errors.go)"]:::presentation
        RESPONSE["Response Handler\n(internal/api/handlers/response/response.go)"]:::presentation
        ROUTES["User Routes\n(internal/api/handlers/routes/user.go)"]:::presentation
        USER_HANDLER["User Handler\n(internal/api/handlers/user/user.go)"]:::presentation
    end

    %% Business Layer
    subgraph "Business Layer"
        SERVICE_IF["User Service Interface\n(internal/services/user/user.go)"]:::business
        SERVICE_IMPL["User Service Default\n(internal/services/user/user_default.go)"]:::business
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        REPO["User Repository\n(internal/repository/user.go)"]:::data
        MODEL["User Model\n(internal/models/user.go)"]:::data
    end

    %% Infrastructure & Utilities
    subgraph "Infrastructure & Utilities"
        CONFIG["Config Loader\n(internal/configs/config.go\ninternal/configs/config.yaml)"]:::infra
        DB["Database Connector\n(internal/utils/database/database.go)"]:::infra
        JWT["JWT Utility\n(internal/utils/jwt/jwt.go)"]:::infra
        LOGGER["Logger\n(internal/utils/logger/logger.go\ninternal/utils/logger/logger_test.go)"]:::infra
        VALIDATORS["Validators\n(internal/validators/common.go\ninternal/validators/user.go)"]:::infra
        CONSTANTS["Constants\n(internal/constants/constants.go)"]:::infra
    end

    %% Swagger & Migration & Deployment
    subgraph "Others"
        SWAGGER["Swagger Specs\n(assets/swagger/swagger.yaml\nassets/swagger/swagger.json\nassets/swagger/docs.go)"]:::infra
        MIGRATE["DB Migration\n(migration/migrate.go)"]:::infra
        DOCKER["Docker Compose\n(deploy/docker-compose.yml)"]:::infra
    end

    %% External Systems
    subgraph "External Systems"
        POSTGRES["PostgreSQL"]:::external
    end

    %% Connections
    CMD_MAIN -->|"initializes"| CMD_WIRE
    CMD_WIRE -->|"generates"| CMD_GEN
    CMD_GEN -->|"bootstraps"| SERVER

    SERVER -->|"uses"| MIDDLEWARE
    MIDDLEWARE -->|"handles"| ROUTES
    ROUTES -->|"routes to"| USER_HANDLER
    USER_HANDLER -->|"calls"| SERVICE_IF
    SERVICE_IF -->|"implemented by"| SERVICE_IMPL
    SERVICE_IMPL -->|"uses"| REPO
    REPO -->|"maps to"| MODEL
    REPO -->|"reads/writes"| DB
    SERVICE_IMPL -->|"validates via"| VALIDATORS
    SERVER -->|"loads config"| CONFIG
    SERVER -->|"logs via"| LOGGER
    USER_HANDLER -->|"issues JWT"| JWT
    CMD_MAIN -->|"runs migration"| MIGRATE
    SWAGGER -->|"provides specs for"| SERVER
    DOCKER -->|"hosts"| POSTGRES
    DB -->|"connects to"| POSTGRES

    %% Click Events
    click CMD_MAIN "https://github.com/yonger0718/go-template/blob/main/cmd/go-template/main.go"
    click CMD_WIRE "https://github.com/yonger0718/go-template/blob/main/cmd/go-template/wire.go"
    click CMD_GEN "https://github.com/yonger0718/go-template/blob/main/cmd/go-template/wire_gen.go"
    click SERVER "https://github.com/yonger0718/go-template/blob/main/internal/server/server.go"
    click MIDDLEWARE "https://github.com/yonger0718/go-template/blob/main/internal/middleware/auth.go"
    click EXCEPTION "https://github.com/yonger0718/go-template/blob/main/internal/api/handlers/exception/errors.go"
    click RESPONSE "https://github.com/yonger0718/go-template/blob/main/internal/api/handlers/response/response.go"
    click ROUTES "https://github.com/yonger0718/go-template/blob/main/internal/api/handlers/routes/user.go"
    click USER_HANDLER "https://github.com/yonger0718/go-template/blob/main/internal/api/handlers/user/user.go"
    click SERVICE_IF "https://github.com/yonger0718/go-template/blob/main/internal/services/user/user.go"
    click SERVICE_IMPL "https://github.com/yonger0718/go-template/blob/main/internal/services/user/user_default.go"
    click REPO "https://github.com/yonger0718/go-template/blob/main/internal/repository/user.go"
    click MODEL "https://github.com/yonger0718/go-template/blob/main/internal/models/user.go"
    click CONFIG "https://github.com/yonger0718/go-template/blob/main/internal/configs/config.go"
    click VALIDATORS "https://github.com/yonger0718/go-template/blob/main/internal/validators/common.go"
    click JWT "https://github.com/yonger0718/go-template/blob/main/internal/utils/jwt/jwt.go"
    click DB "https://github.com/yonger0718/go-template/blob/main/internal/utils/database/database.go"
    click LOGGER "https://github.com/yonger0718/go-template/blob/main/internal/utils/logger/logger.go"
    click SWAGGER "https://github.com/yonger0718/go-template/blob/main/assets/swagger/swagger.yaml"
    click MIGRATE "https://github.com/yonger0718/go-template/blob/main/migration/migrate.go"
    click DOCKER "https://github.com/yonger0718/go-template/blob/main/deploy/docker-compose.yml"

    %% Styles
    classDef presentation fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef business fill:#90EE90,stroke:#333,stroke-width:1px
    classDef data fill:#FFD580,stroke:#333,stroke-width:1px
    classDef infra fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef external fill:#D8BFD8,stroke:#333,stroke-width:1px
```
