Examples showcasing the server library features and best practices.
A simple example showcasing basic API implementation with the server library.
cd cmd/hello
make build
make runFeatures Demonstrated:
- Basic API registration
- HTTP routing with chi
- JSON responses
- Health checks and version endpoints
Comprehensive example demonstrating advanced server features including configuration registry, lifecycle management, and graceful shutdown.
cd cmd/lifecycle
make build
make runFeatures Demonstrated:
- Configuration Registry - Self-registering configuration with validation
- API Lifecycle Management - Graceful startup and shutdown hooks
- Enhanced Error Context - Correlation ID tracking and structured errors
- Database Service - Mock database with connection management
- Background Worker - Job processing with lifecycle hooks
- Graceful Shutdown - SIGTERM/SIGINT handling with resource cleanup
Environment Configuration:
# Database settings
DATABASE_URL=mock://localhost:5432/testdb
DATABASE_MAX_CONNECTIONS=10
DATABASE_CONNECT_TIMEOUT=5s
# Worker settings
WORKER_INTERVAL=30s
WORKER_MAX_JOBS=100
WORKER_ENABLE_PROCESSING=trueAPI Endpoints:
GET /api/database/users- List usersPOST /api/database/users- Create userGET /api/database/health- Database health checkGET /api/worker/status- Worker status and metricsPOST /api/worker/jobs- Create background jobGET /api/worker/jobs- List jobs
Testing the APIs:
make test-apisCustom Configuration:
make run-with-configEach example includes its own Makefile with common targets:
make build # Build the example
make run # Run the server
make clean # Remove build artifacts
make help # Show available targetsexamples/
├── cmd/
│ ├── hello/ # Basic HTTP server example
│ └── lifecycle/ # Advanced lifecycle example
└── internal/
├── build/ # Build information
└── service/ # Service implementations
├── hello/ # Basic service
├── database/ # Database service with lifecycle
└── worker/ # Background worker service
-
Basic Example:
cd cmd/hello && make run
-
Advanced Example:
cd cmd/lifecycle && make run
-
Test APIs:
# In another terminal curl http://localhost:8080/version curl http://localhost:8080/healthz
Run the lifecycle example and press Ctrl+C to see graceful shutdown in action:
cd cmd/lifecycle && make run
# Press Ctrl+C to trigger graceful shutdownYou'll see:
- Configuration loading and validation
- Database connection establishment
- Background worker startup
- Server accepting requests
- Graceful shutdown on SIGTERM
- Background worker stopping
- Database connections closing
- Existing requests completing