- Backend: https://github.com/Web-Social-Platform/Web_Backend
- Frontend_User: https://github.com/Web-Social-Platform/Web_Frontend
- Frontend_Admin: https://github.com/Web-Social-Platform/Management_Frontend
Backend repository for Software Engineering project, built with Go, Gin framework, MySQL, Redis, RabbitMQ, and Docker.
api.backend.xjco2013/
├── cmd/ # Application entry point
├── config/ # Configuration files
├── middleware/ # HTTP middleware
├── controller/ # Request handlers
│ └── dto/ # Data Transfer Objects
├── service/ # Business logic layer
│ └── sdto/ # Service Data Transfer Objects
├── dao/ # Data Access Objects
└── util/ # Utility functions
The cmd directory contains the package main and serves as the application entry point. It includes:
- Main function
- Router initialization
- Essential resource initialization at startup
The config directory stores various configuration files, including:
- Database configuration (credentials, connection strings)
- Environment variables
- JWT encryption keys
- Docker configuration files
Sensitive information is stored as configuration rather than hardcoded in the source code to enhance security. Some files in this directory are excluded from version control.
The middleware directory contains custom middleware implementations for:
- Authentication and authorization
- Rate limiting
- Context enrichment
- Request preprocessing
After passing through middleware, requests arrive at the controller layer. The controller layer is responsible for:
- Receiving request objects
- Validating request parameters
- Business logic validation (e.g., checking for required parameters)
- Calling service layer methods
- Wrapping business data for responses
Note: The controller layer focuses on request validation and data packaging rather than implementing business logic.
DTOs encapsulate request parameters into structured objects for easier management and passing between layers.
Example:
Given the following request body:
{
"id": 1,
"name": "xiaofei",
"age": "20"
}We define a corresponding DTO:
// Naming convention: xxxReq
type UserReq struct {
ID int `json:"id"`
Name string `json:"name"`
Age string `json:"age"`
}The service layer is the core of the application, containing the majority of business logic. It:
- Receives packaged business data from the controller layer
- Processes business logic
- Transforms business data into database models
- Passes data to the DAO layer
Similar to DTOs, SDTOs encapsulate data for transfer between layers:
- DTO: HTTP Request → Controller
- SDTO: Controller → Service
DAO (Data Access Object) layer encapsulates database operations. In this project, this layer is generated using framework scaffolding tools for convenience and consistency.
The util directory contains global utility functions and helper methods used throughout the application.
┌─────────────────────────────────────────────────────────────┐
│ HTTP Request │
└─────────────────────────┬───────────────────────────────────┘
│
▼
┌───────────────┐
│ Middleware │
│ │
│ • Auth │
│ • Rate Limit │
│ • Context │
└───────┬───────┘
│
▼
┌───────────────┐
│ Controller │◄──── DTO
│ │
│ • Validate │
│ • Package │
└───────┬───────┘
│
▼
┌───────────────┐
│ Service │◄──── SDTO
│ │
│ • Business │
│ Logic │
└───────┬───────┘
│
▼
┌───────────────┐
│ DAO │◄──── Model
│ │
│ • Database │
│ Operations │
└───────┬───────┘
│
▼
┌───────────────┐
│ Database │
│ (MySQL) │
└───────────────┘
- HTTP Request → Enters the application
- Middleware → Performs authentication, rate limiting, and context setup
- Controller → Validates request parameters, wraps data into DTOs
- Service → Executes business logic, processes SDTOs
- DAO → Performs database operations with Models
- Database → Persists and retrieves data
- Go 1.x or higher
- MySQL
- Redis
- RabbitMQ
- Docker (optional)
# Clone the repository
git clone https://github.com/yourusername/api.backend.xjco2013.git
# Navigate to project directory
cd api.backend.xjco2013
# Install dependencies
go mod download
# Configure environment
cp config/example.env config/.env
# Edit config/.env with your settings
# Run the application
go run cmd/main.goPlease read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.