Build Go REST API with Chi Router
backend
Go
scaffolding
strict_senior
Create production-ready Go REST API with Chi router, middleware, and clean architecture.
By sophia_m
12/8/2025
Prompt
Build a complete Go REST API for [APPLICATION_NAME] using Chi router with the following specifications:
Requirements
- Application name: [APPLICATION_NAME]
- API version: [v1/v2]
- Resource endpoints: [RESOURCE_1], [RESOURCE_2], [RESOURCE_3]
- CRUD operations needed: [CREATE/READ/UPDATE/DELETE]
- Authentication: [JWT/API Key/OAuth]
- Middleware needed: [CORS/Logging/Rate Limiting/Auth]
- Database: [PostgreSQL/MySQL/MongoDB]
- Port: [PORT_NUMBER]
- Request validation: [YES/NO]
- Response format: [JSON/XML]
- Error handling strategy: [Standard/Custom]
Deliverables
Generate complete Go Chi API code with:
Main server setup (main.go):
- Package imports (chi/v5, middleware)
- Chi router initialization
- Global middleware stack
- Route registration
- HTTP server startup on [PORT]
- Graceful shutdown handling
Router configuration:
- Chi NewRouter() setup
- Built-in middleware (Logger, Recoverer, RequestID)
- Custom middleware registration
- CORS configuration if needed
- Timeout middleware
- Compressor for gzip
Resource routes for each [RESOURCE]:
- Grouped routes with r.Route("/api/[VERSION]/[RESOURCE]")
- GET / - List all with pagination
- POST / - Create new
- GET /{id} - Get by ID
- PUT /{id} - Update by ID
- DELETE /{id} - Delete by ID
- Nested routes if needed
Handler functions for each endpoint:
- Handler signature: func(w http.ResponseWriter, r *http.Request)
- URL parameter extraction with chi.URLParam()
- Request body parsing and validation
- Business logic execution
- Response writing with proper status codes
- Error handling
Middleware implementations:
- Authentication middleware for [AUTH_TYPE]
- Token/API key validation
- Context injection for user info
- 401 responses for invalid auth
- CORS middleware if needed
- Allowed origins, methods, headers
- Preflight handling
- Rate limiting middleware if needed
- Request logging with duration
Request/Response models (structs):
- Request DTOs for each endpoint
- Response DTOs with proper JSON tags
- Error response structure
- Validation tags if using validator library
Database integration:
- Database connection setup for [DATABASE]
- Repository pattern for data access
- CRUD operations for each [RESOURCE]
- Connection pooling
- Query error handling
Validation:
- Request body validation
- URL parameter validation
- Custom validation rules
- Validation error responses
Error handling:
- HTTP error wrapper functions
- Consistent error response format
- Error logging
- Panic recovery
JSON utilities:
- Response helper functions (WriteJSON, WriteError)
- Request parser functions
- Content-Type validation
Health check endpoint:
- GET /health for liveness probe
- GET /ready for readiness probe
- Database connection check
Project structure:
- main.go - Server setup
- handlers/ - HTTP handlers
- middleware/ - Custom middleware
- models/ - Data models
- repository/ - Database layer
- utils/ - Helper functions
Output production-ready Go Chi API with clean architecture.
Tags
go
chi
router
api
Tested Models
gpt-4
claude-3-opus