# nsaltun/todolist-service architecture

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

- Interactive diagram: https://gitdiagram.com/nsaltun/todolist-service
- Repository: https://github.com/nsaltun/todolist-service

## Overview

##### Project Type and Purpose
- This is a microservice dedicated to managing a TODOLIST, built using Golang.
- It follows a Hexagonal architecture style, ensuring that core domain logic remains decoupled from the infrastructure.
- The README indicates a focus on features such as user CRUD operations, health & metrics endpoints, and integration with observability tools like Prometheus and Grafana. It also leverages the Fiber framework for HTTP handling, Zap for logging, and OpenTelemetry.

##### Analyzing the File Structure
- Entry Point: The “cmd/main.go” file is where the application starts, initializing configuration and setting up servers.
- Application Layer: The “app” directory contains subdirectories for specific functionality:
- “app/healthcheck” for health endpoints.
- “app/todoitem” for handlers that manage TODOLIST item creation and retrieval.
- Domain Layer: The “domain” directory holds core business models (e.g., “todo.go” and “filter.go”) representing the central logic.
- Infrastructure Layer:
- The “infra” directory (particularly “infra/postgres”) manages Postgresql connectivity and the concrete implementation of repositories.
- Middleware: Contains “middleware/error_middleware.go” and “middleware/logging_middleware.go” to handle error logging and request monitoring.
- Reusable Packages: Under “pkg”, you have generic HTTP handler logic, logging, and pagination which support the service’s operations.
- Deployment and Observability:
- The “deploy” directory includes configuration for Grafana (dashboards and datasources) and Prometheus, highlighting focus on monitoring.
- Additional Configuration & DB:
- The “config” directory offers centralized application configuration.
- The “migrations” folder holds the SQL migration scripts for evolving the database schema.

##### Main Components & Their Relationships
- Client Layer: Any external client (or internal user) sending HTTP requests to the service endpoints.
- API/Adapter Layer (HTTP Handlers): Represented by the “app/todoitem” and “app/healthcheck” packages. This layer leverages the generic HTTP handler logic from “pkg/httphandler/fiber_handler.go” to map incoming requests to the service's business logic.
- Domain Core: The “domain” directory encapsulates the business models and rules. This is the core of your Hexagonal architecture.
- Infrastructure / Adapters:
- Database: “infra/postgres” shows the database connection management and repository implementation, interfacing directly with your Postgresql DB.
- External Adapters: Deployment scripts and resources for Grafana and Prometheus link the internal metrics and logs to these external monitoring systems.
- Middleware: Ensures cross-cutting concerns (logging, error handling) are applied to requests throughout the system.
- Configuration & Logging: Centralized configuration (via “config/config.go”) and logging mechanisms (via “pkg/logging”) are incorporated throughout the service.
- Deployment/DevOps: Docker, Docker-compose, and deployment configs that ensure the service runs in containerized environments.

