# cod-e-codes/marchat architecture

> Architecture diagram of the GitHub repository cod-e-codes/marchat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cod-e-codes/marchat
- Repository: https://github.com/cod-e-codes/marchat

## Overview

#### 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`](https://github.com/cod-e-codes/marchat/blob/main/client/main.go) (TUI lifecycle), [`server/client.go`](https://github.com/cod-e-codes/marchat/blob/main/server/client.go) (Connection lifecycle), [`cmd/server/main.go`](https://github.com/cod-e-codes/marchat/blob/main/cmd/server/main.go) (Server bootstrap), [`server/handlers.go`](https://github.com/cod-e-codes/marchat/blob/main/server/handlers.go) (HTTP / WebSocket routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_server["Server runtime"]
  node_server_bootstrap["Server bootstrap<br/>Go binary<br/>[main.go]"]
  node_routes["HTTP / WebSocket routes<br/>request dispatch<br/>[handlers.go]"]
  node_connection["Connection lifecycle<br/>WebSocket session<br/>[client.go]"]
  node_message_state["Message state machine<br/>validation and transitions<br/>[message_state.go]"]
  node_hub{{"Connection hub<br/>presence and fan-out<br/>[hub.go]"}}
  node_persistence[("Chat persistence<br/>database layer<br/>[db.go]")]
  node_server_config["Server configuration<br/>environment config<br/>[config.go]"]
  node_admin["Admin surfaces<br/>terminal and web admin<br/>[admin_web.go]"]
end

subgraph group_client["Terminal client"]
  node_client_tui["TUI lifecycle<br/>Go terminal client<br/>[main.go]"]
  node_client_transport["WebSocket transport<br/>client protocol<br/>[websocket.go]"]
  node_client_config["Local profiles and settings<br/>client storage<br/>[config.go]"]
  node_client_keystore[("E2E keystore<br/>protected local keys<br/>[keystore.go]")]
  node_client_hooks["Local process hooks<br/>optional hooks<br/>[exthook.go]"]
end

subgraph group_plugins["Plugin runtime"]
  node_plugin_manager["Plugin manager<br/>installation orchestration<br/>[manager.go]"]
  node_plugin_host["Plugin process host<br/>process lifecycle<br/>[host.go]"]
  node_plugin_sdk["Plugin stdio SDK<br/>process protocol<br/>[stdio.go]"]
end

subgraph group_deploy["Deployment"]
  node_tls_proxy["Caddy TLS proxy<br/>optional reverse proxy"]
end

node_shared_protocol["Shared protocol types<br/>wire and domain types<br/>[types.go]"]
node_shared_crypto{{"Shared E2E crypto<br/>ChaCha20-Poly1305<br/>[crypto.go]"}}

node_server_bootstrap -->|"loads"| node_server_config
node_server_bootstrap -->|"starts"| node_routes
node_server_bootstrap -->|"initializes"| node_persistence
node_routes -->|"upgrades WS"| node_connection
node_connection -->|"validated frames"| node_message_state
node_message_state -->|"persists state"| node_persistence
node_message_state -->|"publishes events"| node_hub
node_connection -->|"registers sessions"| node_hub
node_hub -->|"scoped delivery"| node_connection
node_persistence -->|"handshake replay"| node_connection
node_admin -->|"admin state"| node_persistence
node_client_tui -->|"sends and receives"| node_client_transport
node_client_tui -->|"uses"| node_client_config
node_client_tui -.->|"uses optional keys"| node_client_keystore
node_client_tui -.->|"invokes optional hooks"| node_client_hooks
node_client_transport -->|"WebSocket traffic"| node_routes
node_client_tui -->|"uses"| node_shared_protocol
node_connection -->|"uses"| node_shared_protocol
node_client_keystore -->|"protects key material"| node_shared_crypto
node_client_tui -.->|"encrypts payloads"| node_shared_crypto
node_plugin_manager -->|"launches"| node_plugin_host
node_plugin_host -->|"stdio protocol"| node_plugin_sdk
node_server_bootstrap -.->|"manages optional runtime"| node_plugin_manager
node_tls_proxy -.->|"proxies optional TLS"| node_routes

