Skip to content

Web-Social-Platform/Web_Backend

Repository files navigation

web.backend

golang gin mysql redis rabbitmq docker

🔗 Repository

GitHub Organization

Overview

Backend repository for Software Engineering project, built with Go, Gin framework, MySQL, Redis, RabbitMQ, and Docker.

Project Structure

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

Directory Descriptions

1. cmd

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

2. config

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.

3. middleware

The middleware directory contains custom middleware implementations for:

  • Authentication and authorization
  • Rate limiting
  • Context enrichment
  • Request preprocessing

4. controller

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.

DTO (Data Transfer Object)

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"`
}

5. service

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
SDTO (Service Data Transfer Object)

Similar to DTOs, SDTOs encapsulate data for transfer between layers:

  • DTO: HTTP Request → Controller
  • SDTO: Controller → Service

6. dao

DAO (Data Access Object) layer encapsulates database operations. In this project, this layer is generated using framework scaffolding tools for convenience and consistency.

7. util

The util directory contains global utility functions and helper methods used throughout the application.

Architecture

Layer Interaction Diagram

┌─────────────────────────────────────────────────────────────┐
│                         HTTP Request                         │
└─────────────────────────┬───────────────────────────────────┘
                          │
                          ▼
                  ┌───────────────┐
                  │  Middleware   │
                  │               │
                  │ • Auth        │
                  │ • Rate Limit  │
                  │ • Context     │
                  └───────┬───────┘
                          │
                          ▼
                  ┌───────────────┐
                  │  Controller   │◄──── DTO
                  │               │
                  │ • Validate    │
                  │ • Package     │
                  └───────┬───────┘
                          │
                          ▼
                  ┌───────────────┐
                  │   Service     │◄──── SDTO
                  │               │
                  │ • Business    │
                  │   Logic       │
                  └───────┬───────┘
                          │
                          ▼
                  ┌───────────────┐
                  │     DAO       │◄──── Model
                  │               │
                  │ • Database    │
                  │   Operations  │
                  └───────┬───────┘
                          │
                          ▼
                  ┌───────────────┐
                  │   Database    │
                  │  (MySQL)      │
                  └───────────────┘

Data Flow

  1. HTTP Request → Enters the application
  2. Middleware → Performs authentication, rate limiting, and context setup
  3. Controller → Validates request parameters, wraps data into DTOs
  4. Service → Executes business logic, processes SDTOs
  5. DAO → Performs database operations with Models
  6. Database → Persists and retrieves data

Getting Started

Prerequisites

  • Go 1.x or higher
  • MySQL
  • Redis
  • RabbitMQ
  • Docker (optional)

Installation

# 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.go

Contributing

Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages