How gzydong/go-chat works
Project Type and Purpose
• Lumen-IM is a backend server for a web-based instant messaging (IM) system written in Go. It exposes HTTP APIs, runs a WebSocket (“comet”) server, processes asynchronous tasks via a queue, and schedules periodic jobs via cron.
• Key use cases: user authentication, one-to-one and group chat, file uploads, message recall/forward, group voting, system scheduling.
High-Level Architecture & Main Components
a. Clients
• Web frontend (separate repo) communicates over HTTP(S) and WebSocket.
b. HTTP API Server (cmd/lumenim http)
• internal/apis – REST handlers for admin, open and web namespaces.
• internal/business & internal/service – business logic and orchestration.
• internal/repository – MySQL/Redis data access.
• internal/provider – external integrations (Minio, email, captcha, OpenAI).
• config & resource – app configuration and templates.
c. Comet (cmd/lumenim comet)
• internal/comet – WebSocket server, real-time message routing, NSQ consumers for chat events, subscriber management.
d. Queue Worker (cmd/lumenim queue)
• internal/mission/queue and internal/comet/consume – asynchronous job producers/consumers using NSQ.
e. Crontab Scheduler (cmd/lumenim crontab)
• internal/mission/cron – scheduled cleanup, maintenance tasks.
f. Shared Core Libraries (internal/pkg)
• core server abstractions, middleware, socket adapters, validators, logging, error handling, utilities.
g. Infrastructure
• MySQL – persistent storage
• Redis – caching, session/state management
• NSQ – message queue for async events
• Minio – object storage for file uploads
Component Interactions and Data Flows
• HTTP request → router → handler → service → repository → MySQL/Redis → response
• WebSocket client connects → comet router → event handler → service → publishes to NSQ or writes to Redis → comet broadcast back to clients
• Business logic emits jobs to NSQ → queue worker consumes and updates database/Redis or publishes further events
• Cron scheduler triggers tasks → mission handlers perform cleanup or periodic database/Redis operations
• All layers wired via Google Wire (cmd/lumenim/wire.go, internal/*/wire.go)
Architectural Patterns & Principles
• Layered (Onion/Hexagonal) architecture:
Presentation (handlers) → Application (services) → Domain (business) → Infrastructure (repositories/providers)
• Dependency Injection via Wire for testability and clear component boundaries.
• Event-driven components communicating via NSQ for decoupling.
• Micro-process model: separate processes for HTTP, WebSocket, queue, cron.
• Protobuf definitions under api/proto and generated Go files in api/pb for internal/open/admin API contracts or future gRPC endpoints.