A high-performance, scalable backend system for tracking API usage with advanced caching, rate limiting, and JWT authentication built with Go, Echo, GORM, PostgreSQL, and Redis.
- RESTful API Design - Clean, intuitive API endpoints
- Client Management - Register and manage API clients
- Activity Logging - Track all API hits with detailed metadata
- Usage Analytics - Daily usage reports and top client statistics
- JWT Authentication - Secure token-based authentication
- API Key Management - Generate and validate API keys
- Redis Caching - High-performance caching with TTL and invalidation
- Rate Limiting - Per-client hourly rate limits (default: 1000 req/hour)
- Database Optimization - Indexed queries, batch operations
- Graceful Degradation - Fallback when Redis is unavailable
- Docker Support - Containerized for easy deployment
- Go 1.21 or higher
- PostgreSQL 12+
- Redis 6+
- Clone the repository
git clone https://github.com/iniakunhuda/nexmedis-golang
cd nexmedis-golang- Install dependencies
go mod download- Configure environment
cp .env.example .env
# Edit .env with your configuration- Run with Docker Compose (Recommended)
docker-compose up -d- Or run locally
# Start PostgreSQL and Redis first
go run main.goPOST /api/register
Content-Type: application/json
{
"name": "Huda",
"email": "huda@gmail.com"
}Response:
{
"success": true,
"message": "Client registered successfully",
"data": {
"id": "uuid",
"client_id": "client_abc12345",
"name": "Huda",
"email": "huda@gmail.com",
"api_key": "generated-api-key",
"created_at": "2025-01-01T00:00:00Z"
}
}POST /api/login
Content-Type: application/json
{
"api_key": "your-api-key"
}Response:
{
"success": true,
"message": "Login successful",
"data": {
"token": "jwt-token",
"client_id": "client_abc12345",
"expires_in": "24h"
}
}POST /api/logs
Content-Type: application/json
{
"api_key": "your-api-key",
"ip": "192.168.1.1",
"endpoint": "/api/some-endpoint"
}Add JWT token to all protected endpoints:
Authorization: Bearer your-jwt-tokenGET /api/usage/dailyResponse:
{
"success": true,
"message": "Daily usage retrieved successfully",
"data": [
{
"client_id": "uuid",
"client_name": "John Doe",
"date": "2025-01-15",
"count": 150
}
]
}GET /api/usage/topResponse:
{
"success": true,
"message": "Top clients retrieved successfully",
"data": [
{
"client_id": "uuid",
"client_name": "John Doe",
"email": "john@example.com",
"total_requests": 500
}
]
}GET /api/usage/statsGET /api/usage/client/:client_id# Server
SERVER_PORT=8080
SERVER_HOST=0.0.0.0
# Database
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=api_tracking
DB_SSL_MODE=disable
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DB=0
# JWT
JWT_SECRET=your-secret-key
JWT_EXPIRATION=24h
# Rate Limiting
RATE_LIMIT_PER_HOUR=1000
# Cache
CACHE_TTL=3600nexmedis-golang/
βββ db/ # Database connections
β βββ database.go # PostgreSQL setup
β βββ redis.go # Redis setup
βββ handler/ # HTTP handlers
β βββ auth_handler.go
β βββ client_handler.go
β βββ log_handler.go
β βββ usage_handler.go
βββ model/ # Data models
β βββ client.go
β βββ log.go
β βββ request.go
βββ router/ # Routes and middleware
β βββ middleware.go
β βββ router.go
βββ store/ # Data access layer
β βββ client_store.go
β βββ log_store.go
βββ utils/ # Utilities
β βββ crypto.go
β βββ jwt.go
β βββ rate_limiter.go
β βββ response.go
β βββ validator.go
βββ main.go # Entry point
βββ Dockerfile
βββ docker-compose.yml
βββ README.md
- Framework: Echo v4 (High-performance HTTP framework)
- ORM: GORM (Database abstraction)
- Database: PostgreSQL (Primary data store)
- Cache: Redis (Caching & Pub/Sub)
- Authentication: JWT (golang-jwt/jwt/v5)
- Containerization: Docker & Docker Compose
- JWT Authentication - Secure token-based auth for protected endpoints
- API Key Validation - Cryptographic API key generation and validation
- Rate Limiting - Per-client hourly rate limits
- Input Validation - Comprehensive request validation
- SQL Injection Protection - Parameterized queries via GORM
- Security Headers - CORS, XSS, Content-Type protection
-
Redis Caching
- 1-hour TTL for usage endpoints
- Cache invalidation on data updates
- Graceful fallback to database
-
Database Optimizations
- Composite indexes on frequently queried fields
- Batch insert operations for logs
- Connection pooling (max 100 connections)
Not yet implemented
- Environment Variables: Use secrets management
- Database: Enable SSL, use read replicas
- Redis: Enable persistence, use Redis Cluster
- Monitoring: Add Prometheus metrics
- Logging: Structured logging with log aggregation
- Load Balancing: Use Nginx or cloud load balancer
- HTTPS: Enable TLS/SSL certificates
CREATE TABLE clients (
id UUID PRIMARY KEY,
client_id VARCHAR UNIQUE NOT NULL,
name VARCHAR NOT NULL,
email VARCHAR UNIQUE NOT NULL,
api_key VARCHAR UNIQUE NOT NULL,
created_at TIMESTAMP,
updated_at TIMESTAMP,
deleted_at TIMESTAMP
);CREATE TABLE api_logs (
id UUID PRIMARY KEY,
client_id UUID REFERENCES clients(id),
api_key VARCHAR NOT NULL,
ip VARCHAR NOT NULL,
endpoint VARCHAR NOT NULL,
timestamp TIMESTAMP NOT NULL,
created_at TIMESTAMP
);
CREATE INDEX idx_api_logs_client_timestamp ON api_logs(client_id, timestamp DESC);
CREATE INDEX idx_api_logs_timestamp ON api_logs(timestamp DESC);
CREATE INDEX idx_api_logs_endpoint ON api_logs(endpoint);- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
This project is licensed under the MIT License.
For issues and questions, please open an issue on GitHub.
Built with β€οΈ by Huda