Build production-ready REST APIs in Go with automatic code generation
ποΈ Code Generator for Go REST APIs Transform Go structs into production-ready REST APIs with OpenAPI specs, storage backends, and middleware in minutes.
Fabrica is a powerful code generation tool that accelerates API development by transforming simple Go struct definitions into complete, production-ready REST APIs. Define your resources once, and Fabrica generates everything you need: handlers, storage layers, clients, validation, OpenAPI documentation, and more.
- π Zero-Config Generation - Define resources as Go structs, get complete APIs instantly
- π Multiple Storage Backends - Choose between file-based storage or SQL databases (SQLite, PostgreSQL, MySQL)
- π Security Ready - Flexible middleware system for custom authentication and authorization
- π OpenAPI Native - Auto-generated specs with Swagger UI out of the box
- π― Smart Validation - Request validation with detailed, structured error responses
- β‘ Developer Experience - CLI tools, hot-reload development, comprehensive testing
- π‘ CloudEvents Integration - Automatic event publishing for resource lifecycle (CRUD) and condition changes
- π Cloud-Native Ready - API versioning, conditional requests (ETags), event-driven patterns
- ποΈ Production Patterns - Consistent API structure, error handling, and middleware
- Microservices Architecture - Maintain consistent API patterns across services
- Rapid Prototyping - From struct definition to running API in under 5 minutes
- API Standardization - Enforce best practices and patterns across development teams
- OpenAPI-First Development - Generate comprehensive documentation alongside your code
macOS/Linux:
# Direct download and install
curl -L https://github.com/openchami/fabrica/releases/download/v0.3.1/fabrica-$(uname -s)-$(uname -m) -o fabrica
chmod +x fabrica
sudo mv fabrica /usr/local/bin/
# Verify installation
fabrica versionUsing Go:
go install github.com/openchami/fabrica/cmd/[email protected]git clone https://github.com/openchami/fabrica.git
cd fabrica
make install1. Initialize your project:
fabrica init device-api
cd device-api2. Add your first resource:
fabrica add resource Device3. Update your Spec and Status fields in pkg/resources/device/device.go:
Add desired fields to generated DeviceSpec and DeviceStatus structs, retaining other code.
// DeviceSpec defines the desired state of a Device
type DeviceSpec struct {
// copy contents to generated DeviceSpec
Type string `json:"type" validate:"required,oneof=server switch router storage"`
IPAddress string `json:"ipAddress" validate:"required,ip"`
Status string `json:"status" validate:"required,oneof=active inactive maintenance"`
Tags map[string]string `json:"tags,omitempty"`
LastSeen *time.Time `json:"lastSeen,omitempty"`
Port int `json:"port,omitempty" validate:"min=1,max=65535"`
}
// DeviceStatus represents the observed state of a Device
type DeviceStatus struct {
// copy contents to generated DeviceSpec
Health string `json:"health" validate:"required,oneof=healthy degraded unhealthy unknown"`
Uptime int64 `json:"uptime" validate:"min=0"`
LastChecked time.Time `json:"lastChecked"`
ErrorCount int `json:"errorCount" validate:"min=0"`
Version string `json:"version,omitempty"`
}4. Generate your API:
fabrica generate5. Update dependencies:
go mod tidy6. Run your server:
go run ./cmd/server7. Test your API:
# Create a device
curl -X POST http://localhost:8080/devices \
-H "Content-Type: application/json" \
-d '{
"name": "web-server-01",
"type": "server",
"ipAddress": "192.168.1.100",
"status": "active",
"port": 443,
"tags": {"role": "web", "datacenter": "us-west-2"},
"labels": {"environment": "production", "team": "platform"}
}'
# List all devices
curl http://localhost:8080/devices
# Get specific device
curl http://localhost:8080/devices/web-server-01
# View OpenAPI documentation
open http://localhost:8080/swagger/π That's it! You now have a fully functional REST API with validation, OpenAPI docs, and structured error handling.
Explore hands-on examples in the examples/ directory:
- Basic CRUD β‘ - Start here! Complete CRUD API in 5 minutes
- FRU Service π - Production patterns with database integration
- CloudEvents Integration π‘ - Automatic event publishing for lifecycle and condition changes
- Rack Reconciliation π - Event-driven resource management
π Learning Path: Start with Example 1 to understand core concepts, try Example 5 for CloudEvents, then advance to Example 3 for production patterns and database integration.
Fabrica follows clean architecture principles and generates well-structured projects:
π Generated Project Structure
βββ π cmd/
β βββ π server/ # π REST API server with all endpoints
β βββ π cli/ # π₯οΈ Command-line client tools
βββ π pkg/
β βββ π resources/ # π Your resource definitions (you write these)
β βββ π client/ # π Generated HTTP client with proper error handling
βββ π internal/
β βββ π storage/ # πΎ Generated storage layer (file or database)
β βββ π middleware/ # βοΈ Generated middleware (auth, validation, etc.)
βββ π docs/ # π Generated OpenAPI specs and documentation
βββ π .fabrica.yaml # βοΈ Project configuration
πͺ Storage Backends:
- π File Backend - JSON files with atomic operations, perfect for development and small datasets
- ποΈ Ent Backend - Type-safe ORM supporting SQLite, PostgreSQL, MySQL for production workloads
β‘ Generated Features:
- β REST handlers with proper HTTP methods, status codes, and content negotiation
- β Comprehensive request/response validation with structured error messages
- β OpenAPI 3.0 specifications with interactive Swagger UI
- β Type-safe HTTP clients with automatic retries and error handling
- β CLI tools for testing, administration, and automation
- β Middleware for authentication, authorization, versioning, and caching
β οΈ IMPORTANT: Code RegenerationFabrica supports regenerating code when you modify your resources or configuration. This means:
β SAFE TO EDIT:
pkg/resources/*/- Your resource definitions (spec/status structs).fabrica.yaml- Project configurationcmd/server/main.go- Server customizations (before first// Generatedcomment)β NEVER EDIT:
- Any file ending in
_generated.go- These are completely regenerated on eachfabrica generate- Files in generated directories after running
fabrica generateπ Regeneration Command:
fabrica generate # Safely regenerates all *_generated.go filesYour custom code in resource definitions and main.go will be preserved, but all generated files will be completely rewritten.
Fabrica uses a Kubernetes-inspired envelope pattern that provides consistent structure across all resources. Every API resource follows this standardized format:
{
"apiVersion": "v1",
"kind": "Device",
"metadata": {
"name": "web-server-01",
"uid": "550e8400-e29b-41d4-a716-446655440000",
"labels": {
"environment": "production",
"team": "platform"
},
"annotations": {
"description": "Primary web server for customer portal"
},
"createdAt": "2025-10-15T10:30:00Z",
"updatedAt": "2025-10-15T14:22:15Z"
},
"spec": {
"type": "server",
"ipAddress": "192.168.1.100",
"status": "active",
"port": 443,
"tags": {"role": "web", "datacenter": "us-west-2"}
},
"status": {
"health": "healthy",
"uptime": 2592000,
"lastChecked": "2025-10-15T14:22:15Z",
"errorCount": 0,
"version": "1.2.3"
}
}| Component | Purpose | Your Code | Generated |
|---|---|---|---|
apiVersion |
API compatibility versioning | β | β Auto-managed |
kind |
Resource type identifier | β | β From struct name |
metadata |
Resource identity & organization | β | β Standard fields |
spec |
Desired state (your data) | β You define | β |
status |
Observed state (runtime info) | β You define | β |
spec struct - The desired configuration/state of your resource:
type DeviceSpec struct {
Type string `json:"type" validate:"required,oneof=server switch router"`
IPAddress string `json:"ipAddress" validate:"required,ip"`
Status string `json:"status" validate:"oneof=active inactive maintenance"`
// ... your business logic fields
}status struct - The observed/runtime state of your resource:
type DeviceStatus struct {
Health string `json:"health" validate:"oneof=healthy degraded unhealthy"`
Uptime int64 `json:"uptime"`
LastChecked time.Time `json:"lastChecked"`
// ... your runtime/monitoring fields
}- π Consistency - All resources follow the same structure regardless of domain
- π·οΈ Rich Metadata - Built-in support for labels, annotations, and timestamps
- π State Separation - Clear distinction between desired (
spec) and observed (status) state - π§ Tooling Integration - Compatible with Kubernetes tooling and patterns
- π Scalability - Proven pattern used by Kubernetes for managing complex systems
π‘ Pro Tip: Focus on designing your
specandstatusstructs - Fabrica handles all the envelope complexity automatically!
π Getting Started:
- Complete Getting Started Guide - Step-by-step tutorial
- Quick Start Examples - Hands-on learning
ποΈ Architecture & Design:
- Architecture Overview - Understanding Fabrica's design principles
- Resource Model Guide - How to design and define resources
πΎ Storage & Data:
- Storage Systems - File vs database backends comparison
- Ent Storage Integration - Database setup and configuration
βοΈ Advanced Topics:
- Code Generation - How templates work and customization
- Validation System - Request validation and error handling
- Event System - CloudEvents integration
- Reconciliation - Controller pattern for resource management
We welcome contributions from the community! Here's how to get involved:
π Report Issues:
π» Code Contributions:
- Fork the repository and create a feature branch
- Write tests for your changes
- Ensure all tests pass:
make test integration - Submit a pull request with a clear description
π¬ Community:
- GitHub Discussions - Ask questions and share ideas
Current Version: v0.3.1
π Recent Updates:
- β Enhanced template system with better error handling
- β Improved integration testing framework
- β Updated documentation and examples
- β Better CI/CD pipeline with comprehensive testing
π Resources:
- π Release Notes - Detailed changelog for each version
- Full Changelog - Complete project history
This project is licensed under the MIT License - see the license file for details.