A sophisticated and professional Todo REST API built with Go, featuring clean architecture, comprehensive documentation, and production-ready features.
- β Clean Architecture - Layered architecture with clear separation of concerns
- β RESTful API - Standard HTTP methods and status codes
- β Database Support - SQLite (development) and PostgreSQL (production)
- β GORM ORM - Type-safe database operations with auto-migration
- β UUID Primary Keys - Secure and globally unique identifiers
- β Input Validation - Comprehensive request validation
- β Structured Logging - JSON-formatted logs with zerolog
- β CORS Support - Cross-origin resource sharing configuration
- β Request ID Tracking - Request tracing and debugging
- β Health Checks - Application health monitoring
- β Prometheus Metrics - Performance and business metrics
- β Swagger Documentation - Auto-generated API documentation
- β Graceful Shutdown - Proper server termination
- β Environment Configuration - Flexible configuration management
- β Pagination - Efficient data retrieval with metadata
- β Soft Deletes - Data preservation with logical deletion
- Framework: Gin - High-performance HTTP web framework
- ORM: GORM - Object-relational mapping library
- Logging: Zerolog - Structured JSON logging
- Validation: Validator - Request validation
- Documentation: Swagger - API documentation
- Database: SQLite (dev) / PostgreSQL (prod)
- Monitoring: Prometheus metrics
- Go 1.21 or higher
- Git
- SQLite (for development)
- PostgreSQL (for production, optional)
git clone https://github.com/1cbyc/go-todo-api.git
cd go-todo-api
go mod tidy
Create a .env
file in the root directory:
# Server Configuration
PORT=8080
GIN_MODE=debug
LOG_LEVEL=info
# Database Configuration
DB_DRIVER=sqlite
DB_NAME=todo_api
# For PostgreSQL (production)
# DB_DRIVER=postgres
# DB_HOST=localhost
# DB_PORT=5432
# DB_USER=postgres
# DB_PASSWORD=password
# DB_NAME=todo_api
# DB_SSLMODE=disable
go run cmd/api/main.go
The API will be available at https://localhost:8080
Once the server is running, visit:
- Swagger UI: https://localhost:8080/swagger/index.html
- Health Check: https://localhost:8080/health
- Metrics: https://localhost:8080/api/v1/metrics
Method | Endpoint | Description |
---|---|---|
GET |
/api/v1/todos |
List all todos with pagination |
GET |
/api/v1/todos/:id |
Get a specific todo |
POST |
/api/v1/todos |
Create a new todo |
PUT |
/api/v1/todos/:id |
Update a todo |
DELETE |
/api/v1/todos/:id |
Delete a todo |
PATCH |
/api/v1/todos/:id/toggle |
Toggle todo completion |
Method | Endpoint | Description |
---|---|---|
GET |
/health |
Health check |
GET |
/api/v1/metrics |
Prometheus metrics |
GET |
/swagger/* |
API documentation |
curl -X POST https://localhost:8080/api/v1/todos \
-H "Content-Type: application/json" \
-d '{
"title": "Learn Go",
"description": "Study Go programming language",
"priority": "high",
"due_date": "2024-12-31T23:59:59Z"
}'
curl -X GET "https://localhost:8080/api/v1/todos?page=1&per_page=10"
curl -X PUT https://localhost:8080/api/v1/todos/{id} \
-H "Content-Type: application/json" \
-d '{
"title": "Learn Go Programming",
"completed": true
}'
curl -X PATCH https://localhost:8080/api/v1/todos/{id}/toggle
The application uses SQLite by default for development. The database file will be created automatically as todo_api.db
.
To use PostgreSQL in production:
- Install PostgreSQL
- Create a database
- Update environment variables:
DB_DRIVER=postgres
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=todo_api
DB_SSLMODE=disable
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests with verbose output
go test -v ./...
# Run only unit tests
go test ./internal/services/...
# Run only integration tests
go test ./internal/repository/...
# Run benchmarks
go test -bench=. ./...
docker build -t todo-api .
docker run -p 8080:8080 todo-api
Create a docker-compose.yml
file:
version: '3.8'
services:
api:
build: .
ports:
- "8080:8080"
environment:
- DB_DRIVER=sqlite
- DB_NAME=todo_api
volumes:
- ./data:/app/data
# For PostgreSQL
postgres:
image: postgres:15
environment:
POSTGRES_DB: todo_api
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Run with:
docker-compose up -d
curl https://localhost:8080/health
Response:
{
"status": "ok",
"message": "Todo API is running",
"version": "1.0.0",
"timestamp": "2024-01-01T00:00:00Z",
"uptime": "1h2m3s",
"memory": {
"alloc": 1234567,
"total_alloc": 2345678,
"sys": 3456789,
"num_gc": 5
},
"goroutines": 10
}
curl https://localhost:8080/api/v1/metrics
Variable | Default | Description |
---|---|---|
PORT |
8080 |
Server port |
GIN_MODE |
debug |
Gin mode (debug/release) |
LOG_LEVEL |
info |
Log level (debug/info/warn/error) |
DB_DRIVER |
sqlite |
Database driver (sqlite/postgres) |
DB_HOST |
localhost |
Database host |
DB_PORT |
5432 |
Database port |
DB_USER |
postgres |
Database user |
DB_PASSWORD |
`` | Database password |
DB_NAME |
todo_api |
Database name |
DB_SSLMODE |
disable |
Database SSL mode |
# Build for production
go build -o bin/api cmd/api/main.go
# Run production binary
./bin/api
For production, set:
GIN_MODE=release
LOG_LEVEL=info
DB_DRIVER=postgres
# ... other production settings
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Follow Go coding standards and conventions
- Write comprehensive tests for new features
- Update documentation for API changes
- Use conventional commit messages
- Ensure all tests pass before submitting PRs
This project is licensed under the MIT License - see the LICENSE file for details.
- Technical Explanation - Detailed architecture and design decisions
- What's Next - Development roadmap and future features
- API Documentation - Interactive API docs
If you encounter any issues or have questions:
- Check the documentation
- Search existing issues
- Create a new issue with detailed information
Happy coding! π