How cod-e-codes/marchat works
1. Purpose and executable entry points
marchat is a self-hosted, Go-based terminal chat system. It consists of a WebSocket server, a Bubble Tea reference TUI client, and an optional server-managed plugin runtime. Main binaries are:
• Server bootstrap: cmd/server/main.go
• Terminal client bootstrap and TUI lifecycle: client/main.go
• Shared wire/domain types and protocol-compatible crypto primitives: shared/types.go, shared/crypto.go
2. Server connection and message pipeline
The server accepts HTTP/WebSocket traffic, authenticates/validates clients, and maintains live connection state. The hub is the concurrency and fan-out boundary: it tracks users, broadcasts scoped events, and cleans stale connections.
• HTTP/WS routes and request dispatch: server/handlers.go, server/health.go
• Per-WebSocket client read/write/auth lifecycle: server/client.go, server/request_context.go
• Connection registry, presence, broadcast, cleanup: server/hub.go
• Message validation and state transitions for send/edit/delete/pin/react/read/typing: server/message_validate.go, server/message_state.go
Control flow is broadly client frame → server validation/authorization/rate controls → persistence/state update → hub delivery to eligible connected recipients. Channel and DM scope are enforced server-side; client views are projections of the stream plus handshake replay.
3. Persistence and history boundary
The server owns durable chat state and replay history, including messages, reactions, user channel state, read receipts, bans, and per-user history state. It supports SQLite by default and PostgreSQL/MySQL through dialect-aware initialization and queries.
• Database initialization, migrations, and persistence operations: server/db.go
• Backend/DDL selection: server/db_dialect.go
• Runtime server configuration including database source: server/config.go
The database stores ciphertext rather than plaintext when E2E is enabled; therefore server search and moderation logic operate on stored payloads and metadata, not decrypted user content.
4. Reference client and local state
The client is a terminal UI that collects commands/input, maintains transcript/view state, connects over WebSockets, and renders server events. It persists profiles, themes, notification settings, local DM UI state, and optional encrypted key material outside the server database.
• TUI model, commands, rendering, and input handling: client/main.go, client/commands.go, client/render.go
• WebSocket client transport and frame sanitization: client/websocket.go
• Client profiles/configuration: client/config/config.go
• Desktop/bell notification policy: client/notification_manager.go
• Optional local send/receive process hooks: client/exthook/exthook.go
A reconnect replaces/rebuilds the local transcript from server handshake replay, making the server—not the client cache—the authority for recent message history.
5. Security and administration
Authentication distinguishes configured admin users from regular users. Server configuration is environment/.env-driven; diagnostics inspect resolved paths/configuration without starting normal runtime.
• Server config loading and interactive setup: server/config.go, server/config_ui.go
• Shared-key ChaCha20-Poly1305 E2E operations: shared/crypto.go
• Client passphrase-protected E2E keystore: client/crypto/keystore.go
• Admin terminal/web surfaces: server/admin_panel.go, server/admin_web.go
• Shared diagnostics framework: internal/doctor/doctor.go
E2E uses a globally shared symmetric key distributed out of band, not per-user key exchange. TLS can be terminated by the server or an external reverse proxy.
6. Plugin and deployment boundaries
Plugins are installed/managed by the server and run as separate plugin processes using a stdio SDK/protocol boundary. Registry acquisition is an outbound network dependency; plugins themselves may introduce further dependencies.
• Plugin process host and lifecycle: plugin/host/host.go
• Installation/enabled-state orchestration: plugin/manager/manager.go, plugin/store/store.go
• Plugin SDK and stdio contract: plugin/sdk/plugin.go, plugin/sdk/stdio.go
The runtime is Go; material infrastructure includes SQLite files or PostgreSQL/MySQL, WebSocket clients, optional Docker deployment, and optional Caddy TLS proxy:
• Container/server launch: Dockerfile, docker-compose.yml, entrypoint.sh
• Caddy reverse proxy: docker-compose.proxy.yml, deploy/caddy/Caddyfile
Main entry points: client/main.go TUI lifecycle · client.go Connection lifecycle · server/main.go Server bootstrap · handlers.go HTTP / WebSocket routes
Components 19
Server runtime
Authoritative chat service.
- Server bootstrap
cmd/server/main.go - HTTP / WebSocket routes
server/handlers.go - Connection lifecycle · Authenticates clients and handles frame I/O.
server/client.go - Message state machine · Authorizes and applies chat mutations.
server/message_state.go - Connection hub · Scopes delivery and cleans stale connections.
server/hub.go - Chat persistence · Durable state and handshake replay history.
server/db.go - Server configuration
server/config.go - Admin surfaces
server/admin_web.go
Terminal client
Reference Bubble Tea client.
- TUI lifecycle · Maintains views and rebuilds transcripts from replay.
client/main.go - WebSocket transport · Connects and sanitizes frames.
client/websocket.go - Local profiles and settings
client/config/config.go - E2E keystore
client/crypto/keystore.go - Local process hooks
client/exthook/exthook.go
Plugin runtime
Server-managed external processes.
- Plugin manager · Manages installed and enabled plugins.
plugin/manager/manager.go - Plugin process host · Runs plugins as separate processes.
plugin/host/host.go - Plugin stdio SDK
plugin/sdk/stdio.go
Other components
- Shared protocol types
shared/types.go - Shared E2E crypto · Uses an out-of-band globally shared symmetric key.
shared/crypto.go
Connections 24
- Server bootstrap → Server configuration: loads
- Server bootstrap → HTTP / WebSocket routes: starts
- Server bootstrap → Chat persistence: initializes
- HTTP / WebSocket routes → Connection lifecycle: upgrades WS
- Connection lifecycle → Message state machine: validated frames
- Message state machine → Chat persistence: persists state
- Message state machine → Connection hub: publishes events
- Connection lifecycle → Connection hub: registers sessions
- Connection hub → Connection lifecycle: scoped delivery
- Chat persistence → Connection lifecycle: handshake replay
- Admin surfaces → Chat persistence: admin state
- TUI lifecycle → WebSocket transport: sends and receives
- TUI lifecycle → Local profiles and settings: uses
- TUI lifecycle → E2E keystore: uses optional keys
- TUI lifecycle → Local process hooks: invokes optional hooks
- WebSocket transport → HTTP / WebSocket routes: WebSocket traffic
- TUI lifecycle → Shared protocol types: uses
- Connection lifecycle → Shared protocol types: uses
- E2E keystore → Shared E2E crypto: protects key material
- TUI lifecycle → Shared E2E crypto: encrypts payloads
- Plugin manager → Plugin process host: launches
- Plugin process host → Plugin stdio SDK: stdio protocol
- Server bootstrap → Plugin manager: manages optional runtime
- Caddy TLS proxy → HTTP / WebSocket routes: proxies optional TLS