Go REST API for e-commerce with JWT auth, PostgreSQL, and role-based access.
Tech Stack: Go 1.21+ | Chi Router | PostgreSQL | JWT | Bcrypt
Before setting up the project, ensure you have the following installed:
# Using createdb command
createdb -U postgres ecom
# OR using psql
sudo -u postgres psql -c "CREATE DATABASE ecom;"# copy .env.example to .env
cp .env.example .env
# Navigate to project directory
cd /home/oh3idx/BackEnd/Golang/api_e
# Install Go dependencies
go mod download
go mod tidygo install github.com/pressly/goose/v3/cmd/goose@latest# Run migrations
goose -dir ./internal/adapters/psql/migrations postgres "$GOOSE_DBSTRING" upAdd some test data:
psql -U postgres -d ecom << EOF
-- Insert test products
INSERT INTO products (name, price_in_cents, quantity) VALUES
('Laptop', 99999, 10),
('Mouse', 2999, 50),
('Keyboard', 4999, 30);
-- Verify
SELECT * FROM products;
EOF# run directly without building
go run cmd/*.go# Health check
curl http://localhost:8081/health| Method | Endpoint | Description |
|---|---|---|
| GET | /health |
Health check |
| POST | /register |
Register new user |
| POST | /login |
Login (returns JWT token) |
| GET | /products |
List all products |
| GET | /products/{id} |
Get single product |
| GET | /orders |
List all orders |
| GET | /orders/{id} |
Get single order |
| Method | Endpoint | Description |
|---|---|---|
| POST | /order |
Place new order |
| DELETE | /orders/{id} |
Delete order |
| Method | Endpoint | Description |
|---|---|---|
| POST | /products |
Create product |
| PUT | /products/{id} |
Update product |
| DELETE | /products/{id} |
Delete product |
api_e/
├── cmd/
│ ├── main.go # Application entry point
│ └── api.go # HTTP routes & middleware
├── internal/
│ ├── auth/ # JWT authentication
│ │ └── middleware.go
│ ├── orders/ # Order management
│ │ ├── handlers.go
│ │ ├── service.go
│ │ └── repo.go
│ ├── products/ # Product management
│ │ ├── handlers.go
│ │ ├── service.go
│ │ └── repo.go
│ ├── users/ # User authentication
│ │ ├── handlers.go
│ │ ├── service.go
│ │ └── repo.go
│ └── adapters/psql/ # Database layer
│ ├── db.go
│ └── migrations/
│ ├── 20251127232449_create_users.sql
│ ├── 20251125162904_create_products.sql
│ └── 20251126153056_create_orders.sql
├── go.mod
├── go.sum
└── README.md
MIT License