This project was created to help Node.js developers adapt to the Go ecosystem. As a Node developer transitioning to Go, this repository provides structured comments and patterns that relate Express.js concepts to their Gin framework equivalents, making the learning curve smoother and more intuitive.
- Authentication & Authorization — JWT token-based auth with role-based middleware (admin/user)
- Product Management — Full CRUD operations with admin-only write access
- Shopping Cart — Add, update, remove items with automatic price calculation
- Order Processing — Checkout with atomic stock updates and transaction management
The project follows a clean three-layer architecture pattern:
Handler Layer (HTTP) → Service Layer (Business Logic) → Repository Layer (Database)
| Module | Description |
|---|---|
| Auth | User registration, login, role management |
| Product | Product catalog with inventory tracking |
| Cart | Shopping cart operations |
| Order | Checkout and order processing |
| Category | Technology |
|---|---|
| Language | Go 1.x |
| Framework | Gin |
| Database | PostgreSQL |
| ORM | GORM |
| Authentication | JWT |
| Password Hashing | bcrypt |
- Go 1.x or higher
- PostgreSQL database
- Clone the repository
git clone <repository-url>
cd goxpress- Set up environment variables
Create a .env file in the root directory:
DATABASE_CONFIG=host=localhost user=postgres password=yourpassword dbname=goxpress port=5432 sslmode=disable
APP_PORT=8080
JWT_SECRET=your_jwt_secret_key- Install dependencies
go mod download- Run the application
go run cmd/main.goThe server will start at http://localhost:8080
| Method | Endpoint | Description | Access |
|---|---|---|---|
POST |
/api/v1/auth/login |
User login | Public |
POST |
/admin/register |
Admin registration | Public |
POST |
/user/register |
User registration | Public |
{
"username": "john_doe",
"password": "password123"
}{
"username": "john_doe",
"email": "john@example.com",
"password": "password123"
}| Method | Endpoint | Description | Access |
|---|---|---|---|
GET |
/api/v1/product |
List all products | Public |
GET |
/api/v1/product/:id |
Get product by ID | Public |
POST |
/api/v1/product |
Create product | Admin only |
PUT |
/api/v1/product/:id |
Update product | Admin only |
DELETE |
/api/v1/product/:id |
Delete product | Admin only |
{
"name": "Product Name",
"description": "Product description",
"price": 10000,
"stock": 50
}All cart endpoints require user authentication.
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/v1/cart |
Get user's cart |
POST |
/api/v1/cart/add |
Add item to cart |
PUT |
/api/v1/cart/item/:id |
Update cart item |
DELETE |
/api/v1/cart/item/:id |
Remove cart item |
{
"product_id": 1,
"quantity": 2
}{
"quantity": 5
}All order endpoints require user authentication.
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/v1/order/checkout |
Process checkout |
type Product struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt time.Time
Name string
Stock int
Price float64
Description string
}type Transaction struct {
ID uint
UserID uint
TotalPrice float64
Payment Payment
OrderItems []OrderItem
}type OrderItem struct {
ID uint
TransactionID uint
ProductID uint
Quantity int
PriceAtTime float64
}type Cart struct {
ID uint
UserID uint
CartItems []CartItem
}type CartItem struct {
ID uint
CartID uint
ProductID uint
Quantity int
Price float64
}goxpress/
├── cmd/
│ └── main.go # Application entry point
├── internal/
│ ├── auth/ # Authentication module
│ │ ├── handlers/ # HTTP handlers
│ │ ├── services/ # Business logic
│ │ ├── repository/ # Data access layer
│ │ └── auth.go # Route setup
│ ├── product/ # Product module
│ │ ├── handlers/
│ │ ├── services/
│ │ ├── repository/
│ │ ├── model/ # Product model
│ │ └── product.go
│ ├── cart/ # Cart module
│ │ ├── handlers/
│ │ ├── services/
│ │ ├── repository/
│ │ ├── model/
│ │ └── cart.go
│ ├── order/ # Order module
│ │ ├── handlers/
│ │ ├── services/
│ │ ├── repository/
│ │ ├── model/
│ │ └── order.go
│ ├── db/ # Database connection
│ │ └── db.go
│ └── middleware/ # Auth middleware
│ └── middleware.go
├── pkg/
│ └── util/ # Utility functions
│ └── jwt.go # JWT helper
├── .env # Environment variables
├── go.mod # Go module file
└── go.sum # Go dependencies
- JWT tokens are stored in HTTP-only cookies for security
- Tokens include user role information for authorization
- Token expiration is enforced
- Passwords are hashed using bcrypt with default cost
- Plain text passwords are never stored in the database
The application implements middleware for role-based authorization:
middleware.RoleMiddleware("admin") // Admin-only routes
middleware.RoleMiddleware("user") // User-only routes- Product creation, updates, and deletion require admin role
- Cart operations require user authentication
- Order processing requires user authentication
Handler Layer
- Receives HTTP requests
- Validates request data
- Calls service layer methods
- Returns HTTP responses
Service Layer
- Contains business logic
- Coordinates between repositories
- Handles transactions
- Implements validation rules
Repository Layer
- Direct database operations
- GORM query implementations
- Data persistence logic
The Cart and Order modules consume Product repository services for:
- Price lookups during cart operations
- Stock validation before checkout
- Atomic stock updates during order processing
All modules are initialized during application startup:
- Load environment variables
- Connect to PostgreSQL database
- Initialize Gin router
- Set up module repositories
- Wire services with dependencies
- Register HTTP routes
- Start server
go test ./...go build -o goxpress cmd/main.go
./goxpressThe application uses GORM's AutoMigrate feature. Models are automatically migrated when the application starts.
Built with Go and Gin Framework