# mceasy-id/go-service-boilerplate architecture

> Architecture diagram of the GitHub repository mceasy-id/go-service-boilerplate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mceasy-id/go-service-boilerplate
- Repository: https://github.com/mceasy-id/go-service-boilerplate

## Overview

##### Main Components

a. External Layer:
- Client Applications (Frontend/External Services)
- API Gateway/Load Balancer (implied by the infrastructure)

b. Application Core:
- HTTP Server Layer (Fiber framework)
- Middleware Components
- Domain Services
- Database Layer

##### Detailed Component Breakdown

a. API Layer (cmd/api):
- Middleware handlers (CORS, Guards, Internal)

b. Domain Structure (for each domain, e.g., Product, Identity):
1. Delivery Layer (HTTP Handlers)
2. UseCase Layer (Business Logic)
3. Repository Layer (Data Access)

c. Infrastructure Components:
- PostgreSQL Database
- Observability Stack (Metrics, Tracing)
- External Services (via HTTP Client)

##### Interaction Flows

- Request flow: Client → Middleware → Delivery → UseCase → Repository → Database
- Response flow: Database → Repository → UseCase → Delivery → Client
- Observability flow: Components → Metrics/Tracing collectors

##### Specific Implementation Details

Specialized components:
- Resource Management (pkg/resourceful)
- Error Handling (pkg/apperror)
- Configuration Management (config/)
- Database Migrations (database/migrations)

##### Additional Elements

Annotations for:
- Configuration flow
- Error handling paths
- Observability integration points
- Database transaction boundaries

##### Technical Details to Highlight

- DTOs for request/response handling
- Entity definitions
- Table definitions for resource management
- Mock implementations for testing
- Middleware chain
- Database transaction handling
- Observability instrumentation

##### Cross-cutting concerns (how observability, error handling, etc., span all layers)

