# msardary/user-management-chat architecture

> Architecture diagram of the GitHub repository msardary/user-management-chat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/msardary/user-management-chat
- Repository: https://github.com/msardary/user-management-chat

## Overview

##### Project type and purpose
- This is a Go-based **user management system with real-time chat**, organized as a modular microservices-style API. It provides JWT authentication, role management, chat messaging, and built-in monitoring/logging via Prometheus, Loki, and Grafana.

##### File structure
- **cmd/app**: single entrypoint (`main.go`) that boots the API server
- **internal/**: core business domains
- **auth**: handlers, middleware, service, model for JWT and role checks
- **user**: user CRUD and listing
- **chat**: real-time messaging service and client integration
- **db**: Postgres connection, migrations, SQLC queries
- **config**: application configuration loader
- **server**: router, middleware, logger, Prometheus instrumentation, HTTP server
- **health**: liveness/readiness endpoints
- **pkg/**: reusable utilities (Redis client/cache, token/hash utils, HTTP response helpers, validation)
- **docker-compose.yml** + **Dockerfile**: container definitions for API, Postgres, Redis, Prometheus, Loki, Grafana, Promtail
- **prometheus/**, **loki-config.yaml**, **promtail-config.yaml**: observability setup

##### README for additional insights
- Architecture: monolithic Go API broken into domain modules (auth, user, chat)
- Data stores: PostgreSQL for persistent data, Redis for message caching
- Monitoring: Prometheus client library inside Go service, scraped by Prometheus
- Logging: structured logs shipped via Promtail to Loki, visualized in Grafana

##### Detail
- Ports and protocols (e.g., “HTTP 8080”, “gRPC/REST”, “Redis TCP 6379”).
- Docker Compose orchestration layer if deployment topology is relevant.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    subgraph "Clients"
        Client["Web/Mobile UI"]:::external
    end

    %% API Service
    subgraph "Go API Service" 
        direction TB
        APIGateway["API Gateway / HTTP Server\n(cmd/app/main.go)"]:::service
        Config["Config Loader\n(internal/config/config.go)"]:::service
        Router["HTTP Server Setup & Routing\n(internal/server/server.go,\ninternal/server/router.go)"]:::service
        Middleware["Server Middleware & Logging\n(internal/server/middleware.go,\ninternal/server/logger.go)"]:::service
        PromInst["Prometheus Instrumentation\n(internal/server/prometheus.go)"]:::service
        Health["Health Check Endpoints\n(internal/health/health.go)"]:::service

        subgraph "Domain Modules"
            direction TB
            Auth["Auth Module\n(internal/auth/handler.go,\ninternal/auth/service.go,\ninternal/auth/middleware.go,\ninternal/auth/model.go)"]:::service
            User["User Module\n(internal/user/handler.go,\ninternal/user/service.go,\ninternal/user/model.go)"]:::service
            Chat["Chat Module\n(internal/chat/handler.go,\ninternal/chat/service.go,\ninternal/chat/client.go,\ninternal/chat/model.go)"]:::service
        end

        subgraph "Utilities (pkg)"
            direction TB
            RedisClient["Redis Cache Client\n(pkg/redisx/client.go)"]:::cache
            MessageCache["Message Cache\n(pkg/redisx/message_cache.go)"]:::cache
            TokenUtil["Token & Hash Utilities\n(pkg/utils/token.go,\n pkg/utils/hash.go)"]:::service
            HTTPResp["HTTP Response Helpers\n(pkg/response/response.go)"]:::service
            Validation["Input Validation\n(pkg/validation/validator.go)"]:::service
        end
    end

    %% Datastores
    subgraph "Datastores"
        direction TB
        Postgres["PostgreSQL\n(internal/db/postgres.go,\ninternal/db/migrations/schema.sql)"]:::database
        SQLC["SQLC Queries\n(internal/db/queries/user.sql,\ninternal/db/queries/message.sql,\ninternal/db/queries/token.sql,\ninternal/db/sqlc.yaml)"]:::database
        Redis["Redis Cache"]:::cache
    end

    %% Observability
    subgraph "Observability & Logging"
        direction TB
        Prometheus["Prometheus\n(prometheus/prometheus.yml)"]:::observability
        Loki["Loki\n(loki-config.yaml)"]:::observability
        Promtail["Promtail\n(promtail-config.yaml)"]:::observability
        Grafana["Grafana"]:::external
    end

    %% Connections
    Client -->|"HTTPS/API Calls"| APIGateway
    APIGateway -->|loads config| Config
    APIGateway -->|routes requests| Router
    Router -->|applies middleware| Middleware
    Middleware -->|metrics| PromInst
    Router -->|health checks| Health
    Router -->|auth flow| Auth
    Auth -->|user data| User
    Auth -->|issue/verify JWT| TokenUtil
    User -->|persistence| Postgres
    Chat -->|persistence| Postgres
    Chat -->|cache msgs| RedisClient
    RedisClient --> Redis
    MessageCache --> Redis
    Chat -->|use cache| MessageCache
    PromInst -->|expose /metrics| Prometheus
    Middleware -->|structured logs| Promtail
    Promtail -->|ship logs| Loki
    Grafana -->|dashboards metrics| Prometheus
    Grafana -->|query logs| Loki

    %% Click events
    click APIGateway "https://github.com/msardary/user-management-chat/blob/main/cmd/app/main.go"
    click Router "https://github.com/msardary/user-management-chat/blob/main/internal/server/server.go"
    click Router "https://github.com/msardary/user-management-chat/blob/main/internal/server/router.go"
    click Middleware "https://github.com/msardary/user-management-chat/blob/main/internal/server/middleware.go"
    click Middleware "https://github.com/msardary/user-management-chat/blob/main/internal/server/logger.go"
    click PromInst "https://github.com/msardary/user-management-chat/blob/main/internal/server/prometheus.go"
    click Config "https://github.com/msardary/user-management-chat/blob/main/internal/config/config.go"
    click Health "https://github.com/msardary/user-management-chat/blob/main/internal/health/health.go"
    click Auth "https://github.com/msardary/user-management-chat/blob/main/internal/auth/handler.go"
    click Auth "https://github.com/msardary/user-management-chat/blob/main/internal/auth/service.go"
    click Auth "https://github.com/msardary/user-management-chat/blob/main/internal/auth/middleware.go"
    click Auth "https://github.com/msardary/user-management-chat/blob/main/internal/auth/model.go"
    click User "https://github.com/msardary/user-management-chat/blob/main/internal/user/handler.go"
    click User "https://github.com/msardary/user-management-chat/blob/main/internal/user/service.go"
    click User "https://github.com/msardary/user-management-chat/blob/main/internal/user/model.go"
    click Chat "https://github.com/msardary/user-management-chat/blob/main/internal/chat/handler.go"
    click Chat "https://github.com/msardary/user-management-chat/blob/main/internal/chat/service.go"
    click Chat "https://github.com/msardary/user-management-chat/blob/main/internal/chat/client.go"
    click Chat "https://github.com/msardary/user-management-chat/blob/main/internal/chat/model.go"
    click Postgres "https://github.com/msardary/user-management-chat/blob/main/internal/db/postgres.go"
    click Postgres "https://github.com/msardary/user-management-chat/blob/main/internal/db/migrations/schema.sql"
    click SQLC "https://github.com/msardary/user-management-chat/blob/main/internal/db/queries/user.sql"
    click SQLC "https://github.com/msardary/user-management-chat/blob/main/internal/db/queries/message.sql"
    click SQLC "https://github.com/msardary/user-management-chat/blob/main/internal/db/queries/token.sql"
    click SQLC "https://github.com/msardary/user-management-chat/blob/main/internal/db/sqlc.yaml"
    click RedisClient "https://github.com/msardary/user-management-chat/blob/main/pkg/redisx/client.go"
    click MessageCache "https://github.com/msardary/user-management-chat/blob/main/pkg/redisx/message_cache.go"
    click TokenUtil "https://github.com/msardary/user-management-chat/blob/main/pkg/utils/token.go"
    click TokenUtil "https://github.com/msardary/user-management-chat/blob/main/pkg/utils/hash.go"
    click HTTPResp "https://github.com/msardary/user-management-chat/blob/main/pkg/response/response.go"
    click Validation "https://github.com/msardary/user-management-chat/blob/main/pkg/validation/validator.go"

    %% Styles
    classDef service fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef database fill:#FFE5B4,stroke:#D2691E,color:#D2691E;
    classDef cache fill:#E8D0FF,stroke:#800080,color:#800080;
    classDef external fill:#DFFFE0,stroke:#00A000,color:#00A000;
    classDef observability fill:#E0FFE0,stroke:#00B050,color:#00B050;
```
