# quentinregnier/nubo-backend architecture

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

- Interactive diagram: https://gitdiagram.com/quentinregnier/nubo-backend
- Repository: https://github.com/quentinregnier/nubo-backend

## Overview

##### Project Type & Purpose
- This is a backend microservice written in Go (Golang) that exposes both a REST API and a WebSocket interface.
- It uses Docker Compose to orchestrate four core services: the Go API, Redis, PostgreSQL, and MongoDB.
- Purpose: serve as the server-side for a social-media-style application (“nubo-backend”) supporting authentication, posts, messaging, real-time events, and caching layers.

##### File Structure Analysis
Top-level artifacts:
- Dockerfile / docker-compose.yml / .env → containerized environment setup
- cmd/main.go → entry point wiring Gin REST handlers, WebSocket hub, DB & cache initialization
- internal/ → Go code split by concern
– internal/api → REST route definitions and middleware (routes.go, middleware.go)
– internal/websocket → WebSocket handler + hub for broadcast (handler.go, hub.go)
– internal/cache → Redis client init + caching strategy (redis.go, redis_caches.go, redis_strategy.go)
– internal/db → MongoDB and PostgreSQL connectors/managers (mongo.go, postgres.go, mongo_manage.go)
– internal/initdata → startup jobs to clean Redis & Mongo (“init.go”)
– internal/tools → shared utilities (collections, parsing)
- sql/ → raw SQL scripts (functions, procedures, tables, views) for PostgreSQL schema
- mongo-data/ & postgres-data-broken/ → local data volumes persisted by Docker

##### README Insights
- Confirms architecture: Go + Gin for REST, Gorilla WebSocket, Redis for cache & pub/sub, PostgreSQL for “truth,” MongoDB for 30-day ephemeral store.
- Stepwise dev plan: REST → WebSocket → DB connections → JWT auth → deploy.
- Detailed Docker Compose service definitions.
- Future architectural phases: scaling WebSocket via Redis pub/sub, token protection, data cleaning, multi-instance.

1. Clients (mobile & web)
2. API Server (Go/Gin)
3. WebSocket Hub (Gorilla/WebSocket + Redis Pub/Sub)
4. Redis (cache, session store, pub/sub channel)
5. PostgreSQL (relational permanent storage)
6. MongoDB (document store for recent data, TTL)
7. Docker Compose network (overlay of containers)
- HTTP clients →→ Gin REST handlers →→ Postgres/Mongo/Redis
- WebSocket clients ↔ HTTP upgrader → WebSocket handler → Hub
- Hub broadcasts → publishes to Redis Pub/Sub → other hub instances or same instance → WebSocket clients
- Business logic layer within Go orchestrates multi-store writes: Redis → Mongo → PostgreSQL
c. Architectural Patterns & Principles:
- Layered architecture (API, service, data layers)
- CQRS-style read-path: Redis cache → Mongo (recent) → PostgreSQL (history)
- Pub/Sub for real-time scaling via Redis channel
- Module separation: each concern in its own Go package under internal/
- Startup hooks: initdata for one-time cleaning tasks
d. Key Technologies / Frameworks:
- Go + Gin (REST), Gorilla WebSocket (real-time)
- Redis v8 client (cache, pub/sub), custom LRU cache strategy
- go.mongodb.org/mongo-driver (MongoDB TTL collections)
- jackc/pgx (PostgreSQL connection) + SQL scripts (functions, procedures, tables, views)
- Docker + Docker Compose for environment orchestration

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    Clients["Clients (Web & Mobile)"]:::client

    %% Docker Compose Group
    subgraph "Docker Compose Network"
        direction TB
        API["API Server\nGo+Gin\nHTTP:8080\nWS:/ws"]:::api
        Redis["Redis\nCache & Pub/Sub\n:6379"]:::cache
        Mongo["MongoDB\nDocument Store\n:27017"]:::mongo
        Postgres["PostgreSQL\nRelational Store\n:5432"]:::db
    end

    %% Data Flow
    Clients -->|"HTTP REST"| API
    Clients -->|"WebSocket Upgrade"| API
    API -->|"Read/Write Cache"| Redis
    API -->|"Publish/Sub"| Redis
    API -->|"Write Recent"| Mongo
    API -->|"Write History"| Postgres
    Redis -->|"Pub/Sub Channel"| API

    %% Click Events
    click API "https://github.com/quentinregnier/nubo-backend/blob/main/cmd/main.go"
    click Routes "https://github.com/quentinregnier/nubo-backend/blob/main/internal/api/routes.go"
    click Middleware "https://github.com/quentinregnier/nubo-backend/blob/main/internal/api/middleware.go"
    click WSHandler "https://github.com/quentinregnier/nubo-backend/blob/main/internal/websocket/handler.go"
    click WSHub "https://github.com/quentinregnier/nubo-backend/blob/main/internal/websocket/hub.go"
    click RedisClient "https://github.com/quentinregnier/nubo-backend/blob/main/internal/cache/redis.go"
    click RedisCaches "https://github.com/quentinregnier/nubo-backend/blob/main/internal/cache/redis_caches.go"
    click RedisStrategy "https://github.com/quentinregnier/nubo-backend/blob/main/internal/cache/redis_strategy.go"
    click PGConnector "https://github.com/quentinregnier/nubo-backend/blob/main/internal/db/postgres.go"
    click MongoConnector "https://github.com/quentinregnier/nubo-backend/blob/main/internal/db/mongo.go"
    click MongoManager "https://github.com/quentinregnier/nubo-backend/blob/main/internal/db/mongo_manage.go"
    click InitData "https://github.com/quentinregnier/nubo-backend/blob/main/internal/initdata/init.go"
    click Utils "https://github.com/quentinregnier/nubo-backend/blob/main/internal/tools/collections.go"
    click Dockerfile "https://github.com/quentinregnier/nubo-backend/tree/main/Dockerfile"
    click DockerCompose "https://github.com/quentinregnier/nubo-backend/blob/main/docker-compose.yml"
    click Env "https://github.com/quentinregnier/nubo-backend/blob/main/.env"
    click SQL "https://github.com/quentinregnier/nubo-backend/tree/main/sql/"
    click MongoData "https://github.com/quentinregnier/nubo-backend/tree/main/mongo-data/"
    click PostgresData "https://github.com/quentinregnier/nubo-backend/tree/main/postgres-data-broken/"

    %% Styles
    classDef client fill:#f9f,stroke:#333,stroke-width:2px;
    classDef api fill:#bbf,stroke:#333,stroke-width:2px;
    classDef cache fill:#bfb,stroke:#333,stroke-width:2px;
    classDef db fill:#feb,stroke:#333,stroke-width:2px;
    classDef mongo fill:#8ff,stroke:#333,stroke-width:2px;
```