Remember to maintain a balance between detail and clarity, focusing on the architectural decisions while avoiding implementation-specific details that might change frequently.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph External
        Client[Client Applications]
        Gateway[API Gateway/Load Balancer]
    end

    subgraph Server["HTTP Server Layer"]
        MainEntry["Main Entry Point"]:::entry
        ServerSetup["Server Setup"]:::server
        RequestHandler["Request Handler"]:::handler
    end

    subgraph Middleware["Middleware Layer"]
        CORS["CORS Handler"]:::middleware
        Guard["Guard Handler"]:::middleware
        Internal["Internal Handler"]:::middleware
    end

    subgraph Product["Product Domain"]
        direction TB
        ProductHTTP["HTTP Delivery"]:::delivery
        ProductUseCase["Use Cases"]:::usecase
        ProductRepo["Repository"]:::repo
        ProductEntities["Entities"]:::entity
        ProductDTOs["DTOs"]:::dto
    end

    subgraph Identity["Identity Domain"]
        direction TB
        IdentityHTTP["HTTP Repository"]:::repo
        IdentityEntities["Entities"]:::entity
        IdentityAuth["Authentication"]:::auth
    end

    subgraph Infrastructure["Infrastructure Layer"]
        direction TB
        DB[(PostgreSQL)]:::database
        DBTx["Database Transactions"]:::infra
        DBMigrations["Migrations"]:::infra
        Config["Configuration"]:::config
    end

    subgraph CrossCutting["Cross-cutting Concerns"]
        direction TB
        ErrorHandler["Error Handling"]:::cross
        ResourceMgmt["Resource Management"]:::cross
        HTTPClient["HTTP Client"]:::cross
        Logger["Logger"]:::cross
    end

    subgraph Observability["Observability Stack"]
        direction TB
        Metrics["Metrics"]:::obs
        Tracing["Tracing"]:::obs
        Instrument["Instrumentation"]:::obs
        Notify["Notifications"]:::obs
    end

    %% Connections
    Client --> Gateway
    Gateway --> MainEntry
    MainEntry --> ServerSetup
    ServerSetup --> RequestHandler
    RequestHandler --> |Routes| CORS
    CORS --> Guard
    Guard --> Internal
    Internal --> ProductHTTP
    Internal --> IdentityHTTP
    
    ProductHTTP --> ProductUseCase
    ProductUseCase --> ProductRepo
    ProductRepo --> DB
    ProductEntities --> ProductUseCase
    ProductDTOs --> ProductHTTP

    IdentityHTTP --> IdentityEntities
    IdentityAuth --> IdentityHTTP

    %% Cross-cutting connections
    ErrorHandler -.-> ProductUseCase
    ResourceMgmt -.-> ProductRepo
    HTTPClient -.-> IdentityHTTP
    Logger -.-> ServerSetup
    
    %% Observability connections
    Metrics -.-> ServerSetup
    Tracing -.-> ProductUseCase
    Instrument -.-> ProductRepo
    Notify -.-> ErrorHandler

    %% Click events
    click MainEntry "https://github.com/mceasy-id/go-service-boilerplate/blob/master/cmd/api/main.go"
    click ServerSetup "https://github.com/mceasy-id/go-service-boilerplate/blob/master/internal/server/server.go"
    click RequestHandler "https://github.com/mceasy-id/go-service-boilerplate/blob/master/internal/server/handler.go"
    click CORS "https://github.com/mceasy-id/go-service-boilerplate/blob/master/internal/middleware/cors.go"
    click Guard "https://github.com/mceasy-id/go-service-boilerplate/blob/master/internal/middleware/guard.go"
    click Internal "https://github.com/mceasy-id/go-service-boilerplate/blob/master/internal/middleware/internal.go"
    click ProductHTTP "https://github.com/mceasy-id/go-service-boilerplate/tree/master/internal/product/delivery/http/external/v1"
    click ProductUseCase "https://github.com/mceasy-id/go-service-boilerplate/blob/master/internal/product/usecase/usecase.go"
    click ProductRepo "https://github.com/mceasy-id/go-service-boilerplate/blob/master/internal/product/repository/product.go"
    click ProductEntities "https://github.com/mceasy-id/go-service-boilerplate/blob/master/internal/product/entities/core.go"
    click ProductDTOs "https://github.com/mceasy-id/go-service-boilerplate/tree/master/internal/product/dtos"
    click IdentityHTTP "https://github.com/mceasy-id/go-service-boilerplate/tree/master/internal/identity/httprepository"
    click IdentityEntities "https://github.com/mceasy-id/go-service-boilerplate/tree/master/internal/identity/identityentities"
    click IdentityAuth "https://github.com/mceasy-id/go-service-boilerplate/blob/master/internal/identity/identityentities/auth.go"
    click DBTx "https://github.com/mceasy-id/go-service-boilerplate/blob/master/pkg/database/tx.go"
    click DBMigrations "https://github.com/mceasy-id/go-service-boilerplate/tree/master/database/migrations"
    click Config "https://github.com/mceasy-id/go-service-boilerplate/blob/master/config/config.go"
    click ErrorHandler "https://github.com/mceasy-id/go-service-boilerplate/blob/master/pkg/apperror/error.go"
    click ResourceMgmt "https://github.com/mceasy-id/go-service-boilerplate/blob/master/pkg/resourceful/resource.go"
    click HTTPClient "https://github.com/mceasy-id/go-service-boilerplate/blob/master/pkg/httpclient/init.go"
    click Logger "https://github.com/mceasy-id/go-service-boilerplate/blob/master/pkg/logger/logger.go"
    click Metrics "https://github.com/mceasy-id/go-service-boilerplate/blob/master/pkg/observability/metric.go"
    click Tracing "https://github.com/mceasy-id/go-service-boilerplate/blob/master/pkg/observability/tracer.go"
    click Instrument "https://github.com/mceasy-id/go-service-boilerplate/blob/master/pkg/observability/instrumentation/trace.go"
    click Notify "https://github.com/mceasy-id/go-service-boilerplate/blob/master/pkg/observability/notification.go"

    %% Styles
    classDef entry fill:#2196F3,stroke:#1565C0,color:white
    classDef server fill:#42A5F5,stroke:#1E88E5,color:white
    classDef handler fill:#64B5F6,stroke:#42A5F5,color:white
    classDef middleware fill:#90CAF9,stroke:#64B5F6,color:black
    classDef delivery fill:#4CAF50,stroke:#388E3C,color:white
    classDef usecase fill:#66BB6A,stroke:#4CAF50,color:white
    classDef repo fill:#81C784,stroke:#66BB6A,color:white
    classDef entity fill:#A5D6A7,stroke:#81C784,color:black
    classDef dto fill:#C8E6C9,stroke:#A5D6A7,color:black
    classDef database fill:#FDD835,stroke:#FBC02D,color:black
    classDef infra fill:#FFEE58,stroke:#FDD835,color:black
    classDef config fill:#FFF59D,stroke:#FFF176,color:black
    classDef cross fill:#9575CD,stroke:#7E57C2,color:white
    classDef obs fill:#B39DDB,stroke:#9575CD,color:black
    classDef auth fill:#F06292,stroke:#EC407A,color:white
```
