How santosl2/go-api-whatsmeow works
Purpose and Entry Point
A compact Go HTTP API that exposes WhatsApp operations through the whatsmeow library. The process starts in main.go, which is responsible for assembling infrastructure, WhatsApp connectivity, and HTTP handlers.
Primary entry point:
• main.go
Application Composition
The repository is organized around two service adapters and an instance-oriented HTTP boundary:
• SQL-backed persistence adapter: pkg/services/sqlstore.go
• WhatsApp/whatsmeow integration adapter: pkg/services/whatsmeow.go
• Instance HTTP handler: pkg/instances/handler/instances_handler.go
main.go is the composition root: it should create concrete services and bind handler routes rather than embedding business logic itself.
WhatsApp Integration Boundary
pkg/services/whatsmeow.go encapsulates communication with the external WhatsApp network via the third-party whatsmeow Go library. This is the material external-service boundary and likely owns client lifecycle concerns such as connection, authentication/session restoration, event handling, and message transport.
Paths:
• pkg/services/whatsmeow.go
• go.mod
HTTP handlers should depend on this service boundary rather than invoke whatsmeow directly.
Persistent Session / Device Storage
pkg/services/sqlstore.go provides the SQL persistence boundary, likely used by whatsmeow to retain device identity, login state, and session material across process restarts. It separates database details from API and WhatsApp behavior.
Paths:
• pkg/services/sqlstore.go
• go.mod
The database is infrastructure required for durable WhatsApp client state, not an API-facing domain store unless additional behavior is added.
Instance API Layer
The instances package models the application’s primary API concept: a WhatsApp instance. The handler package translates HTTP requests into instance lifecycle actions and returns HTTP responses.
Paths:
• pkg/instances/handler/instances_handler.go
• pkg/instances
Likely control flow: HTTP request → instance handler → WhatsApp service and/or SQL-backed session store → response. This is the main boundary between callers and integration infrastructure.
Runtime and External Interfaces
Runtime: a single Go service/module, defined by:
• go.mod
• main.go
External interfaces:
• HTTP API, with example/manual requests in client.http
• WhatsApp protocol/network through whatsmeow
• SQL database through the store adapter
The process is not structured as a frontend application; clients interact over HTTP while the service manages WhatsApp client sessions internally.
Architectural Constraints
Keep transport-specific behavior in pkg/instances/handler/instances_handler.go, WhatsApp protocol behavior in pkg/services/whatsmeow.go, and persistence concerns in pkg/services/sqlstore.go. New instance operations should preserve this direction of dependency: entrypoint/handler orchestration → service adapters → external WhatsApp and SQL systems.
Main entry points: instances_handler.go Instance Handler · whatsmeow.go Whatsmeow Service · main.go Composition Root · sqlstore.go SQL Store
Components 10
Go Service
- Composition Root · Assembles services and binds HTTP routes.
main.go - Instance Handler · Translates instance API requests into lifecycle actions.
pkg/instances/handler/instances_handler.go - Instances · Primary WhatsApp instance domain boundary.
pkg/instances - Go Module · Defines the service module and third-party dependencies.
go.mod - Request Examples
client.http
Infrastructure Adapters
- Whatsmeow Service · Owns WhatsApp client lifecycle and protocol transport.
pkg/services/whatsmeow.go - SQL Store · Persists device identity and WhatsApp session state.
pkg/services/sqlstore.go
External Systems
- HTTP Clients
- WhatsApp Network
- SQL Database
Connections 13
- Composition Root → Instance Handler: binds routes
- Composition Root → Whatsmeow Service: constructs
- Composition Root → SQL Store: constructs
- Composition Root → Go Module: uses dependencies
- HTTP Clients → Instance Handler: HTTP requests
- Request Examples → Instance Handler: example requests
- Instance Handler → Instances: handles instances
- Instance Handler → Whatsmeow Service: orchestrates
- Instance Handler → SQL Store: accesses session state
- Whatsmeow Service → SQL Store: persists sessions
- Whatsmeow Service → WhatsApp Network: WhatsApp protocol
- SQL Store → SQL Database: stores state
- Go Module → Whatsmeow Service: provides whatsmeow