# filiphasan/golang-minify-url architecture

> Architecture diagram of the GitHub repository filiphasan/golang-minify-url, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/filiphasan/golang-minify-url
- Repository: https://github.com/filiphasan/golang-minify-url

## Overview

##### Main Components
- API Entry Point and Server:
- API layer, seen in the "cmd/api" folder (main.go, Dockerfile). This is where the application starts, bootstraps configurations (using Viper from the configs folder), and sets up Gin as the HTTP router.
- Controllers & Routes:
- Located in "internal/app/controllers" and "internal/app/routes". These handlers manage HTTP requests (e.g., health check and URL shortening endpoints).
- Service Layer:
- Under "internal/app/services". These components (such as the shortener_service.go and token_service.go) encapsulate the business logic for generating shortened URLs and managing tokens.
- Caching:
- Found in "internal/app/caches", which includes an abstraction (cache.go) and a Redis implementation (redis.go). This layer improves performance by storing transient data.
- Persistence/Database:
- Managed in "internal/database" which deals with MongoDB connections and setups. This shows the project uses MongoDB for persistent storage.
- Jobs and Scheduling:
- In "internal/app/jobs", indicating the use of cron jobs (with gocron) for tasks such as token pool management and other periodic setups.
- Middleware & Logging:
- "internal/middlewares" provide capabilities for exception handling, logging of request/response, and passing correlation IDs. The logging itself is handled in "internal/logger".
- Dependency Injection:
- Google’s Wire is used (as shown in the README and presence of Wire files) to manage and inject dependencies across the various components.
- External Dependencies:
- Additional external services such as Redis and MongoDB, configured via "configs" and environment variables (e.g., .env.example) and deployed using Docker-compose (docker-compose.yml).

##### Relationships and Interactions
- Incoming HTTP requests first hit the API (Gin) layer via the controllers/routes.
- Requests are passed through middleware that handles logging, exception handling, and correlation.
- Controllers then call the service layer. The services interact with both:
- The persistence layer (MongoDB) for permanent data.
- The caching layer (Redis) for performance optimizations.
- Background jobs (scheduled via gocron) periodically execute tasks (like managing URL token pools) and use the same services.
- All components are configured using the configuration files from the "configs" directory.
- Dependency injection via Wire ensures that components such as the logger, database connectors, and cache clients are provided to the parts of the app that need them.

##### Architectural Patterns and Design Principles
- Layered Architecture: Separation between API/controllers, business logic (services), data persistence, caching, and background jobs.
- Dependency Injection: Use of Google Wire simplifies dependency management.
- Microservice-ready Modular Design: Although this project is a single service, its separation into clearly defined modules (controllers, services, repositories, middlewares) facilitates future scaling or decomposition into microservices.
- Use of external libraries (gin for routing, uber-zap for logging, viper for config management) indicates a modern Go development approach.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client("Client"):::client
    API_EP["API Entry Point"]:::api
    MW["Middlewares"]:::infra
    LOG["Logger"]:::infra
    CTRL["Controllers"]:::api
    ROUTE["Routes"]:::api
    SERV["Service Layer"]:::service
    CACHE["Caching Layer (Redis)"]:::service
    DB["Database Layer (MongoDB)"]:::service
    JOBS["Jobs"]:::service
    CONFIG["Configuration"]:::config
    ENV["Env Variables"]:::config
    DI["Dependency Injection (Wire)"]:::infra

    Client -->|"HTTP_Request"| API_EP
    API_EP -->|"initializes"| MW
    MW -->|"applies"| LOG
    LOG -->|"handles"| CTRL
    CTRL -->|"routesTo"| ROUTE
    ROUTE -->|"invokes"| SERV
    SERV -->|"cachesUsing"| CACHE
    SERV -->|"persistsVia"| DB
    JOBS -->|"triggers"| SERV
    CONFIG -->|"configures"| API_EP
    CONFIG -->|"configures"| SERV
    ENV -->|"supplies"| CONFIG
    DI -->|"injectsInto"| API_EP
    DI -->|"injectsInto"| SERV
    DI -->|"injectsInto"| JOBS

    click API_EP "https://github.com/filiphasan/golang-minify-url/tree/main/cmd/api"
    click CTRL "https://github.com/filiphasan/golang-minify-url/tree/main/internal/app/controllers"
    click ROUTE "https://github.com/filiphasan/golang-minify-url/tree/main/internal/app/routes"
    click SERV "https://github.com/filiphasan/golang-minify-url/tree/main/internal/app/services"
    click CACHE "https://github.com/filiphasan/golang-minify-url/tree/main/internal/app/caches"
    click DB "https://github.com/filiphasan/golang-minify-url/tree/main/internal/database"
    click JOBS "https://github.com/filiphasan/golang-minify-url/tree/main/internal/app/jobs"
    click MW "https://github.com/filiphasan/golang-minify-url/tree/main/internal/middlewares"
    click LOG "https://github.com/filiphasan/golang-minify-url/tree/main/internal/logger"
    click CONFIG "https://github.com/filiphasan/golang-minify-url/tree/main/configs"
    click ENV "https://github.com/filiphasan/golang-minify-url/blob/main/.env.example"
    click DI "https://github.com/filiphasan/golang-minify-url/tree/main/internal/setup"

    classDef api fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef service fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef infra fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef config fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef client fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
```