##### Architectural Principles
- Hexagonal Architecture: Separate core (Domain) from the outer layers (HTTP handlers, database, and other external systems). Emphasize clear boundaries with ports (interfaces) and adapters (implementations).
- Port-Adaptor Design: Layer the application to isolate the business logic from both the web framework (Fiber) and the infrastructure details (Postgresql, Prometheus, Grafana).
- Observability: Integration with Grafana and Prometheus ensures health and metrics endpoints are properly monitored.
- Extensibility & Modularity: Use of middleware and generic HTTP handlers supports easy extension to other protocols or frameworks if needed.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    Client("Client"):::client

    %% Application Startup
    subgraph "Application Startup"
        Main("Entry Point"):::appStartup
    end

    %% API/HTTP Adapter (Fiber)
    subgraph "API/HTTP Adapter (Fiber)"
        FiberHandler("Fiber Handler"):::api
        CreateTodo("Create Todo Handler"):::api
        GetTodo("Get Todo Handler"):::api
        RepositoryInterface("Repository Interface"):::api
        Healthcheck("Healthcheck Handler"):::api
    end

    %% Middleware
    subgraph "Middleware"
        ErrorMiddleware("Error Middleware"):::middleware
        LoggingMiddleware("Logging Middleware"):::middleware
    end

    %% Business Logic (Domain)
    subgraph "Business Logic (Domain)"
        TodoModel("Todo Model"):::domain
        FilterModel("Filter Model"):::domain
    end

    %% Infrastructure Adapter
    subgraph "Infrastructure Adapter"
        DBConnection("DB Connection"):::infra
        TodoRepoImpl("Todo Repository Implementation"):::infra
        Database("Database (Postgresql)"):::database
    end

    %% Configuration & Utilities
    subgraph "Configuration & Utilities"
        Config("Configuration"):::config
        LoggingUtil("Logging Utility"):::config
        PaginationUtil("Pagination Utility"):::config
    end

    %% Observability & Monitoring
    subgraph "Observability & Monitoring"
        Grafana("Grafana Config"):::observability
        Prometheus("Prometheus Config"):::observability
    end

    %% Deployment & Database Migrations
    subgraph "Deployment & Database Migrations"
        DockerCompose("Docker Compose"):::deployment
        Migrations("Migrations"):::deployment
    end

    %% Connections - Main Request Flow
    Client -->|"sendsRequest"| Main
    Main -->|"initializes"| FiberHandler
    FiberHandler -->|"routesTo"| CreateTodo
    FiberHandler -->|"routesTo"| GetTodo
    FiberHandler -->|"routesTo"| Healthcheck
    CreateTodo -->|"callsPort"| RepositoryInterface
    GetTodo -->|"callsPort"| RepositoryInterface
    RepositoryInterface -->|"invokes"| TodoModel
    Healthcheck -->|"invokes"| TodoModel
    TodoModel -->|"usesImpl"| TodoRepoImpl
    TodoRepoImpl -->|"connectsVia"| DBConnection
    DBConnection -->|"accesses"| Database

    %% Connections - Supporting Components
    Main -->|"loads"| Config
    FiberHandler -->|"uses"| LoggingUtil
    LoggingUtil -->|"feeds"| LoggingMiddleware
    TodoModel -->|"applies"| PaginationUtil

    %% Observability Integration
    LoggingMiddleware -->|"sendsMetrics"| Prometheus
    TodoModel -->|"emitsLogs"| Grafana

    %% Deployment Integration
    DockerCompose -->|"deploys"| Database
    Migrations -->|"updates"| Database

    %% Styles
    classDef client fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef appStartup fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef api fill:#a3ffcc,stroke:#333,stroke-width:2px;
    classDef middleware fill:#ffccaa,stroke:#333,stroke-width:2px;
    classDef domain fill:#d6ffb3,stroke:#333,stroke-width:2px;
    classDef infra fill:#ffd1dc,stroke:#333,stroke-width:2px;
    classDef database fill:#b3d9ff,stroke:#333,stroke-width:2px;
    classDef config fill:#e6ccff,stroke:#333,stroke-width:2px;
    classDef observability fill:#ffe6b3,stroke:#333,stroke-width:2px;
    classDef deployment fill:#cce6ff,stroke:#333,stroke-width:2px;

    %% Click Events
    click Main "https://github.com/nsaltun/todolist-service/blob/main/cmd/main.go"
    click CreateTodo "https://github.com/nsaltun/todolist-service/blob/main/app/todoitem/create_todo_item_handler.go"
    click GetTodo "https://github.com/nsaltun/todolist-service/blob/main/app/todoitem/get_todo_items_handler.go"
    click RepositoryInterface "https://github.com/nsaltun/todolist-service/blob/main/app/todoitem/repository.go"
    click Healthcheck "https://github.com/nsaltun/todolist-service/blob/main/app/healthcheck/health.go"
    click FiberHandler "https://github.com/nsaltun/todolist-service/blob/main/pkg/httphandler/fiber_handler.go"
    click TodoModel "https://github.com/nsaltun/todolist-service/blob/main/domain/todo.go"
    click FilterModel "https://github.com/nsaltun/todolist-service/blob/main/domain/filter.go"
    click DBConnection "https://github.com/nsaltun/todolist-service/blob/main/infra/postgres/connection.go"
    click TodoRepoImpl "https://github.com/nsaltun/todolist-service/blob/main/infra/postgres/todo_repository.go"
    click ErrorMiddleware "https://github.com/nsaltun/todolist-service/blob/main/middleware/error_middleware.go"
    click LoggingMiddleware "https://github.com/nsaltun/todolist-service/blob/main/middleware/logging_middleware.go"
    click Config "https://github.com/nsaltun/todolist-service/blob/main/config/config.go"
    click LoggingUtil "https://github.com/nsaltun/todolist-service/blob/main/pkg/logging/log.go"
    click PaginationUtil "https://github.com/nsaltun/todolist-service/blob/main/pkg/pagination/pagination.go"
    click Grafana "https://github.com/nsaltun/todolist-service/tree/main/deploy/grafana"
    click Prometheus "https://github.com/nsaltun/todolist-service/blob/main/deploy/prometheus/prometheus.yml"
    click DockerCompose "https://github.com/nsaltun/todolist-service/blob/main/docker-compose.yml"
    click Migrations "https://github.com/nsaltun/todolist-service/tree/main/migrations"
```
