A modern, scalable e-commerce REST API built with Go, following clean architecture principles and best practices.
This project implements clean architecture with clear separation of concerns:
graph TD
User((User)) -->|HTTP Request| API[API Entry Point - cmd/api]
API -->|Router| Chi[Chi Router]
Chi -->|Middleware| MW[Logger/Recoverer/Auth]
MW -->|Route Handler| Handlers[Internal Handlers]
Handlers -->|Service Call| Services[Business Logic Services]
Services -->|Data Access| Store[Data Store/Repository]
Store -->|Query| DB[(Database)]
ecommerce-api-go/
├── cmd/
│ └── api/ # Application entry point
│ └── main.go
├── internal/ # Private application code
│ ├── handlers/ # HTTP request handlers (controllers)
│ ├── services/ # Business logic (service layer)
│ ├── repositories/ # Data access (repository layer)
│ ├── models/ # Data models (entities)
│ └── router/ # Route definitions
├── pkg/ # Shared utilities or helpers
├── config/ # Configuration management
├── .env.example # Environment variables template
├── go.mod
└── README.md
- 🔒 JWT Authentication: Secure user authentication and authorization
- 🛡️ Input Validation: Comprehensive request validation
- 📊 Structured Logging: Detailed logging for debugging and monitoring
- 🔄 Middleware Support: Logger, recoverer, and custom middleware
- 🎯 Clean Architecture: Separation of concerns with handlers, services, and repositories
- 🚀 Chi Router: Fast and lightweight HTTP router
- 📝 Structured Responses: Consistent JSON response format
- Go 1.21 or higher
- PostgreSQL (or your preferred database)
- Git
-
Clone the repository
git clone https://github.com/Moeed-ul-Hassan/ecommerce-api-go.git cd ecommerce-api-go -
Install dependencies
go mod download
-
Set up environment variables
cp .env.example .env # Edit .env with your configuration -
Run the application
go run cmd/api/main.go
The API will be available at http://localhost:8080 (or your configured port).
Create a .env file in the root directory with the following variables:
# Server Configuration
PORT=8080
ENV=development
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=ecommerce_db
# JWT Configuration
JWT_SECRET=your_secret_keyGET /health- Check API health status
POST /api/v1/users/register- Register a new userPOST /api/v1/users/login- User loginGET /api/v1/users/profile- Get user profile (requires auth)
GET /api/v1/products- List all productsGET /api/v1/products/:id- Get product detailsPOST /api/v1/products- Create product (admin only)PUT /api/v1/products/:id- Update product (admin only)DELETE /api/v1/products/:id- Delete product (admin only)
- Clean Architecture: Separate concerns between handlers, services, and repositories
- Dependency Injection: Pass dependencies (database, config) explicitly to structs
- Error Handling: Wrap errors with context (e.g.,
fmt.Errorf("service: %w", err)) - Validation: Validate all incoming requests before processing
- Consistency: Use a standard JSON response format for all endpoints
- Logging: Log important events and errors using a structured logger
go test ./...go build -o bin/api cmd/api/main.go- Project initialization
- Basic server setup with Chi router
- Health check endpoint
- Database integration (PostgreSQL)
- User authentication & authorization (JWT)
- Product management
- Order management
- Payment integration
- API documentation (Swagger)
- Unit & integration tests
- Docker containerization
- CI/CD pipeline
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License.
Moeed ul Hassan
- GitHub: @Moeed-ul-Hassan
- Chi Router - Lightweight, idiomatic HTTP router
- godotenv - Go port of Ruby's dotenv library