# mesh-dell/weather-api architecture

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

- Interactive diagram: https://gitdiagram.com/mesh-dell/weather-api
- Repository: https://github.com/mesh-dell/weather-api

## Overview

##### Project type and purpose
- This is a backend service (an HTTP‐based microservice) written in Go.
- It exposes “/weather” endpoints to clients, applying rate limiting, caching results, and fetching data from an external weather API.

##### File structure
- cmd/main.go
– Entry point: sets up HTTP server, middleware, routing, and reads configuration.
- internal/config
– Loads environment variables or config files (API keys, cache settings).
- internal/middleware
– Implements rate limiting (limiter.go) on incoming requests.
- internal/handlers
– HTTP handlers (weather_handler.go) translate HTTP requests to service calls and format responses.
- internal/services
– Core business logic (weather_service.go): checks cache, calls external API, processes data.
- internal/cache
– Caching layer (cache.go): in-memory or external (Redis, etc.) to store recent results.
- internal/api
– API client (api.go): wraps external weather provider HTTP calls.
- internal/dtos
– Data Transfer Objects (weatherDTO.go): define request/response schemas.

##### Key architectural patterns
- Layered/Clean Architecture:
– Transport layer (HTTP + middleware)
– Handler/controller layer
– Service/business logic layer
– External dependencies (cache, external API)
- Dependency injection: config and cache passed into services.
- Separation of concerns: each package has a single responsibility.

##### Main components and their interactions
- Client (e.g., browser, mobile app, another service)
↓
- HTTP Server (cmd/main.go)
– Reads config, initializes router, middleware, and dependencies
- Middleware (Rate Limiter)
– Enforces request quotas per client IP or API key
↓
- Handler (weather_handler)
– Parses incoming request, maps to internal DTO
↓
- Service (weather_service)
– Checks cache.lookup(key)
- If cache hit → return cached DTO
- If cache miss → calls external API client
↳ internal/api → makes HTTP request to third-party weather provider
← receives raw data, maps to DTO
– Stores result in cache.set(key, dto)
– Returns DTO to handler
↓
- Handler serializes DTO to JSON and sends HTTP response

##### External dependencies and technologies
- Go standard library (net/http) or a router (e.g., gorilla/mux)
- (Optional) Redis or in-memory cache via internal/cache
- Third-party weather data provider (HTTP REST API)
- Environment/config management (Viper, godotenv, or simple os.Getenv)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    Client["Client"]:::external

    %% Transport Layer
    subgraph "Transport Layer"
        HTTP_Server["HTTP Server"]:::transport
        Config["Configuration Loader"]:::transport
        Rate_Limiter["Middleware: Rate Limiter"]:::transport
    end

    %% Application Layer
    subgraph "Application Layer"
        Weather_Handler["Handler: WeatherHandler"]:::application
        Weather_Service["Service: WeatherService"]:::application
    end

    %% External/Persistence Layer
    subgraph "External/Persistence Layer"
        Cache["Cache: In-Memory/Redis"]:::persistence
        API_Client["External API Client"]:::persistence
        External_Provider["External Weather Provider"]:::external
    end

    %% Connections
    Client -->|HTTP Request| HTTP_Server
    HTTP_Server -->|reads config| Config
    HTTP_Server -->|applies rate limiting| Rate_Limiter
    Rate_Limiter -->|passes request| Weather_Handler
    Weather_Handler -->|calls service| Weather_Service
    Weather_Service -->|cache lookup| Cache
    Cache -->|hit/miss| Weather_Service
    Weather_Service -->|calls external API| API_Client
    API_Client -->|HTTP Request| External_Provider
    External_Provider -->|response data| API_Client
    API_Client -->|returns data| Weather_Service
    Weather_Service -->|returns DTO| Weather_Handler
    Weather_Handler -->|HTTP Response| HTTP_Server
    HTTP_Server -->|HTTP Response| Client
    Config -->|provides settings| Cache
    Config -->|provides settings| API_Client

    %% Click Events
    click HTTP_Server "https://github.com/mesh-dell/weather-api/blob/main/cmd/main.go"
    click Config "https://github.com/mesh-dell/weather-api/blob/main/internal/config/config.go"
    click Rate_Limiter "https://github.com/mesh-dell/weather-api/blob/main/internal/middleware/limiter.go"
    click Weather_Handler "https://github.com/mesh-dell/weather-api/blob/main/internal/handlers/weather_handler.go"
    click Weather_Service "https://github.com/mesh-dell/weather-api/blob/main/internal/services/weather_service.go"
    click Cache "https://github.com/mesh-dell/weather-api/blob/main/internal/cache/cache.go"
    click API_Client "https://github.com/mesh-dell/weather-api/blob/main/internal/api/api.go"
    click Weather_Handler "https://github.com/mesh-dell/weather-api/blob/main/internal/dtos/weatherDTO.go"

    %% Styles
    classDef transport fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef application fill:#E8FDD0,stroke:#2c662d,color:#2c662d;
    classDef persistence fill:#FFF5D0,stroke:#d69400,color:#d69400;
    classDef external fill:#F0F0F0,stroke:#666,stroke-dasharray: 5 5;
```
