A robust and scalable e-commerce REST API built with Go, featuring user authentication, product management, shopping cart functionality, and order processing.
-
User Authentication & Authorization
- User registration and login
- JWT-based authentication
- Password hashing with bcrypt
- Protected routes for authenticated users
-
Product Management
- Browse all products
- Get product details by ID
- Create new products (authenticated users only)
- Product inventory management
-
Shopping Cart & Orders
- Add items to cart
- Checkout functionality
- Order creation and management
- Order item tracking
- Inventory updates on purchase
-
Database Integration
- MySQL database with migration support
- Clean architecture with repository pattern
- Database connection pooling
- Backend: Go 1.24.3
- Database: MySQL
- Authentication: JWT (JSON Web Tokens)
- HTTP Router: Gorilla Mux
- Database Migrations: golang-migrate
- Validation: go-playground/validator
- Password Hashing: bcrypt
- Environment Management: godotenv
go-ecom/
βββ cmd/
β βββ api/ # API server setup
β βββ migrate/ # Database migrations
β βββ main.go # Application entry point
βββ config/ # Configuration management
βββ db/ # Database connection
βββ service/ # Business logic
β βββ auth/ # Authentication middleware
β βββ user/ # User management
β βββ product/ # Product management
β βββ cart/ # Shopping cart
β βββ order/ # Order processing
βββ types/ # Data models and interfaces
βββ utils/ # Utility functions
βββ go.mod # Go module dependencies
βββ Makefile # Build and run commands
βββ README.md # Project documentation
Before running this project, make sure you have the following installed:
- Go 1.24.3+
- MySQL 8.0+
- golang-migrate (for database migrations)
The easiest way to run the project is using Docker Compose, which will set up both the API server and MySQL database automatically.
git clone https://github.com/CuongDepay/go-ecom.git
cd go-ecomdocker-compose up -dThis will:
- Start a MySQL 8.0 database container
- Build and start the Go API server container
- Automatically configure the database connection
- Run the application on
http://localhost:8080
Once the containers are running, execute migrations:
# Connect to the API container and run migrations
docker-compose exec api make migrate-updocker-compose downTo remove volumes (database data):
docker-compose down -vgit clone https://github.com/CuongDepay/go-ecom.git
cd go-ecomgo mod downloadCreate a .env file in the root directory with the following variables:
# Server Configuration
PUBLIC_HOST=http://localhost
PORT=8080
# Database Configuration
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASSWORD=mypassword
DB_NAME=ecom
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key
JWT_EXPIRATION_IN_SECONDS=604800 # 7 daysCreate a MySQL database:
CREATE DATABASE ecom;Run database migrations:
make migrate-upmake runThe server will start on http://localhost:8080
# Start all services (API + Database)
docker-compose up -d
# Stop all services
docker-compose down
# Stop and remove volumes (deletes database data)
docker-compose down -v
# View logs
docker-compose logs
# Follow logs in real-time
docker-compose logs -f
# Execute commands in the API container
docker-compose exec api <command>
# Run migrations in Docker
docker-compose exec api make migrate-up
# Access MySQL database
docker-compose exec db mysql -u root -pmypassword ecom# Build the application
make build
# Run the application
make run
# Run tests
make test
# Run database migrations up
make migrate-up
# Run database migrations down
make migrate-down
# Create a new migration
make migration <migration_name>| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/v1/register |
Register a new user | No |
| POST | /api/v1/login |
Login user | No |
| GET | /api/v1/users/{userID} |
Get user by ID | Yes |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /api/v1/products |
Get all products | No |
| GET | /api/v1/products/{productID} |
Get product by ID | No |
| POST | /api/v1/products |
Create new product | Yes |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/v1/cart/checkout |
Checkout cart items | Yes |
curl -X POST http://localhost:8080/api/v1/register \
-H "Content-Type: application/json" \
-d '{
"firstName": "John",
"lastName": "Doe",
"email": "[email protected]",
"password": "password123"
}'curl -X POST http://localhost:8080/api/v1/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "password123"
}'Response:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}curl -X GET http://localhost:8080/api/v1/productscurl -X POST http://localhost:8080/api/v1/products \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"name": "Laptop",
"description": "High-performance laptop",
"image": "laptop.jpg",
"price": 999.99,
"quantity": 10
}'curl -X POST http://localhost:8080/api/v1/cart/checkout \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"items": [
{
"productID": 1,
"quantity": 2
},
{
"productID": 2,
"quantity": 1
}
]
}'The application uses the following main tables:
- users: User account information
- products: Product catalog
- orders: Order records
- order_items: Individual items within orders
This API uses JWT (JSON Web Tokens) for authentication. Include the token in the Authorization header:
Authorization: Bearer <your-jwt-token>
JWT tokens expire after 7 days by default (configurable via JWT_EXPIRATION_IN_SECONDS).
Run the test suite:
make testThe project includes unit tests for handlers and services.
make migration add_new_tableThis creates new migration files in cmd/migrate/migrations/.
The project follows a clean architecture pattern:
- cmd/: Application entry points
- service/: Business logic and HTTP handlers
- types/: Data models and interfaces
- db/: Database connection and queries
- config/: Configuration management
- utils/: Utility functions
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License.
- Database connection failed: Ensure MySQL is running and credentials are correct
- Migration errors: Check if database exists and user has proper permissions
- JWT token invalid: Ensure token is not expired and JWT_SECRET matches
Make sure your MySQL server is running and accessible with the credentials specified in your .env file.
For Docker Compose setup, the database connection is automatically configured between containers.
The Dockerfile implements several security best practices:
- β Multi-stage build to reduce attack surface
- β Distroless base image (no shell, no package manager)
- β Non-root user (runs as user 65534)
- β Static binary compilation for better security
- β Updated base images with latest security patches
- β
Dependency verification with
go mod verify
If you encounter any issues or have questions, please open an issue on GitHub.