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
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/#
| File | Middleware |
|---|
jwt_middleware.go | Kiểm tra Bearer JWT token |
search_middleware.go | Kiểm tra Authorization: <API_SEARCH_TOKEN> |
webhook_middleware.go | Kiểm tra Webhook Key |
error_handler.go | Global 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/#
BadRequest(msg) → HTTP 400
Unauthorized(msg) → HTTP 401
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