Important
This is a template project intended for educational purposes. While it demonstrates production-ready practices, please thoroughly review and enhance security measures before deploying to production.
A comprehensive production-ready e-commerce backend template built with Go, designed specifically as a learning resource for Go beginners. This project demonstrates industry-standard practices in Go web development using modern tools and frameworks.
- π Authentication System - JWT-based user registration and login
- π¦ Product Management - Complete product catalog system
- π Shopping Cart - Robust shopping cart functionality
- π Order Processing - Order management and tracking
- π³ Payment Integration - Ready for payment gateway integration
- ποΈ Clean Architecture - Industry-standard project structure
- π Detailed Logging - Comprehensive logging system
- βοΈ Easy Configuration - YAML-based configuration management
- π Database Migrations - Structured database schema management
Note
- Go >= 1.16 required
- MySQL >= 8.0 required
- Redis >= 6.0 recommended for session management
- Features Overview
- Tech Stack
- Frontend Implementation
- Project Structure
- Getting Started
- API Documentation
- Development Guide
- Database Schema
- Contributing
- License
- Author
Tip
Each component in our tech stack was chosen for its reliability and widespread adoption in production environments. See our docs for detailed information about each technology.
This project demonstrates two different approaches to frontend implementation, showcasing the evolution from a simple HTML/JS/CSS stack to a modern React application. Both implementations are provided for educational purposes.
The first version demonstrates fundamental web development concepts using vanilla HTML, JavaScript, and CSS.
public/
βββ pages/ # HTML pages
β βββ login.html
β βββ register.html
βββ css/ # Styling
β βββ style.css
βββ js/ # Client-side logic
βββ login.js
βββ register.js
- Pure HTML/JS/CSS implementation
- No build process required
- Direct integration with Go backend
- Simple state management
- Form validation using HTML5 attributes
- Basic error handling
- Tailwind CSS for styling
- No build step required
- Start Go server:
go run cmd/server/main.go
- Access http://localhost:8080
The second version upgrades to a modern React application with enhanced features and better development experience.
frontend/
βββ src/
β βββ components/ # Reusable React components
β βββ pages/ # Page components
β βββ services/ # API services
β βββ utils/ # Utility functions
βββ package.json
βββ vite.config.js
- Modern React with Hooks
- Vite build system
- Component-based architecture
- Centralized state management
- Enhanced routing with react-router-dom
- Advanced form handling
- Axios for API requests
- Tailwind CSS integration
- Install dependencies:
cd frontend
npm install
- Development mode:
npm run dev # Starts Vite dev server
go run cmd/server/main.go # Start backend in another terminal
- Production build:
npm run build
go run cmd/server/main.go
-
Version 1 (HTML/JS/CSS)
- Quick to start
- No build process
- Simple debugging
- Suitable for learning basics
- Limited code reusability
-
Version 2 (React)
- Modern development environment
- Hot module replacement
- Component reusability
- Better state management
- Enhanced developer tools
-
Version 1
- Lighter initial payload
- No JavaScript framework overhead
- Direct DOM manipulation
-
Version 2
- Optimized bundle size
- Virtual DOM for efficient updates
- Better caching capabilities
- Lazy loading support
-
Version 1
- Direct fetch API calls
- Simple error handling
- Basic CORS setup
-
Version 2
- Axios for requests
- Interceptors for auth
- Centralized API services
- Enhanced error handling
-
CORS Issues
- Ensure correct CORS middleware configuration
- Check request headers in browser dev tools
- Verify API endpoints
-
Authentication Flow
- Store JWT token securely
- Handle token expiration
- Implement proper logout
-
Form Handling
- Version 1: Use HTML5 validation
- Version 2: Implement controlled components
- Error Handling
// Version 1
fetch('/api/v1/login', {
// ... fetch config
}).catch(error => {
document.getElementById('error').textContent = error.message;
});
// Version 2
try {
await loginService.login(credentials);
} catch (error) {
setError(error.response?.data?.message || 'Login failed');
}
- API Integration
// Version 1
const response = await fetch('/api/v1/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
});
// Version 2
const authService = {
register: async (userData) => {
const response = await http.post('/api/v1/register', userData);
return response.data;
}
};
-
Start with Version 1 to understand:
- Basic HTML structure
- Form handling
- API integration
- Simple state management
-
Move to Version 2 to learn:
- React components
- Hooks and state management
- Modern build tools
- Advanced routing
-
Compare implementations to understand:
- Code organization
- State management approaches
- API integration patterns
- Build and deployment processes
douyin-mall-go-template/
βββ api/ # API layer
β βββ v1/ # API version 1 handlers
βββ cmd/ # Application entry points
β βββ server/ # Main server application
βββ configs/ # Configuration files
βββ internal/ # Internal packages
β βββ dao/ # Data Access Objects
β βββ middleware/ # HTTP middlewares
β βββ model/ # Data models and DTOs
β βββ routes/ # Route definitions
β βββ service/ # Business logic layer
βββ pkg/ # Reusable packages
β βββ db/ # Database utilities
β βββ logger/ # Logging utilities
β βββ utils/ # Common utilities
βββ frontend/
β βββ src/
β β βββ components/ # Reusable React components
β β βββ pages/ # Page components
β β βββ services/ # API services
β β βββ utils/ # Utility functions
β βββ package.json
β βββ vite.config.js
βββ public/ # Static assets
Important
Before you begin, ensure you have the following installed:
- Go 1.16 or higher
- MySQL 8.0 or higher
- Git
- Make (optional, for using Makefile commands)
- Clone the repository:
git clone https://github.com/ChanMeng666/douyin-mall-go-template.git
cd douyin-mall-go-template
- Install dependencies:
go mod download
or
go mod tidy
- Set up database:
mysql -u root -p < docs/database/douyin_mall_go_template_structure_only.sql
- Configure application:
cp configs/config.yaml.example configs/config.yaml
# Edit configs/config.yaml with your database credentials
- Start the server:
go run cmd/server/main.go
User Registration
POST /api/v1/register
Content-Type: application/json
{
"username": "testuser",
"password": "password123",
"email": "[email protected]",
"phone": "1234567890"
}
Response 200:
{
"message": "registration successful"
}
User Login
POST /api/v1/login
Content-Type: application/json
{
"username": "testuser",
"password": "password123"
}
Response 200:
{
"token": "eyJhbGci...",
"user": {
"id": 1,
"username": "testuser",
"email": "[email protected]"
}
}
Note
Each component is designed to be modular and follows the SOLID principles:
-
api/v1/: HTTP request handlers
health.go
: Health check endpointuser.go
: User-related endpoints
-
internal/middleware/: Custom middleware
auth.go
: JWT authenticationcors.go
: CORS handlinglogger.go
: Request logging
-
internal/model/: Data models
user.go
: User entitydto/
: Data Transfer Objects
-
internal/service/: Business logic
user_service.go
: User-related operations
Tip
Follow these steps to add new features to the project:
- Define routes in
internal/routes/routes.go
- Create handler in
api/v1/
- Implement service logic in
internal/service/
- Define models in
internal/model/
- Add data access in
internal/dao/
Our comprehensive e-commerce database includes:
users
: User accounts and authenticationproducts
: Product catalog managementcategories
: Product categorizationorders
: Order processingorder_items
: Order detailsshopping_cart_items
: Shopping cart managementpayment_records
: Payment trackingproduct_reviews
: User reviews and ratings
We welcome contributions! Please follow these steps:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Chan Meng
- LinkedIn: chanmeng666
- GitHub: ChanMeng666
β Star us on GitHub | π Read the Wiki | π Report an Issue