Skip to content

chy3xyz/zigmodu

ZigModu

A modular application framework for Zig 0.16.0, inspired by Spring Modulith. Build scalable applications from monolithic to distributed systems with progressive architecture evolution.

Zig License Build Tests Version Score

πŸ“š Documentation

Guide Description
Quick Start Get started in 5 minutes
Best Practices Architecture evolution from 1K to 1M+ DAU
API Reference Detailed API documentation
Architecture System design and patterns
Evaluation Report Production readiness assessment (75/100)
Examples Runnable example projects
ZModu CLI Code generator for modules, ORM, APIs (npm i -g @chy3xyz/zmodu)

✨ Features

Core Framework

  • Module System β€” Declarative module definition with compile-time dependency validation
  • Lifecycle Management β€” Automatic init/deinit orchestration in dependency order
  • Dependency Injection β€” Type-safe container with compile-time hash checking (CRC32)
  • Event System β€” TypedEventBus + DistributedEventBus + TransactionalEvent + Outbox pattern
  • Application Builder β€” Fluent API with shutdown hooks and graceful termination

HTTP & API

  • HTTP Server β€” Async fiber-based server (kqueue/io_uring), trie router, middleware chains
  • WebSocket β€” RFC 6455 server/client with origin validation and monitoring
  • gRPC ⚠️ β€” Service registry + Proto parser + 16 status codes (experimental)
  • OpenAPI β€” 3.0/3.1 JSON document generator from route metadata
  • Idempotency β€” Request deduplication middleware with TTL-based store

Resilience & Flow Control

  • Circuit Breaker β€” Three-state (closed/open/half-open) with configurable thresholds
  • Rate Limiter β€” Token bucket with per-client overrides
  • Retry Policy β€” Exponential backoff with configurable jitter
  • Load Shedder β€” Adaptive concurrency limiting
  • Saga Orchestrator β€” Automatic compensation with reverse-order rollback + step logging

Data & Persistence

  • SQLx β€” PostgreSQL / MySQL / SQLite with connection pooling + circuit breaker
  • ORM β€” Type-safe repository pattern with compile-time table mapping
  • Database Migrations β€” Flyway/Liquibase-style versioned migrations with SHA256 checksums
  • Cache Manager β€” LRU cache with TTL expiration
  • Redis Client β€” Connection pooling and command pipeline
  • Connection Pool β€” Generic resource pool with health checking

Distributed Systems

  • DistributedEventBus β€” Cross-node event pub/sub with heartbeat
  • ClusterMembership ⚠️ β€” Gossip-based node discovery + health check (experimental)
  • DistributedTransaction ⚠️ β€” 2PC + Saga patterns (experimental, needs persistence)
  • Kafka Connector β€” Producer/Consumer with topic stats + EventBridge
  • Sharding β€” Tenant-aware ShardRouter with configurable pools

Observability

  • Distributed Tracing β€” OpenTelemetry-compatible, Jaeger/Zipkin export
  • Prometheus Metrics β€” Counter / Gauge (lock-free CAS) / Histogram / Summary
  • Structured Logging β€” JSON-formatted with log rotation and levels
  • Auto Instrumentation β€” Automatic lifecycle/event/API instrumentation
  • Health Endpoints β€” K8s-compatible liveness/readiness/module-health probes

Security

  • JWT Authentication β€” Token generation/verification with expiry
  • RBAC β€” Role-based access control
  • Password Encoder β€” Scrypt-based password hashing
  • Security Scanner β€” Static SAST with configurable rules
  • Secrets Manager β€” Multi-source secrets (env > file > Vault > default) with priority resolution
  • Multi-Tenancy β€” TenantContext + DataPermission + ShardRouter

Developer Experience

  • Architecture Tester β€” Compile-time dependency rule validation
  • Module Interaction Verifier β€” Spring Modulith verify()-style interaction model checking
  • Contract Testing β€” Pact-style consumer-driven contract verification
  • Plugin System ⚠️ β€” Dynamic extension loading (experimental)
  • Web Monitor ⚠️ β€” HTTP dashboard for module inspection (experimental)
  • Hot Reloader ⚠️ β€” File-watch based module change detection (experimental)
  • CI/CD Pipeline β€” GitHub Actions: matrix build (linux/macOS), lint, benchmark, Docker, release

πŸš€ Quick Start

Fast Compilation

For large projects, import only the domains you need:

const zmodu = @import("zigmodu");

// Full import (everything):
var app = try zmodu.builder(allocator, io).build(.{MyModule});

