# kondohiroki/go-boilerplate architecture

> Architecture diagram of the GitHub repository kondohiroki/go-boilerplate, 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/kondohiroki/go-boilerplate
- Repository: https://github.com/kondohiroki/go-boilerplate

## Overview

##### Main Components
- Application Entry Points:
- CLI Commands: Located under the cmd directory. For example, cmd/serve_api.go for running the HTTP API, cmd/migrate.go for database migrations, and additional commands like make.go, queue.go, schedule.go, and version.go.
- Scheduler: The scheduled jobs reside under internal/scheduler/scheduler.go and internal/job for job definitions and handlers.
- Configuration:
- Config files are managed in the config directory (with YAML configurations) and loaded in cmd/root.go.
- HTTP & gRPC Interfaces:
- HTTP: Found in internal/interface/http, which includes routes for health checks, user endpoints, queue management, and miscellaneous endpoints.
- (Planned) gRPC: There is a placeholder directory under internal/interface/grpc for future gRPC API services.
- Business Logic:
- Located under internal/app (for example, internal/app/queue and internal/app/user), encapsulating domain logic for different services.
- Data Persistence:
- Database Migrations and Models: Found in internal/db/migrations and internal/db/model directories. The project uses PostgreSQL (configured via docker-compose and supported by both pgx and rdb implementations).
- Repositories and Helpers:
- Repository Layer: Found under internal/repository. This abstracts data access for job and user entities.
- Helper Modules: Multiple helpers exist (cache, pagination, queue utils, etc.) providing auxiliary functionalities.
- Logging:
- Logging is handled in internal/logger with settings encapsulated in zap_logger.go and additional logger middleware within internal/router/middleware.
- External Dependencies:
- Docker and docker-compose files indicate containerization.
- Tests are clearly separated in the test directory.

##### Relationships and Interactions
- The CLI commands (in cmd) serve as the entry point to load configuration, invoke business logic, and run tasks such as migrations or starting the HTTP server.
- When running as a web server, the application initializes the Fiber-based HTTP router (inside internal/router) which ties HTTP requests to corresponding handlers in internal/interface/http.
- HTTP endpoints interact with the business logic (internal/app) which then communicates with the repository layer (internal/repository) to perform database operations.
- The database layer is composed of migration scripts, models, and data access implementations, with PostgreSQL as the main database.
- Caching operations are modularized under internal/helper/cache, likely interfacing with Redis (as indicated by README that mentions “Redis Cache” and “Redis Reliable Queue”).
- The Scheduler interacts with both the job processing components (internal/job) and potential queue systems (internal/app/queue) to execute periodic tasks.
- Logging, error handling, and central responses are handled by dedicated modules (internal/logger, internal/interface/response, and pkg/exception) to enforce consistency across components.

##### Architectural Patterns and Principles
- Layered Architecture: The project is structured in layers – entry point (cmd), application logic (internal/app), interface (internal/interface), domain and infrastructure (internal/db, internal/repository, internal/helper).
- Modular Design: Clear separation between individual components (configuration, routing, logging, error handling) enables easier maintenance and extension.
- Extensibility: The boilerplate is designed to support adding more endpoints or background jobs easily, as shown by placeholders and organized directories dedicated to HTTP, CLI, gRPC, and job scheduling.
- Infrastructure as Code: Dockerfile and docker-compose files outline deployment and environmental setup, making it easier to containerize and deploy the system reliably.

##### Relevant Technologies & Frameworks
- Go Programming Language (minimum Go 1.20)
- Fiber Router for HTTP handling
- Zap Logger for logging
- Docker for containerization
- PostgreSQL for data persistence (with migration files and connection setups)
- Redis for caching and potentially reliable queuing
- Cobra for CLI interactions

