A high-performance student enrollment management API built with Go, featuring Redis caching for sub-100ms response times and comprehensive API contract validation.
- β Complete CRUD Operations - Create, Read, Update, Delete enrollments
- β‘ Redis Caching - 5-minute TTL with cache-aside pattern
- π X-Cache-Status Headers - Debug cache hits/misses in real-time
- π‘οΈ API Contract Validation - OpenAPI 3.0 spec with automated validation
- π§ͺ Integration Test Suite - 100% pass rate with performance assertions
- π Graceful Degradation - Works with or without Redis
- π³ Docker Ready - Containerized Redis setup
- Go 1.22.5 or higher
- Docker & Docker Compose (for Redis)
- Git
git clone https://github.com/CanarysPlayground/Techwavegrademanagement.git
cd Techwavegrademanagement
go mod downloaddocker-compose up -dgo run main.goServer starts on http://localhost:8080
# Create an enrollment
Invoke-RestMethod -Uri http://localhost:8080/api/enrollments -Method Post -Headers @{"Content-Type"="application/json"} -Body '{"student_id":"42","course_id":"101","status":"pending"}'
# Get all enrollments
Invoke-RestMethod -Uri http://localhost:8080/api/enrollments
# Get specific enrollment (check X-Cache-Status header)
$response = Invoke-WebRequest -Uri "http://localhost:8080/api/enrollments/<enrollment-id>"
$response.Headers["X-Cache-Status"] # Returns HIT or MISS| Method | Endpoint | Description | Cache Behavior |
|---|---|---|---|
| GET | / |
Root endpoint | N/A |
| GET | /health |
Health check | N/A |
| POST | /api/enrollments |
Create enrollment | No cache |
| GET | /api/enrollments |
List all enrollments | No cache |
| GET | /api/enrollments/{id} |
Get enrollment | Cached (5 min TTL) |
| PUT | /api/enrollments/{id} |
Update enrollment | Invalidates cache |
| DELETE | /api/enrollments/{id} |
Delete enrollment | Invalidates cache |
See the complete OpenAPI specification in api/openapi.yaml for detailed schemas and examples.
Validates API implementation against OpenAPI spec:
go run -tags contract scripts/validate_contract.goExpected Output:
β OpenAPI specification is valid
β Route validated: GET http://localhost:8080/
β Route validated: POST http://localhost:8080/api/enrollments
... (all routes validated)
β
CONTRACT VALIDATION PASSED: All checks successful
Comprehensive test suite with cache behavior validation:
go test -tags integration -v ./tests/integration_test.goTest Coverage:
- β Complete CRUD workflow
- β Cache hit/miss/invalidation behavior
- β Performance assertions (<100ms cached responses)
- β Validation error handling
- β 404 error scenarios
- β Response schema validation
Expected Results:
=== RUN TestCompleteCRUDWorkflow
--- PASS: TestCompleteCRUDWorkflow (0.02s)
=== RUN TestCachePerformance
β Cached response time: 1 ms
--- PASS: TestCachePerformance (0.01s)
... (all tests passing)
PASS
βββ main.go # Application entry point with Redis setup
βββ api/
β βββ openapi.yaml # OpenAPI 3.0 specification
βββ cache/
β βββ enrollment_cache.go # Redis caching layer (5-min TTL)
βββ handlers/
β βββ enrollment_handler.go # HTTP request handlers with cache integration
βββ models/
β βββ enrollment.go # Enrollment data model and validation
βββ repository/
β βββ enrollment_repository.go # In-memory data storage
βββ middleware/
β βββ cache_middleware.go # X-Cache-Status header middleware
βββ scripts/
β βββ validate_contract.go # Contract validation script
βββ tests/
βββ integration_test.go # Integration test suite
Cache-Aside Pattern:
- Check cache on GET requests
- On cache MISS: fetch from DB, store in cache
- On cache HIT: return cached data (<100ms)
- Invalidate cache on UPDATE/DELETE operations
Cache Headers:
X-Cache-Status: HIT- Served from Redis cacheX-Cache-Status: MISS- Fetched from database and cachedX-Cache-Status: SKIP- Caching disabled/not applicable
Environment variables:
REDIS_ADDR=localhost:6379 # Redis server address (default: localhost:6379)
REDIS_PASSWORD= # Redis password (optional)name: API Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
redis:
image: redis:7-alpine
ports:
- 6379:6379
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.22.5'
- name: Install dependencies
run: go mod download
- name: Contract validation
run: go run -tags contract scripts/validate_contract.go
- name: Integration tests
run: go test -tags integration -v ./tests/integration_test.go| Operation | Response Time | Cache Status |
|---|---|---|
| Create Enrollment | ~5-10ms | N/A |
| Get Enrollment (cached) | <1ms | HIT |
| Get Enrollment (uncached) | ~5-10ms | MISS |
| Update Enrollment | ~5-10ms | Invalidates |
| Delete Enrollment | ~5-10ms | Invalidates |
-
Feature Development
- Update api/openapi.yaml with new endpoints
- Implement handlers with cache support
- Run contract validation
- Write integration tests
-
Testing
# Validate contract go run -tags contract scripts/validate_contract.go # Run tests go test -tags integration -v ./tests/integration_test.go
-
Commit
git add . git commit -m "feat: Add new endpoint with caching" git push
Full API documentation is available in api/openapi.yaml. You can view it using:
- Swagger Editor - Paste the YAML content
- Redoc - For beautiful docs rendering
# Check Redis is running
docker ps | grep redis
# View Redis logs
docker-compose logs redis
# Restart Redis
docker-compose restart redis- Check
X-Cache-Statusheader in responses - Verify Redis connection in server logs:
β Redis connection established - API works in degraded mode without Redis (cache disabled)
MIT License - See LICENSE file for details
TEC-17: β Redis caching implementation - COMPLETE TEC-18: β API contract validation & test suite - COMPLETE
- β Complete CRUD API for enrollments
- β Redis caching with 5-minute TTL
- β Cache invalidation on UPDATE/DELETE
- β X-Cache-Status headers for debugging
- β OpenAPI 3.0 specification
- β Automated contract validation
- β Comprehensive integration tests
- β Performance benchmarks (<100ms cached)
- β 100% test pass rate
Built with GitHub Copilot π€ | Powered by Redis β‘ | Validated by OpenAPI π‘οΈ