A high-performance, scalable RESTful API for an e-commerce platform built with Go (Golang). This project demonstrates modern backend practices including Clean Architecture, asynchronous processing with Worker Pools, Redis caching, and containerization.
- Clean Architecture: Clear separation of concerns (Handlers, Services, Domain Models).
- High Performance: Built on top of Gin, one of the fastest Go web frameworks.
- Database: PostgreSQL with GORM for robust data management.
- Caching: Redis implementation (Cache-Aside pattern) for product listings to minimize DB load.
- Concurrency: Asynchronous Worker Pool pattern using Goroutines and Channels to handle background tasks (e.g., simulating email sending and inventory updates) without blocking the API.
- Authentication: Stateless JWT (JSON Web Token) authentication with Bcrypt password hashing.
- ACID Transactions: Safe order processing ensuring inventory and order data integrity.
- Graceful Shutdown: Handles
SIGINT/SIGTERMto ensure background workers finish tasks before the server stops. - Containerization: Full Docker and Docker Compose support for easy deployment.
- Language: Go 1.23+
- Framework: Gin Web Framework
- Database: PostgreSQL 15
- Cache: Redis
- ORM: GORM
- Auth: JWT (golang-jwt) & Bcrypt
- Deployment: Docker & Docker Compose
ecommerce-platform/
├── cmd/
│ └── api/
│ └── main.go # Application entry point
├── internal/
│ ├── core/
│ │ └── domain/ # Database models (User, Product, Order, Cart)
│ ├── handlers/ # HTTP Controllers (Input validation, JSON response)
│ ├── services/ # Business Logic (Caching, Transactions)
│ └── workers/ # Background Worker Pool (Async tasks)
├── pkg/
│ ├── auth/ # JWT Token generation & validation
│ └── database/ # Postgres & Redis connection setup
├── Dockerfile # Multi-stage build definition
├── docker-compose.yml # Orchestration for App, DB, and Redis
└── go.mod # Dependencies
The easiest way to run the application is using Docker Compose.
- Docker
- Docker Compose
- Clone the repository:
git clone [https://github.com/dwiesendanger/go-ecommerce-backend.git](https://github.com/dwiesendanger/go-ecommerce-backend.git)
cd go-ecommerce-backend- Start the application:
docker-compose up --buildThis will start PostgreSQL, Redis, and the Go API.
- Access the API:
The server will start on
http://localhost:8080.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/v1/register |
Register a new user | No |
| POST | /api/v1/login |
Login and receive JWT Token | No |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /api/v1/products |
List all products (Cached) | No |
| POST | /api/v1/products |
Create a new product (Admin) | Yes |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /api/v1/cart |
View current user's cart | Yes |
| POST | /api/v1/cart/items |
Add item to cart | Yes |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/v1/orders |
Checkout (Convert cart to order) | Yes |
| GET | /api/v1/orders |
View order history | Yes |
- Register:
POST /registerwith email/password. - Login:
POST /loginto get thetoken. - Create Product:
POST /products(use the token inAuthorization: Bearer <token>header). - Add to Cart:
POST /cart/itemswithproduct_id. - Checkout:
POST /orders.
- Observation: The API responds immediately. Check the server logs to see the Background Workers processing the email and inventory tasks asynchronously.
- History:
GET /ordersto see your past orders.
Distributed under the MIT License. See LICENSE for more information.