A production-ready Go API service that fetches and processes LeetCode user statistics with rate limiting, user tracking, and comprehensive monitoring.
leetcode-wrap/
├── api/
│ ├── handler/ # HTTP request handlers
│ │ ├── wrap.go # Main wrap endpoint handler
│ │ ├── stats.go # Statistics endpoint handler
│ │ └── health.go # Health check handler
│ ├── middleware/ # HTTP middleware
│ │ ├── ratelimit.go # Rate limiting middleware
│ │ └── logging.go # Logging & CORS middleware
│ └── routes/ # Route definitions
│ └── routes.go # All route configurations
├── bean/ # Data models and constants
│ ├── constants.go # Application constants
│ ├── models.go # Data structures
│ └── config.go # Configuration management
├── pkg/ # Reusable packages
│ ├── leetcode/ # LeetCode API client
│ │ └── client.go
│ ├── ratelimiter/ # Rate limiting implementation
│ │ └── ratelimiter.go
│ └── tracker/ # User tracking
│ └── tracker.go
├── main.go # Application entry point
├── Dockerfile # Multi-stage Docker build
├── docker-compose.yml # Docker Compose configuration
└── README.md # This file
- Rate Limiting: IP-based rate limiting with configurable limits
- User Tracking: Track total requests, unique users, and per-user statistics
- Health Checks: Built-in health check endpoint for monitoring
- Statistics: Real-time statistics about API usage
- CORS Support: Cross-origin resource sharing enabled
- Logging: Request logging with duration tracking
- Docker Support: Production-ready Docker configuration
- Clean Architecture: Organized code structure following Go best practices
- Clone the repository
git clone <repository-url>
cd leetcode-wrap- Install dependencies
go mod download- Run the application
go run main.goThe server will start on http://localhost:8080
- Build and run with Docker Compose
docker-compose up --build- Or build and run manually
docker build -t leetcode-wrap .
docker run -p 8080:8080 leetcode-wrapGET /healthReturns the health status of the service.
GET /api/v1/wrap/{username}Fetches LeetCode statistics for a specific user.
Example:
curl http://localhost:8080/api/v1/wrap/john_doeResponse:
{
"username": "john_doe",
"total_submissions_this_year": 150,
"active_days_this_year": 45,
"difficulty_breakdown_all_time": {
"Easy": 50,
"Medium": 30,
"Hard": 10
},
"favorite_topic": "Dynamic Programming"
}GET /api/v1/statsReturns overall API usage statistics.
Response:
{
"total_requests": 1000,
"unique_users": 250
}GET /api/v1/stats/topReturns the top 10 most queried users.
Configure the application using environment variables:
| Variable | Description | Default |
|---|---|---|
PORT |
Server port | 8080 |
LEETCODE_ENDPOINT |
LeetCode GraphQL endpoint | https://leetcode.com/graphql |
RATE_LIMIT_REQUESTS |
Requests allowed per window | 10 |
RATE_LIMIT_WINDOW |
Time window in seconds | 60 |
RATE_LIMIT_BURST |
Maximum burst size | 5 |
Example:
export PORT=3000
export RATE_LIMIT_REQUESTS=20
go run main.go# Run tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests with verbose output
go test -v ./...# Build for current platform
go build -o leetcode-wrap
# Build for Linux
GOOS=linux GOARCH=amd64 go build -o leetcode-wrap
# Build with optimizations
go build -ldflags="-w -s" -o leetcode-wrapThe project includes a multi-stage Dockerfile for optimal image size and security:
- Stage 1: Builds the Go binary
- Stage 2: Creates a minimal Alpine-based runtime image
Features:
- Non-root user execution
- Health checks
- Minimal attack surface
- Small image size (~15MB)
The application includes:
- Health check endpoint at
/health - Request logging with duration tracking
- Rate limit tracking
- User statistics tracking
- Rate limiting to prevent abuse
- Non-root Docker container
- Input validation
- CORS configuration
- No sensitive data logging
- Create handler in
api/handler/ - Add route in
api/routes/routes.go - Update README with new endpoint
- Follow Go best practices
- Keep handlers thin, business logic in packages
- Use constants from
bean/constants.go - Add appropriate error handling
- Update tests for new features
MIT License - feel free to use this project for learning or production!
Contributions are welcome! Please feel free to submit a Pull Request.