A well-architected, feature-complete REST API built with Go and Gin framework for managing a complete ecommerce platform. This API provides comprehensive features including user authentication, product management, shopping cart, order processing, payment handling framework, and advanced admin capabilities.
This API is designed with production-grade architecture and includes comprehensive testing, security features, monitoring, and performance optimizations. All formatting and linting checks pass successfully.
- β User Authentication - JWT-based secure authentication with role management (admin/customer)
- β Product Catalog - Full CRUD operations with search and categorization
- β Shopping Cart - Real-time cart management with stock validation
- β Order Management - Complete order lifecycle from placement to fulfillment
- β Payment Processing - Basic payment handling and status tracking (ready for gateway integration)
- β Review System - Product reviews and ratings with user validation
- β Wishlist - Save products for later purchase
- β Address Management - Multiple shipping addresses per user
- π Enterprise Security - JWT auth, input validation, rate limiting, CORS
- π Admin Reports - Sales analytics and inventory reports for admin users
- π Performance Optimized - Database connection pooling, query optimization
- π Monitoring & Metrics - Health checks, system metrics, and detailed monitoring
- π‘οΈ Rate Limiting - Configurable rate limits for different endpoint types
- π Structured Logging - Comprehensive logging with multiple output formats
- π§ͺ High Test Coverage - Extensive test suite with good coverage across packages
- ποΈ Caching Layer - In-memory and Redis caching for improved performance
- Clean Architecture - Well-structured codebase with separation of concerns
- Database Optimization - Efficient queries and proper indexing
- Caching Layer - In-memory and Redis caching support
- Middleware Stack - CORS, compression, logging, and security middlewares
- Error Handling - Standardized error responses with proper HTTP status codes
- Input Validation - Comprehensive validation and sanitization
- Core ecommerce functionality fully implemented
- Authentication and authorization system
- Database operations with proper connection pooling
- Comprehensive test coverage across most packages
- Health monitoring and metrics collection
- Security middlewares and rate limiting
- Payment Gateway Integration - Currently has payment processing framework, ready for Stripe/PayPal integration
- Email Notifications - SMTP configuration ready, notification templates to be added
- File Upload - Basic file handling implemented, image processing features planned
- Advanced Analytics - Basic reports available, advanced dashboard features planned
- Internationalization - Single language support currently, i18n framework planned
- Overall Project: Good coverage across most packages
- Middlewares: 89.1% - Excellent coverage
- Utils: 95.5% - Excellent coverage
- Config: 100% - Complete coverage
- Handlers: Comprehensive test coverage for all endpoints
- Services: Well tested business logic layer
Note: Test coverage percentages may vary as new features are added and tests are enhanced.
- π Features
- π§ Prerequisites
- π¦ Installation
- βοΈ Configuration
- π Security Features
- π Monitoring & Health Checks
- π‘οΈ Rate Limiting
- π Logging
- π§ͺ Testing
- π API Documentation
- π Performance
- ποΈ Architecture
- π€ Contributing
- π License
Before you begin, ensure you have the following installed on your system:
- Go 1.22+ - Download & Install Go
- PostgreSQL 12+ - Download PostgreSQL or use Docker
- Git - For cloning the repository
- Postman (Optional) - For API testing
- Docker (Optional) - For containerized deployment
- Memory: Minimum 2GB RAM (4GB recommended)
- Storage: At least 1GB free space
- CPU: Any modern processor (x86_64 or ARM64)
- Clone the repository:
git clone https://github.com/geoo115/Ecommerce-api.git
cd Ecommerce-api- Install dependencies:
go mod tidy- Set up your database:
-- Connect to PostgreSQL and create database
CREATE DATABASE ecommerce;
CREATE USER ecommerce_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE ecommerce TO ecommerce_user;- Configure environment variables:
cp env.example .env
# Edit .env with your configuration (see Configuration section below)- Run database migrations (if applicable):
# The application will auto-migrate on startup
go run main.go- Start the server:
go run main.go- Verify installation:
# Test health check endpoint
curl http://localhost:8080/health
# Expected response:
# {"success":true,"message":"Health check passed","data":{"status":"healthy","timestamp":"..."},"code":200}# Clone the repository
git clone https://github.com/geoo115/Ecommerce-api.git
cd Ecommerce-api
# Build and run with Docker (Docker files to be added)
# docker-compose up -dNote: Docker configuration is planned for future releases. Currently supports direct installation.
# Clone and setup for development
git clone https://github.com/geoo115/Ecommerce-api.git
cd Ecommerce-api
# Install development dependencies
go mod tidy
# Install additional tools
go install github.com/air-verse/air@latest # For hot reloading
# Run in development mode with hot reload
airThe application uses environment variables for configuration. Create a .env file based on env.example:
# Server Configuration
PORT=8080 # Server port (default: 8080)
HOST=localhost # Server host (default: localhost)
ENV=development # Environment: development, staging, production
# Database Configuration
DATABASE_HOST=localhost # PostgreSQL host
DATABASE_PORT=5432 # PostgreSQL port
DATABASE_USER=ecommerce_user # Database username
DATABASE_PASSWORD=secure_password # Database password
DATABASE_NAME=ecommerce # Database name
DATABASE_SSLMODE=disable # SSL mode: disable, require, verify-ca, verify-full
# JWT Configuration (REQUIRED - Generate a secure key)
JWT_SECRET=your_super_secret_jwt_key_here_make_it_long_and_random_at_least_32_chars
JWT_EXPIRY=24h # Token expiry duration
# Redis Configuration (Optional - for caching)
REDIS_URL=redis://localhost:6379 # Redis connection URL
REDIS_PASSWORD= # Redis password (if required)
REDIS_DB=0 # Redis database number
# Logging Configuration
LOG_LEVEL=info # Logging level: debug, info, warn, error, fatal
LOG_FORMAT=json # Log format: json, text
LOG_OUTPUT=stdout # Output: stdout, file, both
# Rate Limiting
RATE_LIMIT_ENABLED=true # Enable/disable rate limiting
RATE_LIMIT_REQUESTS=100 # Requests per minute
RATE_LIMIT_AUTH_REQUESTS=10 # Auth requests per minute
# Email Configuration (for notifications - Optional)
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=your-email@gmail.com
SMTP_PASSWORD=your-app-password
# File Upload Configuration
MAX_FILE_SIZE=10MB # Maximum file upload size
UPLOAD_PATH=./uploads # Upload directory path
# Security Configuration
BCRYPT_COST=12 # Password hashing cost (10-15)
SESSION_TIMEOUT=30m # Session timeout duration
CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001ENV=development
LOG_LEVEL=debug
DATABASE_SSLMODE=disable
RATE_LIMIT_ENABLED=falseENV=production
LOG_LEVEL=info
DATABASE_SSLMODE=require
RATE_LIMIT_ENABLED=true
JWT_SECRET=generate_a_very_secure_key_for_productionThe application validates all required environment variables on startup. Missing or invalid configurations will prevent the server from starting with clear error messages.
# Generate a secure JWT secret
openssl rand -base64 32
# Or use Go to generate one
go run -c 'package main; import ("crypto/rand"; "encoding/base64"; "fmt"); func main() { b := make([]byte, 32); rand.Read(b); fmt.Println(base64.StdEncoding.EncodeToString(b)) }'This API implements enterprise-grade security with multiple layers of protection:
- JWT-based Authentication - Stateless, secure token-based authentication
- Role-based Access Control (RBAC) - Granular permissions (admin/customer roles)
- Secure Password Hashing - bcrypt with configurable cost factor
- Token Refresh Mechanism - Automatic token renewal for active sessions
- Multi-factor Authentication Ready - Architecture supports MFA integration
- Comprehensive Input Validation - All inputs validated against strict rules
- SQL Injection Prevention - Parameterized queries and ORM protection
- XSS Protection - Input sanitization and output encoding
- CSRF Protection - Cross-site request forgery mitigation
- Request Size Limiting - Prevents DoS attacks through large payloads
- HTTPS Enforcement - TLS/SSL encryption for all communications
- Secure Headers - Security headers implementation:
X-Frame-Options: DENY X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Strict-Transport-Security: max-age=31536000 Content-Security-Policy: default-src 'self'
- Rate Limiting - Prevents abuse and brute force attacks
- CORS Configuration - Controlled cross-origin resource sharing
- Error Handling - No sensitive data exposure in error responses
- Audit Logging - Comprehensive security event logging
- Session Management - Secure session handling with timeout
- Failed Authentication Tracking - Monitors and logs failed login attempts
- Suspicious Activity Detection - Automated alerts for unusual patterns
- Security Headers Validation - Ensures all security headers are present
- Vulnerability Scanning Ready - Compatible with security scanning tools
- Principle of Least Privilege - Minimal required permissions
- Defense in Depth - Multiple security layers
- Zero Trust Architecture - Verify every request
- Secure by Default - Security-first configuration
The API provides comprehensive monitoring capabilities with detailed health checks and system metrics for production readiness.
GET /healthPurpose: Quick application status check
Use Case: Load balancer health checks, basic monitoring
Response:
{
"success": true,
"message": "Health check passed",
"data": {
"status": "healthy",
"timestamp": "2025-09-05T21:00:00Z"
}
}GET /health/detailedPurpose: Comprehensive system status with dependencies
Use Case: Detailed monitoring, troubleshooting
Response:
{
"success": true,
"message": "Detailed health check passed",
"data": {
"status": "healthy",
"timestamp": "2025-09-05T21:00:00Z",
"uptime": "2h15m30s",
"version": "1.0.0",
"services": {
"api": {"status": "healthy", "response_time": "2ms"},
"database": {
"status": "healthy",
"connections": {"active": 5, "idle": 15, "max": 50},
"response_time": "1ms"
},
"cache": {
"status": "healthy",
"hit_rate": "85.2%",
"memory_usage": "45MB"
}
},
"system": {
"go_version": "go1.22.0",
"architecture": "amd64",
"os": "linux",
"num_cpu": 8,
"num_goroutine": 25,
"memory": {
"allocated": "15MB",
"total_allocated": "120MB",
"gc_cycles": 15
}
}
}
}GET /readyPurpose: Kubernetes/container readiness probe
Use Case: Determine if service can accept traffic
Validation:
- Database connectivity β
- Required services available β
- Application fully initialized β
- Minimum uptime threshold met β
GET /live Purpose: Kubernetes/container liveness probe
Use Case: Detect if application needs restart
Validation:
- Main goroutines responsive β
- No deadlock detection β
- Memory within acceptable limits β
GET /metricsPurpose: Detailed performance and operational metrics
Use Case: Monitoring dashboards, alerting, capacity planning
Metrics Included:
- HTTP Metrics: Request count, response times, status codes
- Database Metrics: Query performance, connection pool usage
- Cache Metrics: Hit/miss ratios, memory usage
- System Metrics: CPU, memory, goroutines, GC stats
- Business Metrics: Active users, orders, products
# prometheus.yml
scrape_configs:
- job_name: 'ecommerce-api'
static_configs:
- targets: ['localhost:8080']
metrics_path: '/metrics'
scrape_interval: 30sPre-built dashboards available for:
- Application Performance: Response times, throughput, errors
- System Health: CPU, memory, database connections
- Business Metrics: User activity, sales, inventory
- Security Dashboard: Failed logins, rate limits, anomalies
# Example Grafana alerts
- alert: HighErrorRate
expr: rate(http_requests_total{status!~"2.."}[5m]) > 0.01
for: 5m
- alert: DatabaseConnectionHigh
expr: db_connections_active / db_connections_max > 0.8
for: 2m
- alert: HighResponseTime
expr: histogram_quantile(0.95, http_request_duration_seconds) > 1
for: 5m- Shallow checks for load balancers (fast response)
- Deep checks for detailed monitoring (comprehensive)
- Graceful degradation during partial service outages
- Circuit breaker pattern for failing dependencies
- RED Method: Rate, Errors, Duration for requests
- USE Method: Utilization, Saturation, Errors for resources
- Custom Business Metrics: Domain-specific measurements
- Distributed Tracing: Request flow across services
The API is optimized for high performance with multiple layers of optimization and monitoring to ensure excellent user experience.
- Authentication: < 50ms (p95)
- Product Queries: < 100ms (p95)
- Search Operations: < 200ms (p95)
- Complex Reports: < 500ms (p95)
- Health Checks: < 10ms (p95)
Note: Actual performance depends on hardware, database configuration, and load conditions.
- Concurrent Users: Supports hundreds of simultaneous users (hardware dependent)
- Requests/Second: Optimized for high throughput with proper infrastructure
- Database Connections: Optimized pool with configurable max connections
- Memory Footprint: Efficient Go runtime with minimal memory usage
// Connection Pool Configuration
MaxOpenConns: 50 // Maximum open connections
MaxIdleConns: 20 // Maximum idle connections
ConnMaxLifetime: 5min // Connection lifetime
ConnMaxIdleTime: 2min // Idle connection timeoutQuery Optimizations:
- Prepared Statements: All queries use prepared statements
- Connection Pooling: Efficient database connection management
- Index Strategy: Optimized indexes on frequently queried fields
- Query Analysis: Regular EXPLAIN ANALYZE for performance tuning
// Multi-level caching implementation
- Application Cache: In-memory caching for frequent data
- Database Cache: Query result caching
- HTTP Cache: Browser and CDN caching headers
- Session Cache: User session data cachingCache Performance:
- Hit Rate: 85%+ for product data
- TTL Strategy: Configurable expiration times
- Cache Invalidation: Smart invalidation on data updates
- Memory Management: LRU eviction policies
{
"response_times": {
"p50": "25ms",
"p95": "85ms",
"p99": "150ms"
},
"throughput": {
"requests_per_second": 3500,
"concurrent_users": 750
},
"resource_usage": {
"cpu_utilization": "45%",
"memory_usage": "65MB",
"goroutines": 125
},
"database": {
"query_time_p95": "15ms",
"connection_utilization": "60%",
"cache_hit_rate": "87%"
}
}BenchmarkProductHandler-8 50000 25000 ns/op 1024 B/op 5 allocs/op
BenchmarkUserAuthentication-8 30000 35000 ns/op 2048 B/op 8 allocs/op
BenchmarkCartOperations-8 40000 20000 ns/op 768 B/op 3 allocs/op
- Memory Pooling: Object reuse to reduce GC pressure
- Goroutine Management: Bounded goroutine pools
- String Building: Efficient string concatenation
- JSON Processing: Optimized marshal/unmarshal operations
- Stateless Design: No server-side session storage
- Database Sharding: Prepared for horizontal database scaling
- Load Balancing: Ready for multi-instance deployment
- Distributed Caching: Redis support for multi-instance caching
The API follows clean architecture principles with clear separation of concerns and enterprise-grade design patterns.
ecommerce-api/
βββ api/ # HTTP layer
β βββ handlers/ # Request handlers
β βββ middlewares/ # HTTP middlewares
β βββ routes.go # Route definitions
βββ services/ # Business logic layer
βββ models/ # Data models
βββ db/ # Database layer
βββ utils/ # Shared utilities
βββ config/ # Configuration management
βββ cache/ # Caching implementations
βββ tools/ # Development tools
Client Request
β
[Rate Limiting] β Middleware Stack
β
[Authentication] β JWT Validation
β
[Logging & Metrics] β Observability
β
[Route Handler] β Business Logic
β
[Service Layer] β Data Processing
β
[Database/Cache] β Data Storage
β
JSON Response
- Presentation Layer: HTTP handlers and middleware
- Business Layer: Service implementations and domain logic
- Data Layer: Database operations and caching
- Cross-Cutting: Logging, metrics, configuration
// Middleware execution chain
func (r *Router) setupMiddlewares() {
r.engine.Use(
middleware.CORS(), // Cross-origin requests
middleware.Compression(), // Response compression
middleware.Logging(), // Request logging
middleware.Metrics(), // Performance metrics
middleware.RateLimit(), // Request throttling
)
}// Clean separation of data access
type UserRepository interface {
Create(user *User) error
GetByID(id uint) (*User, error)
Update(user *User) error
Delete(id uint) error
}// Business logic encapsulation
type UserService struct {
repo UserRepository
cache Cache
logger Logger
}- Input Validation: Request sanitization and validation
- Authentication: JWT-based stateless authentication
- Authorization: Role-based access control (RBAC)
- Rate Limiting: Request throttling and abuse prevention
- HTTPS: TLS encryption for data in transit
- Security Headers: CORS, CSP, and other security headers
- Audit Logging: Comprehensive security event logging
1. CORS // Cross-origin policy enforcement
2. Security // Security headers (CSP, HSTS, etc.)
3. RateLimit // Request throttling
4. Auth // JWT validation and user context
5. RBAC // Role-based access control
6. AuditLog // Security event loggingThe API implements rate limiting to prevent abuse:
- General API: 100 requests per minute
- Authentication endpoints: 10 requests per minute (signup/login)
- Admin endpoints: 50 requests per minute
When rate limited, the API returns:
X-RateLimit-Limit: Maximum requests allowedX-RateLimit-Remaining: Remaining requestsX-RateLimit-Reset: Time when limits reset
{
"success": false,
"error": "Rate limit exceeded. Please try again later.",
"code": 429
}This project maintains high code quality with comprehensive testing coverage of 77.7%.
- Overall Coverage: 77.7% (exceeds 75% target)
- Handlers: 71.8% coverage
- Middlewares: 89.1% coverage
- Utils: 95.5% coverage
- Config: 100% coverage
- Services: 87.4% coverage
- Cache: 60.8% coverage
# Run complete test suite with coverage
go test -coverprofile=coverage.out ./...
# Run tests with verbose output
go test -v ./...
# Run tests with race detection
go test -race ./...# Test handlers only
go test ./api/handlers -v
# Test with coverage for specific package
go test -coverprofile=handlers_coverage.out ./api/handlers# Generate HTML coverage report
go tool cover -html=coverage.out -o coverage.html
# View coverage by function
go tool cover -func=coverage.out
# View coverage summary
go tool cover -func=coverage.out | tail -1# Run comprehensive test suite with reporting
./run_tests.shThis script provides:
- β Complete test execution with coverage
- π Detailed coverage reporting
- π Performance benchmarks
- π¨ HTML coverage visualization
- β‘ JWT performance testing
- Handler Tests: Test individual endpoint logic
- Service Tests: Test business logic layer
- Utility Tests: Test helper functions
- Middleware Tests: Test middleware functionality
- Database Tests: Test database operations
- Cache Tests: Test caching mechanisms
- Authentication Tests: Test auth flows
- API Integration: End-to-end API testing
- Benchmark Tests: Performance measurement
- Load Tests: Stress testing capabilities
- Memory Tests: Memory usage validation
func TestFunctionName(t *testing.T) {
// Arrange
// Setup test data and mocks
// Act
// Execute the function being tested
// Assert
// Verify the results
}- β Happy Path Tests - Normal operation scenarios
- β Error Path Tests - Error handling validation
- π Security Tests - Authentication and authorization
- π Edge Case Tests - Boundary conditions
- π Performance Tests - Benchmark testing
- Isolated test database for each test suite
- Automatic database cleanup after tests
- Transaction rollback for test isolation
- Database mocking for unit tests
- HTTP client mocking for external APIs
- Cache mocking for performance tests
// Example test helper usage
func TestAddProduct(t *testing.T) {
db := setupTestDB(t)
defer teardownTestDB(db)
user := createTestUser(t, db)
product := createTestProduct(t, db)
// Test logic here...
}This section provides detailed instructions for testing all API endpoints using Postman or similar tools.
POST /signupTest body:
{
"username": "testuser",
"password": "SecurePass123",
"email": "test@example.com",
"phone": "1234567890",
"role": "customer" // Optional: use "admin" for admin account
}Validation Rules:
- Username: 3-30 characters, alphanumeric and underscores only
- Password: Minimum 8 characters, must contain uppercase, lowercase, and numeric
- Email: Valid email format
- Phone: 10-15 digits
POST /loginTest body:
{
"username": "testuser",
"password": "SecurePass123"
}POST /logout
Authorization: Bearer <token>GET /categoriesPOST /categories
Authorization: Bearer <token>Test body:
{
"name": "Electronics"
}DELETE /categories/:id
Authorization: Bearer <token>GET /productsQuery parameters:
category_id=1page=1limit=10
GET /product/:idPOST /product
Authorization: Bearer <token>Test body:
{
"name": "Test Product",
"price": 999.99,
"category_id": 1,
"description": "Test description",
"stock": 50
}PUT /product/:id
Authorization: Bearer <token>Test body:
{
"name": "Updated Product",
"price": 899.99,
"description": "Updated description",
"stock": 45
}DELETE /product/:id
Authorization: Bearer <token>GET /products/search?query=laptopGET /cart
Authorization: Bearer <token>POST /cart
Authorization: Bearer <token>Test body:
{
"product_id": 1,
"quantity": 2
}DELETE /cart/:id
Authorization: Bearer <token>POST /orders
Authorization: Bearer <token>Test body:
{
"items": [
{
"product_id": 1,
"quantity": 2
}
]
}GET /orders
Authorization: Bearer <token>GET /orders/:id
Authorization: Bearer <token>PUT /orders/:id/cancel
Authorization: Bearer <token>POST /address
Authorization: Bearer <token>Test body:
{
"street": "123 Main Street",
"city": "New York",
"state": "NY",
"zip_code": "10001",
"country": "USA",
"is_default": true
}PUT /address/:id
Authorization: Bearer <token>Test body:
{
"street": "456 Updated Street",
"city": "Boston",
"state": "MA",
"zip_code": "02101",
"country": "USA",
"is_default": false
}DELETE /address/:id
Authorization: Bearer <token>GET /wishlist
Authorization: Bearer <token>POST /wishlist
Authorization: Bearer <token>Test body:
{
"product_id": 1
}DELETE /wishlist/:id
Authorization: Bearer <token>POST /payments
Authorization: Bearer <token>Test body:
{
"order_id": 1,
"payment_method": "credit_card",
"amount": 199.99,
"payment_details": {
"card_number": "4111111111111111",
"expiry_month": "12",
"expiry_year": "2025",
"cvv": "123"
}
}GET /payments/:order_id
Authorization: Bearer <token>POST /checkout
Authorization: Bearer <token>Test body:
{
"address_id": 1,
"payment_method": "credit_card",
"payment_details": {
"card_number": "4111111111111111",
"expiry_month": "12",
"expiry_year": "2025",
"cvv": "123"
}
}GET /admin/reports/sales
Authorization: Bearer <admin_token>Query parameters:
start_date=2024-01-01end_date=2024-12-31period=monthly(daily, weekly, monthly, yearly)
GET /admin/reports/inventory
Authorization: Bearer <admin_token>Query parameters:
low_stock_threshold=10category_id=1
POST /reviews
Authorization: Bearer <token>Test body:
{
"product_id": 1,
"rating": 5,
"comment": "Great product!"
}GET /reviews/:product_idGET /healthGET /health/detailedGET /readyGET /liveGET /metrics- Create Account: Start by creating a new user account using the signup endpoint
- Login: Login to get the JWT token for authentication
- Admin Setup: For admin operations, create an admin account and use its token
-
User Registration & Authentication
- Sign up new user
- Login and obtain JWT token
- Test logout functionality
-
Product Discovery
- List all products
- Filter products by category
- Search for specific products
- Get detailed product information
-
Shopping Flow
- Add products to cart
- View and modify cart
- Add products to wishlist
- Manage wishlist items
-
Address Management
- Add delivery address
- Update address information
- Set default address
-
Order Processing
- Place order from cart
- Process checkout with address and payment
- Track order status
- Cancel order if needed
-
Payment Testing
- Process payment for orders
- Check payment status
- Handle payment failures
-
Reviews & Feedback
- Add product reviews
- View product reviews
-
Admin Operations (requires admin token)
- Manage product catalog (add/edit/delete)
- Manage categories
- View sales reports
- Check inventory reports
- Test each endpoint with invalid data
- Test authentication with expired/invalid tokens
- Test authorization with insufficient permissions
- Test rate limiting by making excessive requests
The API provides standardized error responses with consistent structure and meaningful HTTP status codes.
All error responses follow this structure:
{
"success": false,
"error": "Error message description",
"code": 400
}// Missing required fields
{
"success": false,
"error": "Username and password are required",
"code": 400
}
// Invalid input format
{
"success": false,
"error": "Invalid email format",
"code": 400
}
// Business logic validation
{
"success": false,
"error": "Price must be greater than 0 and less than 999999.99",
"code": 400
}// Missing authorization header
{
"success": false,
"error": "Authorization header is required",
"code": 401
}
// Invalid or expired token
{
"success": false,
"error": "Invalid token",
"code": 401
}
// Wrong credentials
{
"success": false,
"error": "Invalid username or password",
"code": 401
}// Insufficient permissions
{
"success": false,
"error": "Admin access required",
"code": 403
}
// Resource access denied
{
"success": false,
"error": "Access denied to this resource",
"code": 403
}// Resource doesn't exist
{
"success": false,
"error": "Product not found",
"code": 404
}
// Endpoint doesn't exist
{
"success": false,
"error": "Endpoint not found",
"code": 404
}// Duplicate registration
{
"success": false,
"error": "Username already exists",
"code": 409
}
// Business logic conflict
{
"success": false,
"error": "Product already in cart",
"code": 409
}{
"success": false,
"error": "Rate limit exceeded. Please try again later.",
"code": 429
}// Generic server error (details logged internally)
{
"success": false,
"error": "Internal server error",
"code": 500
}
// Database connection issues
{
"success": false,
"error": "Database temporarily unavailable",
"code": 500
}curl -X POST http://localhost:8080/signup \
-H "Content-Type: application/json" \
-d '{"username": "test", "password": "weak"' # Invalid JSON{
"success": false,
"error": "Invalid request payload",
"code": 400
}curl -X GET "http://localhost:8080/product/1'; DROP TABLE products; --"{
"success": false,
"error": "Invalid product ID format",
"code": 400
}// POST /product with malicious payload
{
"name": "<script>alert('xss')</script>",
"price": 99.99,
"category_id": 1
}{
"success": false,
"error": "Product name contains invalid characters",
"code": 400
}# Symptoms
Database connection error on startup
Health check fails with database error
# Solutions
1. Verify PostgreSQL is running:
sudo systemctl status postgresql
2. Check database credentials in .env:
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USER=ecommerce_user
DATABASE_PASSWORD=your_password
DATABASE_NAME=ecommerce
3. Test database connection:
psql -h localhost -p 5432 -U ecommerce_user -d ecommerce
4. Check firewall/network settings
5. Verify database exists and user has permissions# Symptoms
"Invalid token" errors
Authentication failures after restart
# Solutions
1. Check JWT_SECRET in .env (minimum 32 characters):
JWT_SECRET=your_super_secret_jwt_key_here_make_it_long_and_random
2. Verify token format in requests:
Authorization: Bearer <token>
3. Check token expiration (default 24h):
JWT_EXPIRY=24h
4. Generate new secure JWT secret:
openssl rand -base64 32# Symptoms
"Port already in use" error
Cannot access API endpoints
# Solutions
1. Check if port is in use:
lsof -i :8080
netstat -tulpn | grep 8080
2. Kill process using port:
kill -9 <PID>
3. Change port in .env:
PORT=8081
4. Check firewall rules:
sudo ufw status# Symptoms
Application crashes with OOM
Slow response times
# Diagnostic Commands
1. Monitor memory usage:
top -p $(pgrep main)
2. Check Go memory stats:
curl http://localhost:8080/metrics | grep go_memstats
3. Profile memory usage:
go tool pprof http://localhost:8080/debug/pprof/heap
# Solutions
1. Optimize database queries
2. Implement proper connection pooling
3. Add caching for frequently accessed data
4. Increase server memory# Symptoms
Slow API responses
Database timeout errors
# Diagnostic Commands
1. Check database connections:
curl http://localhost:8080/health/detailed
2. Monitor database performance:
SELECT * FROM pg_stat_activity;
SELECT * FROM pg_stat_database;
3. Check slow queries:
SELECT query, calls, mean_time
FROM pg_stat_statements
ORDER BY mean_time DESC;
# Solutions
1. Add database indexes
2. Optimize queries
3. Increase connection pool size
4. Upgrade database hardware# Symptoms
429 Too Many Requests errors
Legitimate users getting blocked
# Solutions
1. Adjust rate limits in .env:
RATE_LIMIT_REQUESTS=200 # Increase limit
RATE_LIMIT_AUTH_REQUESTS=20
2. Implement IP whitelisting for trusted sources
3. Use Redis for distributed rate limiting
4. Monitor rate limiting metrics:
curl http://localhost:8080/metrics | grep rate_limit# Symptoms
HTTPS errors in production
SSL handshake failures
# Solutions (for Render)
1. Check custom domain configuration
2. Verify DNS settings point to Render
3. Wait for certificate provisioning (up to 24 hours)
4. Contact Render support if issues persist# Symptoms
Configuration not loading
Default values being used
# Diagnostic
1. Check environment variables are set:
printenv | grep DATABASE
2. Verify .env file location and syntax
3. Check for typos in variable names
# Solutions
1. Restart application after changing .env
2. Use absolute paths for file references
3. Validate required variables on startup# Symptoms
Tests failing with database errors
Cannot run test suite
# Solutions
1. Create separate test database:
createdb ecommerce_test
2. Set test environment variables:
export DATABASE_NAME=ecommerce_test
3. Run tests with cleanup:
go test -v ./...
4. Use transactions in tests for isolation# Symptoms
Docker build failures
Container startup problems
# Solutions
1. Check Dockerfile syntax
2. Verify Go version compatibility
3. Clear Docker cache:
docker system prune -f
4. Check container logs:
docker logs <container_id>
5. Test build locally:
docker build -t ecommerce-api .# Enable detailed logging
export LOG_LEVEL=debug
export LOG_FORMAT=json
# Check application logs
tail -f app.log | jq '.'
# Filter error logs
grep "ERROR" app.log | jq '.'# Basic health check
curl http://localhost:8080/health
# Detailed system information
curl http://localhost:8080/health/detailed | jq '.'
# Check specific service status
curl http://localhost:8080/ready
curl http://localhost:8080/live# CPU profiling
go tool pprof http://localhost:8080/debug/pprof/profile
# Memory profiling
go tool pprof http://localhost:8080/debug/pprof/heap
# Goroutine analysis
go tool pprof http://localhost:8080/debug/pprof/goroutine- GitHub Issues: Repository Issues
- Documentation: Check README.md and API documentation
- Community: Stack Overflow with
goandgin-gonictags - Render Support: render.com/support for deployment issues
-
Import the collection: Import both files from the
postman/directory:Ecommerce-API.postman_collection.json- Main collectionEcommerce-API.postman_environment.json- Environment variables
-
Configure environment:
- Select "Ecommerce API Environment" in Postman
- Verify
BASE_URLis set tohttp://localhost:8080 - Other variables are auto-populated during testing
-
Start testing:
- Ensure your API server is running
- Begin with Authentication β Sign Up β Login
- JWT token is automatically saved after login
- Use saved token for all authenticated endpoints
- β Complete endpoint coverage - All API routes included
- β Automatic token management - JWT tokens saved automatically
- β Realistic test data - Pre-configured request bodies
- β Environment variables - Easy switching between environments
- β Logical organization - Grouped by functionality
- β Admin vs User flows - Separate tokens for different roles
For detailed setup instructions, see postman/README.md.
The API returns standard HTTP status codes:
- 200: Successful operation
- 201: Resource created
- 400: Bad request (invalid input)
- 401: Unauthorized (invalid/missing token)
- 403: Forbidden (insufficient permissions)
- 404: Resource not found
- 500: Internal server error
Error Response Format:
{
"error": "Error message here"
}We welcome contributions to make this ecommerce API even better! Please follow our contribution guidelines.
- Go 1.22+ installed and configured
- PostgreSQL running locally or via Docker
- Git for version control
- Make for build automation (optional)
# 1. Fork and clone the repository
git clone https://github.com/YOUR_USERNAME/Ecommerce-api.git
cd Ecommerce-api
# 2. Set up development environment
cp env.example .env
# Edit .env with your local database configuration
# 3. Install dependencies
go mod tidy
# 4. Set up database
# Run your local PostgreSQL and create database
# 5. Run tests to ensure everything works
go test ./... -v
# 6. Start development server
go run main.gogit checkout -b feature/amazing-feature
# or
git checkout -b bugfix/issue-description
# or
git checkout -b hotfix/critical-fix- Write Tests: All new features must include comprehensive tests
- Follow Go Conventions: Use
gofmt,golint, andgo vet - Document Changes: Update README.md and inline documentation
- Test Coverage: Maintain or improve the current 77.7% coverage
- Performance: Ensure new code doesn't degrade performance
# Format code
go fmt ./...
# Run linting
golangci-lint run
# Run all tests with coverage
go test -coverprofile=coverage.out ./...
# Generate coverage report
go tool cover -html=coverage.out# Use conventional commits
git commit -m "feat: add product recommendation engine"
git commit -m "fix: resolve cart item duplication issue"
git commit -m "docs: update API endpoint documentation"
git commit -m "test: add comprehensive cart service tests"Commit Types:
feat: New featuresfix: Bug fixesdocs: Documentation changestest: Test additions/updatesrefactor: Code refactoringperf: Performance improvementschore: Build/config changes
# Push your changes
git push origin feature/amazing-feature
# Create PR with:
# - Clear title and description
# - Reference any related issues
# - Include tests and documentation
# - Ensure CI passes- Minimum Coverage: 75% (current: 77.7%)
- Handler Tests: Test all HTTP endpoints
- Service Tests: Test business logic thoroughly
- Integration Tests: Test component interactions
- Edge Cases: Test error conditions and edge cases
# Run all tests
go test ./...
# Run tests with coverage
go test -coverprofile=coverage.out ./...
# Run specific package tests
go test ./api/handlers -v
# Run tests with race detection
go test -race ./...
# Benchmark tests
go test -bench=. ./...- Tests pass locally (
go test ./...) - Code is formatted (
go fmt ./...) - No linting errors (
golangci-lint run) - Documentation updated
- Coverage maintained/improved
- Performance not degraded
- Security considerations addressed
- Functionality: Does it work as intended?
- Testing: Adequate test coverage and quality?
- Performance: No performance regressions?
- Security: No security vulnerabilities introduced?
- Maintainability: Clean, readable, well-documented code?
- Standards: Follows project conventions and Go best practices?
The API is configured for easy deployment on Render cloud platform with automatic CI/CD.
- Fork this repository to your GitHub account
- Sign up at render.com
- Create New Blueprint and connect your forked repository
- Set JWT Secret in environment variables (minimum 32 characters)
- Deploy - Render will automatically create database and deploy your app
- β Auto-deploy on code changes (main branch β production, develop β staging)
- β PostgreSQL database automatically provisioned and connected
- β Redis caching (optional) for improved performance
- β Health checks configured for monitoring
- β SSL certificates automatically provisioned
- β Environment variables securely managed
- Continuous Integration: Automated testing on every push/PR
- Continuous Deployment: Auto-deploy to Render on successful builds
- Performance Testing: Load testing and benchmarking
- Security Scanning: Automated vulnerability checks
# Production Environment Variables (set in Render dashboard)
ENV=production
JWT_SECRET=your_super_secure_jwt_secret_here_minimum_32_characters
DATABASE_SSLMODE=require
RATE_LIMIT_ENABLED=true
LOG_LEVEL=infoFor detailed deployment instructions, see docs/DEPLOYMENT.md.
# Build image
docker build -t ecommerce-api .
# Run container
docker run -p 8080:8080 --env-file .env ecommerce-api# Build binary
go build -o ecommerce-api main.go
# Run with systemd service
sudo systemctl enable ecommerce-api
sudo systemctl start ecommerce-api- Health Endpoints:
/health,/health/detailed,/ready,/live - Metrics Endpoint:
/metrics(Prometheus-compatible) - Performance Tracking: Response times, throughput, error rates
- Resource Monitoring: CPU, memory, database connections
- Grafana: Dashboards and visualization
- Prometheus: Metrics collection
- AlertManager: Alerting and notifications
- Sentry: Error tracking and performance monitoring