1. Architecture
Search Tool
  • Tổng quan
  • Bussiness | Logic
    • Nghiệp vụ & Luồng Tìm kiếm
    • Task Orchestration
    • Scraping & Execution
    • Exception & Recovery
    • Proxy Orchestration
  • Project
    • Search API
      • Tổng quan Search API
      • Architecture
        • Database Schema
        • System Architecture
        • Code Structure
      • API Interface
        • Go - Auth
          • Đăng nhập
          • Đăng ký (Public)
        • Go - User
          • Danh sách User (Phân trang)
          • Tạo User
          • Lấy tất cả User
          • Profile cá nhân
          • Chi tiết User
          • Cập nhật User
          • Xóa User
          • Đổi mật khẩu
        • Go - System
          • Nhật ký hoạt động (Phân trang)
          • Cập nhật dữ liệu Search
          • Test Webhook receiver
        • Go - Server
          • Danh sách Server (Phân trang)
          • Tạo Server
          • Lấy tất cả Server
          • Chi tiết Server
          • Cập nhật Server
          • Xóa Server
        • Go - Tasks
          • Tạo nhiều task Search
          • Lấy tất cả task Search
          • Reset trạng thái tất cả nhiệm vụ Search
          • Tạo nhiều task Anchor Text
          • Lấy tất cả nhiệm vụ Anchor Text
          • Reset trạng thái tất cả nhiệm vụ Anchor Text
        • Go - Webhook
          • Webhook cập nhật trạng thái (Dùng WEBHOOK_KEY)
          • Lấy Proxy cho Tool
          • Lấy Proxy có thể rotate
          • Cập nhật kết quả Search
          • Kết quả rotate Proxy
          • Cập nhật kết quả Anchor Text
          • Nhận kết quả từ Tool (v2)
      • Deployment
        • Local
        • Staging
        • Product
    • Search tool
      • Tổng quan Search Tool
      • Architecture
        • Database Schema
        • System Architecture
        • Code Structure
      • API Interface
        • Bun - Main
          • Thông tin server Bun
          • Health Check
          • Lấy file Log
        • Bun - Task
          • Danh sách Search Tasks
          • Tạo nhiều Search Tasks
          • Xóa tất cả Search Tasks
          • Xóa Search Task
          • Reset trạng thái Search
          • Reset running process
          • Danh sách Anchor Tasks
          • Tạo nhiều Anchor Tasks
          • Xóa tất cả Anchor Tasks
          • Xóa Anchor Task
          • Reset trạng thái Anchor
          • Reset running process
      • Deployment
        • Local
        • Staging
        • Product
  • Schemas
    • LoginRequest
    • ProxyRequest
    • CreateUserRequest
    • KeywordTaskRequest
    • UpdateUserRequest
    • SearchAutomationRequest
    • ChangePasswordRequest
    • CreateServerRequest
    • AnchorTextTaskRequest
  1. Architecture

Code Structure

Code Structure — Search Tool API#

1. Cây thư mục tổng quan#

api/
├── cmd/
│   └── main.go                  # Entry point khởi động server
├── bootstrap/
│   ├── routes.go                # Đăng ký tất cả HTTP routes
│   ├── cron/                    # Chạy các cron job khi khởi động
│   ├── injection/
│   │   └── injection.go         # DI container (uber/dig)
│   └── socket/                  # Socket.IO bootstrap
├── internal/                    # Business logic (module-based)
│   ├── user/                    # Module quản lý user
│   ├── server/                  # Module quản lý worker server
│   ├── anchor_text/             # Module anchor text task
│   ├── keyword_pool_task/       # Module keyword search task (= /search/)
│   ├── activity_log/            # Ghi log hoạt động (không có public route)
│   ├── webhook/                 # Webhook handler (callback từ worker)
│   ├── keyword/                 # [Không dùng nữa]
│   ├── proxy/                   # [Dùng nội bộ trong webhook]
│   ├── keyword_result/          # [Không dùng nữa]
│   └── keyword_pool_task/       # Task pool cho /search/
├── pkg/                         # Shared packages
│   ├── env.go                   # Đọc cấu hình .env
│   ├── api/                     # HTTP client đến external services
│   ├── apperror/                # Error response builder
│   ├── cache/                   # Redis cache wrapper
│   ├── database/                # DB connection (MySQL + MongoDB)
│   ├── enum/                    # Constants & enums
│   ├── helper/                  # Utility helpers
│   ├── jwt/                     # JWT helper
│   ├── logger/                  # Logger (zap)
│   ├── messages/                # Response builder
│   ├── middleware/              # HTTP middleware
│   ├── scheduler/               # Scheduler base
│   ├── telegram/                # Telegram notification (optional)
│   ├── time/                    # Time utilities
│   ├── types/                   # Shared types
│   └── validate/                # Validation wrapper
├── common/                      # Shared structs (Paging, ...)
├── public/                      # Static files
├── logs/                        # Log files (auto-created)
├── .env                         # Environment config (không commit)
├── .env.example                 # Template cấu hình
├── docker-compose.yaml          # Docker services
└── go.mod / go.sum              # Go module dependencies

2. Pattern kiến trúc module#

Mỗi module trong internal/ tuân theo cấu trúc layered architecture:
internal/<module>/
├── <module>.go       # Model / Entity struct (GORM hoặc BSON)
├── dto/              # Data Transfer Objects (request/response schema)
├── handler/          # HTTP handler layer (nhận request, gọi service)
│   ├── handler.go    # Khởi tạo handler, đăng ký routes
│   └── *.go          # Mỗi file = 1 action
├── service/          # Business logic layer
│   └── service.go    # Hoặc nhiều file per action
└── repository/       # Data access layer (DB queries)
    └── repository.go
Quy tắc:
Handler không trực tiếp gọi DB, chỉ gọi Service.
Service không biết HTTP request, chỉ nhận DTO thuần.
Repository chứa toàn bộ queries, trả về entity.

pkg/middleware/#

FileMiddleware
jwt_middleware.goKiểm tra Bearer JWT token
search_middleware.goKiểm tra Authorization: <API_SEARCH_TOKEN>
webhook_middleware.goKiểm tra Webhook Key
error_handler.goGlobal error → JSON response

pkg/messages/#

Builder cho response chuẩn:
Success(message) → { success: true, message }
SuccessSimpleData(&data, message) → { success: true, message, data }
SuccessDataPagination(&data, &paging, message) → có pagination

pkg/apperror/#

Error builders:
BadRequest(msg) → HTTP 400
Unauthorized(msg) → HTTP 401
NotFound(msg) → HTTP 404
InternalServerError(msg) → HTTP 500

9. bootstrap/injection/injection.go#

File quan trọng nhất cho DI — đăng ký tất cả components:
Thêm service mới: khai báo NewService(deps...) *Service và thêm vào slice services trong ProvideComponents().
Ngày cập nhật 2026-03-31 02:08:44
Trước
System Architecture
Tiếp theo
Đăng nhập
Built with