##### Specific Elements
- Clearly labeled components for:
- CLI commands (cmd)
- HTTP API interface (internal/interface/http)
- (Future) gRPC interface (internal/interface/grpc)
- Business logic layer (internal/app)
- Database access (internal/db, internal/repository)
- Scheduler and Job processing (internal/scheduler, internal/job)
- Configuration module (config)
- Logging (internal/logger)
- Helper modules (internal/helper)
- Data flow from the CLI or HTTP entry points to business logic components.
- How the application communicates with the database and caching systems.
- Demonstrate the interaction between scheduled jobs and their execution handlers.
- Annotations:

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Entry Points"
        CLI["CLI Commands"]:::entry
        HTTP["HTTP API Interface"]:::entry
        GRPC["gRPC Interface (Planned)"]:::entry
    end

    subgraph "Core Modules"
        CONFIG["Configuration Module"]:::core
        BUS["Business Logic Layer"]:::core
        REPO["Repository Layer"]:::core
        DB["Data Persistence (Migrations & Models)"]:::core
    end

    subgraph "Background Processing"
        SCHED["Scheduler & Job Processing"]:::background
    end

    subgraph "Supporting Services"
        HELPER["Helper Modules"]:::support
        LOG["Logging & Middleware"]:::support
        UTIL["Utilities (CLI/Transport)"]:::support
    end

    subgraph "Deployment & External"
        DEPLOY["Containerization & Deployment"]:::external
    end

    %% Connections from Entry Points to Core
    CLI -->|"loadConfig"| CONFIG
    HTTP -->|"routes"| BUS
    GRPC -->|"routes"| BUS

    %% Connection from Configuration to Business Logic
    CONFIG -->|"configures"| BUS

    %% Business Logic interactions
    BUS -->|"calls"| REPO
    REPO -->|"queries"| DB
    BUS -->|"schedules"| SCHED

    %% Supporting Services interactions
    BUS ---|"supports"| HELPER
    HTTP ---|"logs via"| LOG
    CLI ---|"utilizes"| UTIL

    %% Optional arrow to Deployment (infrastructure)
    DEPLOY ---|"containers"| CLI
    DEPLOY ---|"containers"| HTTP
    DEPLOY ---|"containers"| GRPC
    DEPLOY ---|"containers"| SCHED

    %% Click Events
    click CLI "https://github.com/kondohiroki/go-boilerplate/tree/main/cmd"
    click HTTP "https://github.com/kondohiroki/go-boilerplate/tree/main/internal/interface/http"
    click GRPC "https://github.com/kondohiroki/go-boilerplate/tree/main/internal/interface/grpc"
    click CONFIG "https://github.com/kondohiroki/go-boilerplate/tree/main/config"
    click BUS "https://github.com/kondohiroki/go-boilerplate/tree/main/internal/app"
    click REPO "https://github.com/kondohiroki/go-boilerplate/tree/main/internal/repository"
    click DB "https://github.com/kondohiroki/go-boilerplate/tree/main/internal/db"
    click SCHED "https://github.com/kondohiroki/go-boilerplate/blob/main/internal/scheduler/scheduler.go"
    click HELPER "https://github.com/kondohiroki/go-boilerplate/tree/main/internal/helper"
    click LOG "https://github.com/kondohiroki/go-boilerplate/tree/main/internal/logger"
    click UTIL "https://github.com/kondohiroki/go-boilerplate/tree/main/pkg"
    click DEPLOY "https://github.com/kondohiroki/go-boilerplate/tree/main/Dockerfile"

    %% Styles
    classDef entry fill:#A2D5F2,stroke:#1B6CA8,stroke-width:2px;
    classDef core fill:#E1F5C4,stroke:#68A357,stroke-width:2px;
    classDef background fill:#F9E79F,stroke:#D4AC0D,stroke-width:2px;
    classDef support fill:#FAD7A0,stroke:#E67E22,stroke-width:2px;
    classDef external fill:#D7BDE2,stroke:#8E44AD,stroke-width:2px;
```