// Fast import: only HTTP + Core (skips SQLx, Redis, Kafka, etc.):
const http = zmodu.http;       // Server, middleware, client, OpenAPI
const data = zmodu.data;       // SQLx, Redis, ORM, Cache, Migrations
const sec  = zmodu.security;   // Auth, RBAC, API keys, Secrets
const obs  = zmodu.observability; // Prometheus, Tracing, Logging

Each domain file is self-contained β€” importing zmodu.http does not compile sqlx or redis.

Prerequisites

# Install Zig 0.16.0
brew install zig@0.16.0  # macOS
# or
apt install zig=0.16.0   # Linux

Create Your First Module

// src/modules/user.zig
const std = @import("std");
const zigmodu = @import("zigmodu");

const UserModule = struct {
    pub const info = zigmodu.api.Module{
        .name = "user",
        .description = "User management module",
        .dependencies = &.{},
    };

    pub fn init() !void {
        std.log.info("User module initialized", .{});
    }

    pub fn deinit() void {
        std.log.info("User module cleaned up", .{});
    }
};

Bootstrap Application

// src/main.zig
const std = @import("std");
const zigmodu = @import("zigmodu");

const user = @import("modules/user.zig");

pub fn main(init: std.process.Init) !void {
    const allocator = init.gpa;

    var modules = try zigmodu.scanModules(allocator, .{user});
    defer modules.deinit();

    try zigmodu.validateModules(&modules);
    try zigmodu.startAll(&modules);
    defer zigmodu.stopAll(&modules);

    std.log.info("Application started!", .{});
}

Quick HTTP Server

const Server = zigmodu.http_server.Server;
const Context = zigmodu.http_server.Context;

pub fn main(init: std.process.Init) !void {
    var server = Server.init(init.io, init.gpa, 8080);
    defer server.deinit();

    try server.addRoute(.{
        .method = .GET,
        .path = "/health",
        .handler = struct {
            fn handle(ctx: *Context) anyerror!void {
                try ctx.json(200, "{\"status\":\"ok\"}");
            }
        }.handle,
    });

    try server.start();
}

Docker Compose Quick Start

# Start full stack (zigmodu + PostgreSQL + Redis)
docker compose up -d

# With Vault and Jaeger
docker compose --profile secrets --profile tracing up -d

πŸ“ Project Structure

