# vanh01/api-rate-limiting architecture

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

- Interactive diagram: https://gitdiagram.com/vanh01/api-rate-limiting
- Repository: https://github.com/vanh01/api-rate-limiting

## Overview

##### Project Type and Purpose
- This is a Go (Golang) project designed to implement two distinct problems:
– API Rate Limiting: A system that implements multiple strategies (Fixed Window, Sliding Window, Token Bucket, and even a Leaky Bucket appears to be present) to limit the number of requests per user, similar to rate limiting in major APIs.
– Graph Shortest Path: Solving the “Tìm Đường Trong Đồ Thị Lớn” problem, which involves finding the shortest path in a weighted, undirected graph with real-time updating and algorithm optimization.

##### File Structure
- Top-level directories and files:
- The root contains configuration files (config.yaml and config folder), Docker-related files (.dockerignore, Dockerfile, docker-compose.yml), makefile, and Prometheus configuration (prometheus.yml) for monitoring.
- cmd/main.go is the application entry point.
- The internal directory is organized into subpackages:
▪ app – likely for starting the application and bootstrapping.
▪ controller – handling routes (see internal/controller/router.go).
▪ middleware – containing implementation of rate limiting algorithms (fixedwindow.go, slidingwindow.go, tokenbucket.go, leakybucket.go).
▪ model – domain models (for example, user.go).
▪ repo – data access/repository layer (user.go for data persistence or mock repositories).
▪ usecase – contains business logic and interfaces.
- The pkg folder has a cache module with redis implementation, suggesting caching/external resource integration.
- This structure implies an architecture that could follow an MVC-like or hexagonal/Clean Architecture approach, with clear separation between controllers, application logic, data layers, and external services.

## Diagram (Mermaid)

```mermaid
flowchart TD
    User("User/Client"):::external
    apiGateway("API Gateway/Entry Point"):::core
    configYaml("config.yaml"):::config
    configGo("config/config.go"):::config
    monitoring("Monitoring (Prometheus)"):::external
    apiController("Controllers/API Layer"):::core
    fixedWindow("Fixed Window"):::middleware
    slidingWindow("Sliding Window"):::middleware
    tokenBucket("Token Bucket"):::middleware
    leakyBucket("Leaky Bucket"):::middleware
    businessLogic("Business Logic/Usecase Layer"):::core
    dataRepo("Data Repository"):::database
    redisCache("Redis Cache"):::database
    appBootstrap("Application Bootstrapping"):::core
    graphEngine("Graph Engine/Shortest Path Module"):::core
    graphDataStore("Graph Data Store"):::database

    %% Data Flow
    User --> apiGateway
    configYaml --> apiGateway
    configGo --> apiGateway
    apiGateway --> monitoring
    apiGateway --> apiController

    %% Rate Limiting Lane
    apiController --> fixedWindow
    fixedWindow --> slidingWindow
    slidingWindow --> tokenBucket
    tokenBucket --> leakyBucket
    leakyBucket --> businessLogic
    businessLogic --> dataRepo
    businessLogic --> redisCache
    businessLogic --> monitoring

    %% Graph Processing Lane
    apiGateway --> appBootstrap
    appBootstrap --> graphEngine
    graphEngine --> graphDataStore

    %% Click Events
    click apiGateway "https://github.com/vanh01/api-rate-limiting/blob/master/cmd/main.go"
    click apiController "https://github.com/vanh01/api-rate-limiting/blob/master/internal/controller/router.go"
    click fixedWindow "https://github.com/vanh01/api-rate-limiting/blob/master/internal/middleware/fixedwindow.go"
    click slidingWindow "https://github.com/vanh01/api-rate-limiting/blob/master/internal/middleware/slidingwindow.go"
    click tokenBucket "https://github.com/vanh01/api-rate-limiting/blob/master/internal/middleware/tokenbucket.go"
    click leakyBucket "https://github.com/vanh01/api-rate-limiting/blob/master/internal/middleware/leakybucket.go"
    click businessLogic "https://github.com/vanh01/api-rate-limiting/tree/master/internal/usecase"
    click dataRepo "https://github.com/vanh01/api-rate-limiting/blob/master/internal/repo/user.go"
    click redisCache "https://github.com/vanh01/api-rate-limiting/blob/master/pkg/cache/redis.go"
    click configYaml "https://github.com/vanh01/api-rate-limiting/blob/master/config.yaml"
    click configGo "https://github.com/vanh01/api-rate-limiting/blob/master/config/config.go"
    click monitoring "https://github.com/vanh01/api-rate-limiting/blob/master/prometheus.yml"
    click appBootstrap "https://github.com/vanh01/api-rate-limiting/blob/master/internal/app/app.go"

    %% Styles
    classDef core fill:#AED6F1,stroke:#2E86C1,stroke-width:2px;
    classDef middleware fill:#F9E79F,stroke:#F4D03F,stroke-width:2px;
    classDef external fill:#D7BDE2,stroke:#A569BD,stroke-width:2px;
    classDef database fill:#A9DFBF,stroke:#28B463,stroke-width:2px;
    classDef config fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
```