click node_server_bootstrap "https://github.com/cod-e-codes/marchat/blob/main/cmd/server/main.go"
click node_routes "https://github.com/cod-e-codes/marchat/blob/main/server/handlers.go"
click node_connection "https://github.com/cod-e-codes/marchat/blob/main/server/client.go"
click node_message_state "https://github.com/cod-e-codes/marchat/blob/main/server/message_state.go"
click node_hub "https://github.com/cod-e-codes/marchat/blob/main/server/hub.go"
click node_persistence "https://github.com/cod-e-codes/marchat/blob/main/server/db.go"
click node_server_config "https://github.com/cod-e-codes/marchat/blob/main/server/config.go"
click node_admin "https://github.com/cod-e-codes/marchat/blob/main/server/admin_web.go"
click node_client_tui "https://github.com/cod-e-codes/marchat/blob/main/client/main.go"
click node_client_transport "https://github.com/cod-e-codes/marchat/blob/main/client/websocket.go"
click node_client_config "https://github.com/cod-e-codes/marchat/blob/main/client/config/config.go"
click node_client_keystore "https://github.com/cod-e-codes/marchat/blob/main/client/crypto/keystore.go"
click node_client_hooks "https://github.com/cod-e-codes/marchat/blob/main/client/exthook/exthook.go"
click node_shared_protocol "https://github.com/cod-e-codes/marchat/blob/main/shared/types.go"
click node_shared_crypto "https://github.com/cod-e-codes/marchat/blob/main/shared/crypto.go"
click node_plugin_manager "https://github.com/cod-e-codes/marchat/blob/main/plugin/manager/manager.go"
click node_plugin_host "https://github.com/cod-e-codes/marchat/blob/main/plugin/host/host.go"
click node_plugin_sdk "https://github.com/cod-e-codes/marchat/blob/main/plugin/sdk/stdio.go"
click node_tls_proxy "https://github.com/cod-e-codes/marchat/blob/main/deploy/caddy/Caddyfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server_bootstrap,node_routes,node_connection,node_message_state,node_hub,node_persistence,node_server_config,node_admin toneBlue
class node_client_tui,node_client_transport,node_client_config,node_client_keystore,node_client_hooks toneAmber
class node_plugin_manager,node_plugin_host,node_plugin_sdk toneMint
class node_tls_proxy toneRose
class node_shared_protocol,node_shared_crypto toneNeutral
```

## Components

### Server runtime

Authoritative chat service.

- [Server bootstrap](https://github.com/cod-e-codes/marchat/blob/main/cmd/server/main.go): Go binary · `cmd/server/main.go`
- [HTTP / WebSocket routes](https://github.com/cod-e-codes/marchat/blob/main/server/handlers.go): request dispatch · `server/handlers.go`
- [Connection lifecycle](https://github.com/cod-e-codes/marchat/blob/main/server/client.go): WebSocket session · Authenticates clients and handles frame I/O. · `server/client.go`
- [Message state machine](https://github.com/cod-e-codes/marchat/blob/main/server/message_state.go): validation and transitions · Authorizes and applies chat mutations. · `server/message_state.go`
- [Connection hub](https://github.com/cod-e-codes/marchat/blob/main/server/hub.go): presence and fan-out · Scopes delivery and cleans stale connections. · `server/hub.go`
- [Chat persistence](https://github.com/cod-e-codes/marchat/blob/main/server/db.go): database layer · Durable state and handshake replay history. · `server/db.go`
- [Server configuration](https://github.com/cod-e-codes/marchat/blob/main/server/config.go): environment config · `server/config.go`
- [Admin surfaces](https://github.com/cod-e-codes/marchat/blob/main/server/admin_web.go): terminal and web admin · `server/admin_web.go`

### Terminal client

Reference Bubble Tea client.

- [TUI lifecycle](https://github.com/cod-e-codes/marchat/blob/main/client/main.go): Go terminal client · Maintains views and rebuilds transcripts from replay. · `client/main.go`
- [WebSocket transport](https://github.com/cod-e-codes/marchat/blob/main/client/websocket.go): client protocol · Connects and sanitizes frames. · `client/websocket.go`
- [Local profiles and settings](https://github.com/cod-e-codes/marchat/blob/main/client/config/config.go): client storage · `client/config/config.go`
- [E2E keystore](https://github.com/cod-e-codes/marchat/blob/main/client/crypto/keystore.go): protected local keys · `client/crypto/keystore.go`
- [Local process hooks](https://github.com/cod-e-codes/marchat/blob/main/client/exthook/exthook.go): optional hooks · `client/exthook/exthook.go`

### Plugin runtime

Server-managed external processes.

- [Plugin manager](https://github.com/cod-e-codes/marchat/blob/main/plugin/manager/manager.go): installation orchestration · Manages installed and enabled plugins. · `plugin/manager/manager.go`
- [Plugin process host](https://github.com/cod-e-codes/marchat/blob/main/plugin/host/host.go): process lifecycle · Runs plugins as separate processes. · `plugin/host/host.go`
- [Plugin stdio SDK](https://github.com/cod-e-codes/marchat/blob/main/plugin/sdk/stdio.go): process protocol · `plugin/sdk/stdio.go`

### Deployment

Optional container and TLS boundary.

- [Caddy TLS proxy](https://github.com/cod-e-codes/marchat/blob/main/deploy/caddy/Caddyfile): optional reverse proxy · `deploy/caddy/Caddyfile`

### Other components

- [Shared protocol types](https://github.com/cod-e-codes/marchat/blob/main/shared/types.go): wire and domain types · `shared/types.go`
- [Shared E2E crypto](https://github.com/cod-e-codes/marchat/blob/main/shared/crypto.go): ChaCha20-Poly1305 · Uses an out-of-band globally shared symmetric key. · `shared/crypto.go`

## Connections

- 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
