A Go REST API for an e-commerce application. It provides user authentication, product management, cart checkout, order persistence, and MySQL migrations.
- Go 1.25.1
- Gorilla Mux
- MySQL
- GORM and
database/sql - JWT authentication
golang-migrate
- Go 1.25.1 or newer
- MySQL 8 or compatible MySQL server
- Make (optional, for the commands below)
Clone the repository and install dependencies:
git clone https://github.com/Nutan-Kum12/Ecom.git
cd Ecom
go mod downloadCreate a local .env file. Do not commit it:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your-password
DB_NAME=ecom
PUBLIC_HOST=http://localhost
PORT=8080
JWT_SECRET=replace-with-a-long-random-secret
JWT_EXPIRATION_IN_SECONDS=604800Create the database, then apply migrations:
CREATE DATABASE ecom;make migrate-up
make runThe API is available at http://localhost:8080.
| Method | Endpoint | Description | Auth |
|---|---|---|---|
GET |
/health |
Health check | No |
POST |
/api/v1/register |
Register a user | No |
POST |
/api/v1/login |
Get a JWT token | No |
GET |
/api/v1/products |
List products | No |
GET |
/api/v1/products/{id} |
Get a product | No |
POST |
/api/v1/products |
Create a product | Bearer token |
POST |
/api/v1/cart/checkout |
Checkout the cart | Bearer token |
See api-tests.http for REST Client requests and examples.
make run # Start the API
make test # Run all Go tests
make build # Build bin/ecom
make migrate-up # Apply pending migrations
make migrate-down # Roll back migrations
make db-status # Check database statusMigration files are in cmd/migrate/migrations/. Each schema change should include both an .up.sql and a .down.sql file.
cmd/ Application, API, database migration, and status entry points
config/ Environment-based configuration
db/ MySQL connection setup
services/ Authentication, users, products, cart, and orders
types/ Shared data models and interfaces
utils/ JSON, validation, and HTTP response helpers