"We're software engineers. We create worlds. Why wouldn't we try to make them perfect?"
A complete demonstration of specification-driven development using Go, showcasing how to build event-driven systems where the spec is the single source of truth.
This project demonstrates a doc-first development workflow:
- Write specifications first (OpenAPI 3.1 + AsyncAPI 3.0)
- Generate code from specs (custom
synctlgenerator) - Implement to interfaces (handlers, pipeline stages)
- Validate with conformance tests (responses must match specs)
# Clone the repo
git clone https://github.com/copyleftdev/synapse-spec-first.git
cd synapse-spec-first
# One-time setup (downloads deps + generates code)
make setup
# Run all tests
make test
# Start the server
make runThis project includes a comprehensive Makefile for a pleasant developer experience:
make help # Show all available targets| Command | Description |
|---|---|
make setup |
One-time setup for new clones |
make generate |
Regenerate code from specs |
make test |
Run all tests (requires Docker) |
make test-short |
Run fast tests (no Docker) |
make run |
Start the server |
| Command | Description |
|---|---|
make test-conformance |
Run OpenAPI/AsyncAPI conformance tests |
make test-pipeline |
Run pipeline integration tests |
make coverage |
Generate coverage report |
make benchmark |
Run benchmarks |
| Command | Description |
|---|---|
make lint |
Format and vet code |
make build |
Build the synapse binary |
make diagrams |
Generate architecture diagrams |
make validate-specs |
Validate OpenAPI/AsyncAPI specs |
make clean |
Remove build artifacts |
| Command | Description |
|---|---|
make dev |
generate β test-short β run |
make ci |
deps β generate β lint β test |
make all |
Full build pipeline |
Traditional development: Write code β Document later (maybe)
Doc-first development: Write spec β Generate code β Implement β Prove conformance
Read the full article: ARTICLE.md
- API Layer: Chi router with generated interfaces
- Event Bus: NATS JetStream via Watermill
- Pipeline: Validate β Enrich β Route stages
- Storage: PostgreSQL for persistence, Redis for caching
- Testing: Testcontainers for real infrastructure
synapse/
βββ asyncapi/ # AsyncAPI 3.0 event specifications
βββ openapi/ # OpenAPI 3.1 REST specifications
βββ cmd/
β βββ synapse/ # Application entry point
β βββ synctl/ # Custom code generator
βββ internal/
β βββ generated/ # Generated from specs
β βββ handler/ # HTTP handlers
β βββ pipeline/ # Watermill event pipeline
β βββ conformance/ # Contract testing
β βββ testutil/ # Testcontainers helpers
βββ scripts/ # Diagram generation
// Validate HTTP responses against OpenAPI schema
result := suite.RunTest(ctx, client, baseURL,
"GET", "/health",
nil,
http.StatusOK,
"HealthResponse", // Must match this schema
)
// Validate events against AsyncAPI schema
result := suite.ValidateEvent(
"orders/ingest",
"OrderReceivedPayload",
orderJSON,
)# Unit tests (fast)
go test ./... -short
# Integration tests (requires Docker)
go test ./... -v
# Conformance tests only
go test ./internal/conformance/... -vThe custom synctl generator creates:
| File | Contents |
|---|---|
types.gen.go |
31 Go structs from OpenAPI + AsyncAPI schemas |
server.gen.go |
HTTP interface with all endpoint methods |
client.gen.go |
Typed HTTP client with auth |
events.gen.go |
Watermill publishers + handlers |
# Regenerate after spec changes
go run ./cmd/synctlGenerated using Python's diagrams library:
cd scripts
./venv/bin/python generate_all.py| Diagram | Description |
|---|---|
| architecture.png | System architecture |
| doc_first_lifecycle.png | Development workflow |
| pipeline_stages.png | Event processing |
| testing_strategy.png | Testing approach |
| philosophy.png | Core principles |
- Go 1.21+ β Application language
- Chi β HTTP router
- Watermill β Event-driven processing
- NATS β Message broker
- PostgreSQL β Persistence
- Redis β Caching
- Testcontainers β Integration testing
- OpenAPI 3.1 β REST API specification
- AsyncAPI 3.0 β Event specification
- OpenAPI Initiative β REST API specification standard
- AsyncAPI Initiative β Event-driven API specification
- Testcontainers β Real infrastructure in tests
- Three Dots Labs β Watermill event library
- NATS.io β High-performance messaging
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Built to demonstrate that "the perfect world" is the one we choose to create.




