Skip to content

SWITCHin2/leetcode-wrap-be

Repository files navigation

LeetCode Wrap API 🚀

A production-ready Go API service that fetches and processes LeetCode user statistics with rate limiting, user tracking, and comprehensive monitoring.

📁 Project Structure

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

✨ Features

  • 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

🚀 Quick Start

Local Development

  1. Clone the repository
git clone <repository-url>
cd leetcode-wrap
  1. Install dependencies
go mod download
  1. Run the application
go run main.go

The server will start on http://localhost:8080

Using Docker

  1. Build and run with Docker Compose
docker-compose up --build
  1. Or build and run manually
docker build -t leetcode-wrap .
docker run -p 8080:8080 leetcode-wrap

📡 API Endpoints

Health Check

GET /health

Returns the health status of the service.

Get User Wrap

GET /api/v1/wrap/{username}

Fetches LeetCode statistics for a specific user.

Example:

curl http://localhost:8080/api/v1/wrap/john_doe

Response:

{
  "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 Statistics

GET /api/v1/stats

Returns overall API usage statistics.

Response:

{
  "total_requests": 1000,
  "unique_users": 250
}

Get Top Users

GET /api/v1/stats/top

Returns the top 10 most queried users.

⚙️ Configuration

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

🧪 Testing

# Run tests
go test ./...

# Run tests with coverage
go test -cover ./...

# Run tests with verbose output
go test -v ./...

📦 Building

# 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-wrap

🐳 Docker

The 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)

📊 Monitoring

The application includes:

  • Health check endpoint at /health
  • Request logging with duration tracking
  • Rate limit tracking
  • User statistics tracking

🔒 Security

  • Rate limiting to prevent abuse
  • Non-root Docker container
  • Input validation
  • CORS configuration
  • No sensitive data logging

🛠️ Development

Adding New Endpoints

  1. Create handler in api/handler/
  2. Add route in api/routes/routes.go
  3. Update README with new endpoint

Project Guidelines

  • 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

📝 License

MIT License - feel free to use this project for learning or production!

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published