How anyproto/anytype-heart works
Project type and purpose
• This is Anytype Heart, a Go‑based middleware library for Anytype clients. It is neither a full‑stack web app nor a pure CLI tool; rather, it exposes both a gRPC API server and a set of CLI utilities, and provides client libraries (C, JS, mobile) to apps. Its core responsibility is to implement business logic (object/block management, import/export, subscriptions, sync, storage) and expose it over gRPC.
Top‑level components
• Client libraries (clientlibrary/…): native C bridge, gRPC‑Web JS addon, mobile discovery APIs.
• CLI tools (cmd/…): grpcserver, archiveunpacker, debugtree, logdecode, usecasevalidator, etc. They invoke the core services.
• API server (cmd/grpcserver + core/api/server): gRPC entry point, HTTP/gRPC handlers, middleware, paginated endpoints.
• Business logic (core/… + core/application): object/block/domain model, import/export, subscription, sync status, invites, payments, notifications, etc.
• Space orchestration (space/… + space/spacecore): high‑level “space” construct, local P2P discovery, offloading, migration, storage abstraction, peer‑to‑peer coordination.
• Persistence layer (spacecore/storage): BadgerDB, SQLite, in‑memory, RPC‑backed stores.
• Utilities (util/…, pkg/lib/…): logging, metrics, context, protobuf helpers, image processing, network utilities, key‑value stores.
• Protobuf definitions (pb/… + pkg/lib/cafe/pb): service, event, snapshot, command messages.
• Metrics (metrics/…): interceptors, Anymetry client, Grafana/Prometheus provisioning.
• Makefiles / CI / Docker: build scripts, CI workflows, Dockerfile, docker‑compose, Nix flake.
Component interactions
• Clients (desktop/mobile/web) use client libraries to call the gRPC API Server.
• The API Server routes requests to core API handlers, which execute in‑process business logic modules (core/api/internal/...) and orchestrate use‑case flows (core/application/...).
• Core logic invokes space services for multi‑user “space” management.
• The space layer uses spacecore for low‑level P2P coordination, local discovery, and storage abstraction.
• spacecore persists data into BadgerDB/SQLite stores or remote RPC stores.
• Import/export subsystems use file system, zip, CSV, Markdown, Notion, PB converters.
• Metrics & logging are injected via gRPC interceptors in the API server.
• CLI tools instantiate the same core libraries (no server) to run offline tasks.
• The Makefiles, CI, and Docker artifacts define build/deploy flows but do not alter runtime interactions.
Architectural patterns and principles
• Modular monolith: clear package boundaries (API, application, domain, importers, exporters, subscription, sync).
• Dependency inversion: higher‑level modules depend on interfaces (e.g., storage, P2P) with mocks for testing.
• Layered architecture:
Presentation (CLI, gRPC handlers) → Application/service layer → Domain/core logic → Infrastructure (storage, P2P, network).
• CQRS‐style separation: import/export, read‐model pagination, write‐model application flows.
• Extensive use of interfaces and mocks for unit testing.
• gRPC services with protobuf‐generated code.
• Clients (web, mobile, desktop)
• Client Libraries (C bridge, JS addon, mobile SDK)
• gRPC API Server
• Core API Handlers & Middleware
• Application Services (use cases)
• Domain Model / Business Logic (core/…)
• Space Manager & Orchestrator (space/…)
• P2P & Local Discovery (spacecore/localdiscovery)
• Storage Abstraction (spacecore/storage sub‑modules)
• Import/Export + Converters
• Metrics & Logging
• Protobuf Definitions
• CLI Utilities
• Clients → Client Libraries → gRPC Server → Core API → App Services → Domain Logic → Space → Spacecore → Storage
• Core API ← Protobuf definitions
• CLI Utilities → App Services/Domain Logic (bypass server)
c. Highlighted technologies:
• Go, gRPC, Protocol Buffers, BadgerDB, SQLite, Prometheus/Grafana, Anymetry, CGo, Node.js addon
d. Architectural patterns: layered monolith, dependency inversion, interface mocks.