How getoptimum/mump2p-cli works
Project type and purpose
• This is a standalone CLI tool (“mump2p”) for interacting with the OptimumP2P pub/sub network.
• It is *not* a full-stack web app but rather an open-source command-line utility that:
• Performs JWT-based authentication (device flow)
• Publishes messages to topics
• Subscribes to real-time streams
• Enforces local rate limits and usage tracking
• Persists messages locally and/or forwards them to webhooks
File structure
• Top-level:
• main.go + cmd/ directory – command definitions (login, publish, subscribe, usage, version, etc.)
• internal/auth/ – client, token parsing, storage (JWT handling)
• internal/ratelimit/ – rate-limit models and enforcement
• internal/config/ – loading CLI configuration (DOMAIN, CLIENT_ID, SERVICE_URL)
• scripts/ – helper scripts (e.g., sample message sender)
• docs/ – user guide
Key insights from README
• The CLI calls an external service (OptimumP2P server) over HTTP or WebSocket-like streaming.
• Authentication is token-based; commands must include/refresh JWT.
• Publish and subscribe functionality have optional flags for custom endpoints, persistence paths, and webhook forwarding.
Main components and their relationships
a. Components
1. CLI Command Parser (cobra in cmd/)
2. Configuration Loader (internal/config)
3. Auth Module (internal/auth):
– Device-flow login (obtains JWT)
– Token storage & refresh
4. RateLimiter Module (internal/ratelimit):
– Local publish count & quota checks
5. Publisher (cmd/publish.go + auth & ratelimit)
6. Subscriber (cmd/subscribe.go + auth):
– Persistent Storage (local filesystem)
– Webhook Forwarder (async queue + HTTP POST)
7. Usage Reporter (cmd/usages.go)
8. External OptimumP2P Service (HTTP/streaming API)
9. Local File System (message persistence)
10. External Webhook Endpoint(s)
b. Relationships & data flow
• User invokes CLI → Command Parser → loads config → ensure valid JWT via Auth Module →
• For publish: RateLimiter checks → HTTP POST to P2P service → report result
• For subscribe: open stream to P2P service → receive messages →
– write to disk (if --persist)
– enqueue & POST to webhook (if --webhook)
• For usage: HTTP GET to P2P service with JWT → parse & display limits
• Auth Module may call token endpoint to refresh JWT as needed
c. Architectural patterns
• Modular CLI layers (cobra command pattern)
• Separation of concerns: config/auth/rate-limit/modules
• Adapter pattern for persistence and webhook forwarding
• Token-based security
d. Key technologies
• Go’s cobra CLI framework
• HTTP client
• JSON Web Tokens (JWT)
• Local file I/O
• Shell scripts for auxiliary tasks