A production-ready Go E-Commerce API that integrates:
- Gin - Fast HTTP web framework
- GORM - Feature-rich ORM for database operations
- goauthx - Complete authentication & authorization solution
- gocommerce - E-commerce domain logic library
Authentication and authorization library with:
- JWT token management (access & refresh)
- Google OAuth 2.0 integration
- Role-based access control (RBAC) with permissions
- Admin API for role/permission management
- User management with email verification
- Password reset functionality
- Multi-database support (PostgreSQL, MySQL, SQL Server)
E-commerce domain logic library with:
- Catalog management (products, variants, categories, brands)
- Shopping cart with persistence
- Order processing and management
- Pricing and promotions with date-windowed sale prices
- Tax calculations
- Inventory management (stock levels, reservations, reorder points)
- Supplier management (multi-supplier per product, cost tracking)
- Inventory audit logging (stock_in, stock_out, adjustments, transfers)
- Clean domain-driven design
- ✅ User registration and login with JWT tokens
- ✅ Google OAuth 2.0 - Sign in with Google
- ✅ Access token & refresh token management
- ✅ Role-based access control (RBAC) with permissions
- ✅ Admin API: Role/permission management, user role assignments
- ✅ Protected routes with middleware
- ✅ User profile management
- ✅ Catalog: Products, variants, categories, and brands
- ✅ Product Search: Keyword search by name/description
- ✅ Pagination: All listing endpoints (products, categories, brands, orders)
- ✅ Shopping Cart: Add/update/remove items, cart persistence
- ✅ Orders: Create orders from cart, order history with pagination
- ✅ Pricing: Tax calculation, promotion support, date-windowed sale prices
- ✅ Inventory Ready: Database tables for stock levels, reservations, suppliers
- ✅ Clean domain-driven architecture
- ✅ Multi-database support (PostgreSQL, MySQL, SQL Server)
- ✅ Docker Deployment: Multi-stage builds, health checks
- ✅ RESTful API design with consistent responses
- ✅ Pagination with metadata (page, total_items, has_next/prev)
- ✅ Structured logging and error handling
- ✅ CORS support
- ✅ Graceful shutdown
- ✅ Environment-based configuration
github.com/devchuckcamp/gocommerce-api/
├── cmd/
│ └── api/
│ └── main.go # Application entry point
├── internal/
│ ├── config/
│ │ └── config.go # Configuration management
│ ├── database/
│ │ ├── database.go # GORM connection setup
│ │ ├── models.go # Database models
│ │ └── migrations.go # Auto-migration & seeding
│ ├── repository/
│ │ ├── catalog.go # Product/Category/Brand repositories
│ │ ├── cart.go # Cart repository
│ │ ├── orders.go # Orders repository
│ │ └── pricing.go # Promotion repository
│ ├── services/
│ │ ├── catalog.go # Catalog service with search
│ │ ├── cart.go # Cart service (gocommerce wrapper)
│ │ ├── orders.go # Order service (gocommerce wrapper)
│ │ ├── pricing.go # Pricing service (gocommerce wrapper)
│ │ └── tax.go # Tax calculator implementation
│ ├── http/
│ │ ├── server.go # HTTP server & route setup
│ │ ├── middleware/
│ │ │ ├── auth.go # JWT auth middleware
│ │ │ ├── logger.go # Request logging
│ │ │ ├── recovery.go # Panic recovery
│ │ │ └── cors.go # CORS middleware
│ │ ├── handlers/
│ │ │ ├── admin.go # Admin RBAC handlers (roles, permissions, users)
│ │ │ ├── auth.go # Auth + Google OAuth handlers
│ │ │ ├── catalog.go # Catalog handlers with pagination
│ │ │ ├── cart.go # Cart handlers
│ │ │ └── orders.go # Order handlers with pagination
│ │ └── response/
│ │ └── response.go # API responses + pagination
│ └── utils/
│ └── id.go # ID generation utilities
├── .dockerignore # Docker build exclusions
├── .env.example # Environment template
├── .gitignore # Git exclusions
├── API.md # Complete API documentation
├── docker-compose.yml # Docker orchestration
├── docker-start.sh # Docker deployment script
├── docker-stop.sh # Docker stop script
├── Dockerfile # Multi-stage build config
├── DOCKER.md # Docker deployment guide
├── go.mod # Go module dependencies
├── go.sum # Dependency checksums
├── GOOGLE_OAUTH.md # Google OAuth setup guide
├── README.md # This file
├── ROUTES.md # Complete API routes with permissions
├── setup.sh # Local development setup
├── stop.sh # Local process stop script
└── test-oauth.html # OAuth testing page
- Docker & Docker Compose (recommended) OR
- Go 1.23+ and PostgreSQL/MySQL/SQL Server
- Git
# Clone the repository
git clone https://github.com/devchuckcamp/gocommerce-api.git
cd gocommerce-api
# Copy and configure environment
cp .env.example .env
# Edit .env with your Google OAuth credentials (optional)
# Start services with Docker
./docker-start.sh
# API will be available at http://localhost:8080
# Stop services
./docker-stop.sh# Clone the repository
git clone https://github.com/devchuckcamp/gocommerce-api.git
cd gocommerce-api
# Run setup script
./setup.sh
# Edit .env with your configuration
# IMPORTANT: Set JWT_SECRET (min 32 chars) and database credentials
nano .env
# Create database (PostgreSQL example)
psql -U postgres -c "CREATE DATABASE commerce;"
# Run the application (migrations run automatically)
go run cmd/api/main.go
# Stop with Ctrl+C or use
./stop.sh- Setup environment variables
cp .env.example .env
# Edit .env with your database credentials and JWT secret- Generate a secure JWT secret
# Use a random string generator or:
openssl rand -base64 32- Set up your database
Create a database for the application:
PostgreSQL:
CREATE DATABASE gocommerce;MySQL:
CREATE DATABASE gocommerce CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;- Run migrations and seed (optional)
Migrations run automatically on startup using the official migration systems from both libraries:
- goauthx migrations: Creates authentication tables (users, roles, permissions, refresh_tokens, etc.)
- gocommerce migrations: Creates e-commerce tables (products, categories, brands, carts, orders, etc.)
To seed with sample data:
export SEED_DB=true
# or in .env file: SEED_DB=trueThe seeding process will populate:
- Sample products (laptops, phones, tablets)
- Categories (electronics, computers, accessories)
- Brands (Apple, Dell, Lenovo, HP, Samsung)
- Run the application
go run cmd/api/main.goThe API will be available at http://localhost:8080
Complete API documentation with request/response examples, authentication requirements, and role-based permissions is available in ROUTES.md.
| Category | Routes | Auth Required | Roles |
|---|---|---|---|
| Health | GET /health |
No | - |
| Auth | 6 endpoints | Mixed | - |
| Catalog | 5 endpoints | No | - |
| Cart | 5 endpoints | Yes | Any user |
| Orders | 3 endpoints | Yes | Any user / Admin |
| Admin RBAC | 16 endpoints | Yes | admin, manager, customer_experience |
Total: 37 API endpoints
For detailed documentation including:
- Request/response formats with examples
- Role and permission requirements per endpoint
- Error codes and handling
See ROUTES.md
curl -X POST http://localhost:8080/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "SecurePassword123!",
"first_name": "John",
"last_name": "Doe"
}'curl -X POST http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "SecurePassword123!"
}'Save the access_token from the response.
curl http://localhost:8080/api/v1/catalog/productscurl -X POST http://localhost:8080/api/v1/cart/items \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-d '{
"product_id": "prod-1",
"quantity": 2
}'curl -X POST http://localhost:8080/api/v1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-d '{
"shipping_address": {
"first_name": "John",
"last_name": "Doe",
"address1": "123 Main St",
"city": "New York",
"state": "NY",
"postal_code": "10001",
"country": "US",
"phone_number": "555-0100"
}
}'The application supports multiple database engines through GORM:
DB_DRIVER=postgres
DB_DSN=postgres://user:password@localhost:5432/gocommerce?sslmode=disableDB_DRIVER=mysql
DB_DSN=user:password@tcp(localhost:3306)/gocommerce?parseTime=trueDB_DRIVER=sqlserver
DB_DSN=sqlserver://user:password@localhost:1433?database=gocommerce- Domain Layer: Business logic from
gocommercelibrary - Repository Layer: GORM implementations of repository interfaces
- Service Layer: Orchestration of domain services
- HTTP Layer: Gin handlers and middleware
All dependencies are injected at startup, making the application:
- Easy to test
- Easy to swap implementations
- Clear dependency graph
Consistent error responses across all endpoints:
{
"error": {
"code": "error_code",
"message": "Human-readable error message"
}
}Success responses:
{
"data": { ... },
"meta": { ... }
}go test ./...go build -o bin/api cmd/api/main.go
./bin/apiFull Docker deployment is available! See DOCKER.md for complete guide.
Run both database and API in containers:
docker-compose up --buildThis starts:
- PostgreSQL database (postgres:16-alpine)
- Go API service (built from Dockerfile)
- Automatic migrations and health checks
- Access API at http://localhost:8080
Run just PostgreSQL database:
docker-compose up postgres -dConfiguration:
- Username:
commerce - Password:
commerce - Database:
commerce - Port:
5432
For detailed Docker documentation, see DOCKER.md.
This project uses the official migration capabilities from both goauthx and gocommerce libraries:
Creates authentication and authorization tables:
users- User accountsroles- User rolespermissions- Permission definitionsuser_roles- User-role assignmentsrole_permissions- Role-permission assignmentsrefresh_tokens- JWT refresh tokensemail_verifications- Email verification tokenspassword_resets- Password reset tokensschema_migrations- Migration tracking
Creates e-commerce domain tables:
products- Product catalogcategories- Product categoriesbrands- Product brandscarts- Shopping cartscart_items- Cart line itemsorders- Customer ordersproduct_prices- Date-windowed sale pricessuppliers- Supplier management (v0.0.5)product_suppliers- Product-supplier relationships (v0.0.5)inventory_levels- Stock tracking (on_hand, reserved, available) (v0.0.5)inventory_suppliers- Supplier-specific inventory (v0.0.5)inventory_activities- Inventory audit logging (v0.0.5)gocommerce_migrations- Migration tracking
- Automatic Execution: Migrations run automatically when the application starts
- Idempotent: Safe to run multiple times - only applies new migrations
- Version Tracking: Both systems track which migrations have been applied
- Transaction Safety: Each migration runs in a transaction
Migrations run automatically - just start the application:
# Run migrations only (without seeding)
go run cmd/api/main.go
# or
./bin/api.exeTo run migrations AND seed sample data:
# Set environment variable
export SEED_DB=true
# Then run the application
go run cmd/api/main.goOr update your .env file:
SEED_DB=trueThen start the application:
go run cmd/api/main.goThe startup logs will show:
Running goauthx migrations...
✓ goauthx migrations completed successfully
Running gocommerce migrations...
✓ gocommerce migrations completed successfully
Seeding database with sample data...
✓ Database seeded successfully
If you need to check migration status or troubleshoot:
# View all tables created
docker exec goshop-postgres psql -U commerce -d commerce -c "\dt"
# Check migration history
docker exec goshop-postgres psql -U commerce -d commerce -c "SELECT * FROM schema_migrations;"
docker exec goshop-postgres psql -U commerce -d commerce -c "SELECT * FROM gocommerce_migrations;"
# View seeded data
docker exec goshop-postgres psql -U commerce -d commerce -c "SELECT id, name FROM products LIMIT 5;"
docker exec goshop-postgres psql -U commerce -d commerce -c "SELECT id, name FROM categories LIMIT 5;"
docker exec goshop-postgres psql -U commerce -d commerce -c "SELECT id, name FROM brands LIMIT 5;"Sample data is provided through the gocommerce/migrations package. Set SEED_DB=true in your .env file to populate the database with:
- Products: Various electronics (laptops, phones, tablets)
- Categories: Electronics, Computers, Accessories, Audio, Storage
- Brands: Apple, Dell, Lenovo, HP, Samsung
- Variants: Product variations (sizes, colors)
All configuration is done via environment variables:
| Variable | Description | Default | Required |
|---|---|---|---|
PORT |
Server port | 8080 | No |
DB_DRIVER |
Database driver | postgres | Yes |
DB_DSN |
Database connection string | - | Yes |
JWT_SECRET |
JWT signing key (min 32 chars) | - | Yes |
JWT_ACCESS_TOKEN_EXPIRY |
Access token lifetime | 15m | No |
JWT_REFRESH_TOKEN_EXPIRY |
Refresh token lifetime | 168h | No |
GOOGLE_CLIENT_ID |
Google OAuth Client ID | - | No |
GOOGLE_CLIENT_SECRET |
Google OAuth Client Secret | - | No |
GOOGLE_REDIRECT_URL |
OAuth callback URL | http://localhost:8080/api/v1/auth/google/callback | No |
SEED_DB |
Seed database with sample data | false | No |
The API supports Sign in with Google for seamless user authentication. See GOOGLE_OAUTH.md for complete setup guide.
-
Get Google OAuth Credentials
- Go to Google Cloud Console
- Create/select a project
- Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URI:
http://localhost:8080/api/v1/auth/google/callback
-
Configure Environment Variables
GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com GOOGLE_CLIENT_SECRET=your-client-secret GOOGLE_REDIRECT_URL=http://localhost:8080/api/v1/auth/google/callback
-
Test OAuth Flow
- Open
test-oauth.htmlin your browser - Click "Sign in with Google"
- Authorize the application
- Receive JWT tokens
- Open
GET /api/v1/auth/google- Get authorization URLGET /api/v1/auth/google/callback- OAuth callback handler
For detailed integration examples and troubleshooting, see GOOGLE_OAUTH.md.
- Create handler in
internal/http/handlers/ - Add route in
internal/http/server.go - Apply authentication middleware if needed
Database tables for inventory are already created by gocommerce v0.0.5 migrations (suppliers, inventory_levels, inventory_activities). To enable inventory features:
- Create repository implementations for inventory tables
- Implement the
inventory.Serviceinterface from gocommerce - Inject the service into cart and order services (currently
nil)
Implement the payments.Gateway interface from gocommerce and inject it into the order service.
Implement the shipping.RateCalculator interface from gocommerce and inject it into the pricing service.
MIT
- gocommerce - E-commerce domain library
- goauthx - Authentication & authorization library
- Gin - HTTP web framework
- GORM - ORM library