zigmodu/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ root.zig                       # Public API (PRIMARY / ADVANCED / DEPRECATED)
β”‚   β”œβ”€β”€ Application.zig                # Application builder + lifecycle
β”‚   β”œβ”€β”€ api/                           # Public API types
β”‚   β”‚   β”œβ”€β”€ Module.zig                 # Module / Modulith structs
β”‚   β”‚   β”œβ”€β”€ Server.zig                 # HTTP server + router
β”‚   β”‚   └── Middleware.zig             # Middleware framework
β”‚   β”œβ”€β”€ core/                          # Core framework
β”‚   β”‚   β”œβ”€β”€ Module.zig                 # ModuleInfo, ApplicationModules
β”‚   β”‚   β”œβ”€β”€ ModuleScanner.zig          # Compile-time module scanning
β”‚   β”‚   β”œβ”€β”€ ModuleValidator.zig        # Dependency validation
β”‚   β”‚   β”œβ”€β”€ ModuleInteractionVerifier.zig  # Interaction model verification
β”‚   β”‚   β”œβ”€β”€ EventBus.zig               # Type-safe event bus
β”‚   β”‚   β”œβ”€β”€ DistributedEventBus.zig    # Cross-node event bus
β”‚   β”‚   β”œβ”€β”€ Lifecycle.zig              # startAll/stopAll
β”‚   β”‚   β”œβ”€β”€ Time.zig                   # Monotonic time utility
β”‚   β”‚   β”œβ”€β”€ GrpcTransport.zig          # gRPC service registry + proto parser
β”‚   β”‚   β”œβ”€β”€ KafkaConnector.zig         # Kafka producer/consumer
β”‚   β”‚   β”œβ”€β”€ SagaOrchestrator.zig       # Saga auto-compensation orchestrator
β”‚   β”‚   β”œβ”€β”€ DistributedTransaction.zig # 2PC + Saga transactions
β”‚   β”‚   β”œβ”€β”€ HealthEndpoint.zig         # K8s liveness/readiness probes
β”‚   β”‚   β”œβ”€β”€ HotReloader.zig            # File-watch hot reload
β”‚   β”‚   β”œβ”€β”€ PluginManager.zig          # Dynamic plugin system
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ http/                          # HTTP & API
β”‚   β”‚   β”œβ”€β”€ HttpClient.zig             # HTTP client with pooling
β”‚   β”‚   β”œβ”€β”€ Idempotency.zig            # Request deduplication middleware
β”‚   β”‚   └── OpenApi.zig                # OpenAPI 3.x doc generator
β”‚   β”œβ”€β”€ migration/                     # Database migrations
β”‚   β”‚   └── Migration.zig              # Flyway-style migration runner
β”‚   β”œβ”€β”€ secrets/                       # Secrets management
β”‚   β”‚   └── SecretsManager.zig         # Multi-source secrets with Vault
β”‚   β”œβ”€β”€ resilience/                    # Resilience patterns
β”‚   β”‚   β”œβ”€β”€ CircuitBreaker.zig
β”‚   β”‚   β”œβ”€β”€ RateLimiter.zig
β”‚   β”‚   β”œβ”€β”€ Retry.zig
β”‚   β”‚   └── LoadShedder.zig
β”‚   β”œβ”€β”€ metrics/                       # Observability
β”‚   β”‚   β”œβ”€β”€ PrometheusMetrics.zig
β”‚   β”‚   └── AutoInstrumentation.zig
β”‚   β”œβ”€β”€ tracing/                       # Distributed tracing
β”‚   β”‚   └── DistributedTracer.zig
β”‚   β”œβ”€β”€ security/                      # Authentication & authorization
β”‚   β”‚   β”œβ”€β”€ SecurityModule.zig
β”‚   β”‚   β”œβ”€β”€ SecurityScanner.zig
β”‚   β”‚   β”œβ”€β”€ Rbac.zig
β”‚   β”‚   └── PasswordEncoder.zig
β”‚   β”œβ”€β”€ tenant/                        # Multi-tenancy
β”‚   β”‚   β”œβ”€β”€ TenantContext.zig
β”‚   β”‚   └── ShardRouter.zig
β”‚   β”œβ”€β”€ sqlx/                          # Database drivers
β”‚   β”œβ”€β”€ redis/                         # Redis client
β”‚   β”œβ”€β”€ pool/                          # Connection pool
β”‚   β”œβ”€β”€ cache/                         # Cache (LRU)
β”‚   β”œβ”€β”€ scheduler/                     # Task scheduler (Cron)
β”‚   β”œβ”€β”€ messaging/                     # Message queue + Outbox
β”‚   β”œβ”€β”€ di/                            # DI container
β”‚   β”œβ”€β”€ config/                        # Configuration (JSON/YAML/TOML)
β”‚   β”œβ”€β”€ log/                           # Structured logging
β”‚   β”œβ”€β”€ test/                          # Testing utilities
β”‚   β”‚   β”œβ”€β”€ ContractTest.zig           # Pact-style contract testing
β”‚   β”‚   β”œβ”€β”€ IntegrationTest.zig
β”‚   β”‚   └── ModuleTest.zig
β”‚   └── validation/                    # Object validation
β”œβ”€β”€ docs/                              # Documentation
β”œβ”€β”€ examples/                          # Example projects
β”œβ”€β”€ shopdemo/                          # Full reference app (42 modules, 152 tables)
β”œβ”€β”€ tools/zmodu/                       # zmodu CLI code generator
β”œβ”€β”€ Dockerfile                         # Multi-stage Docker build
β”œβ”€β”€ docker-compose.yml                 # Full stack (PG + Redis + Vault + Jaeger)
└── .github/workflows/ci.yml           # CI/CD pipeline

🎯 Progressive Evolution

ZigModu grows with your application:

Stage DAU Architecture Key Capabilities
1 <1K Monolith Module + Lifecycle
2 1K-10K Vertical Scale Events + Cache
3 10K-100K Multi-Instance CircuitBreaker + RateLimiter
4 100K-1M Distributed DistributedEventBus + Cluster
5 >1M Platform HotReload + Plugins + Kafka

See Best Practices for detailed evolution guide.

πŸ› οΈ Commands

# Build
zig build

# Run tests
zig build test

# Run example
zig build run

# Generate documentation
zig build docs

# Run benchmarks
zig build benchmark

# Format code
zig fmt src/

# Docker
docker compose up -d              # Start full stack
docker compose --profile tracing up -d  # With Jaeger

πŸ“¦ Examples

Example Description
Basic Module fundamentals
Event-Driven Publish-subscribe patterns
Testing Test utilities
HTTP Stress Test Concurrent connections
Metaverse Creative Creative demo
Distributed Multi-node deployment
ShopDemo Full e-commerce (42 modules, 790+ APIs)

🀝 Contributing

Contributions welcome! See CONTRIBUTING.md.

git clone https://github.com/yourusername/zigmodu.git
git checkout -b feature/my-feature
zig build test
git commit -m "feat: add feature"

πŸ“„ License

MIT License - see LICENSE for details.

About

A Modulith framework

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages