Skip to content

Latest commit

 

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

E-Commerce API

Go REST API for e-commerce with JWT auth, PostgreSQL, and role-based access.

Tech Stack: Go 1.21+ | Chi Router | PostgreSQL | JWT | Bcrypt


Prerequisites

Before setting up the project, ensure you have the following installed:


1. Create Database

# Using createdb command
createdb -U postgres ecom

# OR using psql
sudo -u postgres psql -c "CREATE DATABASE ecom;"

2. Clone and Setup Project

# 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 tidy

3. Install Goose (Migration Tool)

go install github.com/pressly/goose/v3/cmd/goose@latest

4. Run Database Migrations

# Run migrations
goose -dir ./internal/adapters/psql/migrations postgres "$GOOSE_DBSTRING" up

5. Seed Database (Optional)

Add 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

6. Build and Run the Server

# run directly without building
go run cmd/*.go

7. Verify Server is Running

# Health check
curl http://localhost:8081/health

API Endpoints Reference

Public Routes

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

Protected Routes (Requires JWT Token)

Method Endpoint Description
POST /order Place new order
DELETE /orders/{id} Delete order

Admin Routes (Admin-Only)

Method Endpoint Description
POST /products Create product
PUT /products/{id} Update product
DELETE /products/{id} Delete product

Project Structure

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

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages