1. Sport API
Sport
  • Sport Site
    • Tài liệu triển khai dự án Sport Site
    • Tài liệu triển khai dự án Landing Page
    • Tài liệu triển khai dự án Newslooks
  • Sport API
    • Tài liệu triển khai dự án KSport API
    • Tài liệu triển khai dự án WSport API
    • Tài liệu triển khai dự án WSport API Game
  • Sport DB
    • Tài liệu triển khai dự án Sport DB
  • Sport Admin
    • Tài liệu triển khai dự án Sport Admin
  1. Sport API

Tài liệu triển khai dự án WSport API

Tài Liệu Triển Khai wsport-api — Golang trên Docker#

   
Triển Khai Golang API Services trên Docker với AWS
  

🔎 Tổng Quan#

Dự án wsport-api chứa 4 thành phần API viết bằng Golang, chạy trên Docker và quản lý bởi Supervisor. Tất cả các thành phần chia sẻ chung 1 repository source code (submodule) nhưng build thành các Docker image riêng biệt trên AWS ECR.

Hạ Tầng Chính#

Thành phầnDịch vụ AWSMục đích
Container ImagesAWS ECRRegistry Docker image riêng tư
Process ManagerSupervisorQuản lý tiến trình Go binary trong container

4 Thành Phần API#

Thành phầnECR RepositorySource BranchServicesGo Version
internalsports/wsport-api-internal-prodwcip-rapid-devapp + cronGo 1.24
commercialsports/wsport-api-commercial-prodwcip-rapid-devapp + cronGo latest
wacpsports/wsport-api-wacp-prodwcip-rapid-devapp + cronGo 1.24
websocketsports/wsport-websocket-internal-prodwcip-rapid-devapp onlyGo 1.23

Base Images#

Mỗi thành phần sử dụng base image riêng được build từ Dockerfile-wsport-api-base:
Thành phầnBase Image ECR
internalsports/wsport-api-internal-base-prod:latest
commercialsports/wsport-api-commercial-base-prod:latest
wacpsports/wsport-api-wacp-prod:base-latest
websocketsports/wsport-websocket-internal-base-prod:latest

📖 Tài Liệu Tham Khảo#

Bạn có thể xem tài liệu tại docs.docker.com và Go Documentation.

🏗 Tổng Quan Kiến Trúc#

Cấu Trúc Thư Mục#

wsport-api/
├── build/
│   ├── Dockerfile-wsport-api-base                        # Base image chung (Debian + Supervisor)
│   ├── Dockerfile-wsport-api-internal.production         # Build image: internal (app + cron + searchsync)
│   ├── Dockerfile-wsport-api-commercial.production       # Build image: commercial (app + cron)
│   ├── Dockerfile-wsport-api-wacp.production             # Build image: wacp (app + cron)
│   ├── Dockerfile-wsport-websocket-internal.production   # Build image: websocket (app only)
│   ├── conf/                                             # Cấu hình Supervisor & TLS certs
│   └── source/                                           # Git submodule → source code
├── .env-wsport-api-internal.production                   # Biến môi trường production — internal
├── .env-wsport-api-commercial.production                 # Biến môi trường production — commercial
├── .env-wsport-api-wacp.production                       # Biến môi trường production — wacp
├── .env-wsport-websocket-internal.production             # Biến môi trường production — websocket
├── docker-compose.yml-wsport-api-internal.production     # Docker Compose — internal
├── docker-compose.yml-wsport-api-commercial.production   # Docker Compose — commercial
├── docker-compose.yml-wsport-api-wacp.production         # Docker Compose — wacp
├── docker-compose.yml-wsport-websocket-internal.production # Docker Compose — websocket
└── docs/                                                 # Tài liệu

Pipeline Build Multi-Stage#

internal (phức tạp nhất — 3 build stages + 2 final stages)#

┌──────────────────────────────────────────────────────────────────────────┐
│                   PIPELINE BUILD MULTI-STAGE — INTERNAL                  │
│                                                                          │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────────┐                │
│  │   build_app  │  │  build_cron  │  │ build_searchsync │                │
│  │              │  │              │  │                  │                │
│  │ • Go 1.24    │  │ • Go 1.24    │  │ • Go 1.24        │                │
│  │ • sqlc gen   │  │ • sqlc gen   │  │ • sqlc gen       │                │
│  │ • go build   │  │ • go build   │  │ • 9 binaries     │                │
│  │   → app bin  │  │   → cron bin │  │   → man*,        │                │
│  │              │  │              │  │     clear*       │                │
│  └──────┬───────┘  └──────┬───────┘  └───────┬──────────┘                │
│         │                 │                  │                           │
│         ▼                 │                  ▼                           │
│  ┌──────────────┐         │  ┌──────────────────────┐                    │
│  │     app      │         │  │   (merged into app)  │                    │
│  │ (BASE_IMAGE) │◄────────┘  │   app + searchsync   │                    │
│  │              │            │   binaries           │                    │
│  │ • Supervisor │            └──────────────────────┘                    │
│  │ • app binary │                                                        │
│  │ • searchsync │         ┌──────────────┐                               │
│  └──────┬───────┘         │     cron     │                               │
│         │                 │ (BASE_IMAGE) │                               │
│         ▼                 │              │                               │
│  ┌──────────────┐         │ • Supervisor │                               │
│  │   ECR Push   │         │ • cron binary│                               │
│  │ :app-latest  │         └──────┬───────┘                               │
│  └──────────────┘                │                                       │
│                                  ▼                                       │
│                           ┌──────────────┐                               │
│                           │   ECR Push   │                               │
│                           │ :cron-latest │                               │
│                           └──────────────┘                               │
└──────────────────────────────────────────────────────────────────────────┘

commercial / wacp (2 build stages + 2 final stages)#

┌──────────────────────────────────────────────────────────────────────────┐
│               PIPELINE BUILD MULTI-STAGE — COMMERCIAL / WACP             │
│                                                                          │
│  ┌──────────────┐                ┌──────────────┐                        │
│  │   build_app  │                │  build_cron  │                        │
│  │              │                │              │                        │
│  │ • Go build   │                │ • Go build   │                        │
│  │ • sqlc gen*  │                │ • sqlc gen*  │                        │
│  │   → app bin  │                │   → cron bin │                        │
│  └──────┬───────┘                └──────┬───────┘                        │
│         │                               │                                │
│         ▼                               ▼                                │
│  ┌──────────────┐                ┌──────────────┐                        │
│  │     app      │                │     cron     │                        │
│  │ (BASE_IMAGE) │                │ (BASE_IMAGE) │                        │
│  │ • Supervisor │                │ • Supervisor │                        │
│  └──────┬───────┘                └──────┬───────┘                        │
│         ▼                               ▼                                │
│  ┌──────────────┐                ┌──────────────┐                        │
│  │   ECR Push   │                │   ECR Push   │                        │
│  │ :app-latest  │                │ :cron-latest │                        │
│  └──────────────┘                └──────────────┘                        │
│                                                                          │
│  * sqlc generate chỉ dùng cho wacp, commercial không sử dụng             │
└──────────────────────────────────────────────────────────────────────────┘

websocket (1 build stage + 1 final stage)#

┌──────────────────────────────────────────────────────────────────────────┐
│                   PIPELINE BUILD — WEBSOCKET                             │
│                                                                          │
│  ┌──────────────┐                                                        │
│  │   build_app  │                                                        │
│  │              │                                                        │
│  │ • Go 1.23    │                                                        │
│  │ • go build   │                                                        │
│  │   → app bin  │                                                        │
│  └──────┬───────┘                                                        │
│         │                                                                │
│         ▼                                                                │
│  ┌──────────────┐                                                        │
│  │     app      │                                                        │
│  │ (BASE_IMAGE) │                                                        │
│  │ • Supervisor │                                                        │
│  └──────┬───────┘                                                        │
│         ▼                                                                │
│  ┌──────────────┐                                                        │
│  │   ECR Push   │                                                        │
│  │ :app-latest  │                                                        │
│  └──────────────┘                                                        │
└──────────────────────────────────────────────────────────────────────────┘

Kiến Trúc Runtime Production#

┌─────────────────────────────────────────────────────────────────────────────────────┐
│                          MÔI TRƯỜNG PRODUCTION AWS                                  │
│                                                                                     │
│  ┌──────────────────────────────────────┐  ┌──────────────────────────────────────┐ │
│  │          Docker Host                 │  │            DỊCH VỤ AWS               │ │
│  │                                      │  │                                      │ │
│  │  ┌────────────────────────────────┐  │  │  ┌──────────────────────────────┐    │ │
│  │  │   internal.app Container       │  │  │  │       AWS ECR                │    │ │
│  │  │                                │  │  │  │  • :app-latest (×4)          │    │ │
│  │  │  • Go API binary               │  │  │  │  • :cron-latest (×3)         │    │ │
│  │  │  • Supervisor managed          │  │  │  │  • base images (×4)          │    │ │
│  │  │  • .env mounted (RO)           │  │  │  └──────────────────────────────┘    │ │
│  │  │  • TLS cert mounted (RO)       │  │  │                                      │ │
│  │  │  • Port 51011 exposed          │  │  │                                      │ │
│  │  └────────────────────────────────┘  │  │                                      │ │
│  │                                      │  │                                      │ │
│  │  ┌────────────────────────────────┐  │  │                                      │ │
│  │  │   internal.cron Container      │  │  │                                      │ │
│  │  │                                │  │  │                                      │ │
│  │  │  • Go cron binary              │  │  │                                      │ │
│  │  │  • SP_ENABLE_CRON=false        │  │  │                                      │ │
│  │  │  • .env mounted (RO)           │  │  │                                      │ │
│  │  └────────────────────────────────┘  │  │                                      │ │
│  │                                      │  │                                      │ │
│  │  ┌────────────────────────────────┐  │  │                                      │ │
│  │  │   commercial.app Container     │  │  │                                      │ │
│  │  │   commercial.cron Container    │  │  │                                      │ │
│  │  │   (tương tự internal)          │  │  │                                      │ │
│  │  └────────────────────────────────┘  │  │                                      │ │
│  │                                      │  │                                      │ │
│  │  ┌────────────────────────────────┐  │  │                                      │ │
│  │  │   wacp.app / wacp.cron         │  │  │                                      │ │
│  │  │   websocket.app                │  │  │                                      │ │
│  │  └────────────────────────────────┘  │  │                                      │ │
│  │                                      │  │                                      │ │
│  └──────────────────────────────────────┘  │                                      │ │
│                                            │──────────────────────────────────────┘ │
│  Networks: prod-network, backend-network   │                                        │
│            proxy-network (wacp)            │                                        │
└────────────────────────────────────────────┴────────────────────────────────────────┘

📦 Yêu Cầu Tiên Quyết#

Công cụ cần thiết#

Công cụPhiên bản tối thiểuMục đích
Docker≥ 24.0.0Build và chạy container
Docker Compose≥ 2.20.0Điều phối nhiều container
AWS CLI≥ 2.0Xác thực và push lên AWS ECR
Git≥ 2.30Quản lý source code & submodules

Yêu cầu AWS#

Tài khoản AWS với quyền truy cập ECR
ECR repositories đã tạo cho mỗi thành phần (app + cron + base)
AWS CLI đã cấu hình credentials

⚙ Cấu Hình#

Bước 1: Clone Repository và Khởi Tạo Submodule#

Bước 2: Cấu Hình Biến Môi Trường#

Mỗi thành phần có file .env riêng. Copy file mẫu production tương ứng:

internal#

commercial#

wacp#

websocket#

Bước 3: Cấu Hình Source .env#


🏭 Build & Đẩy Image Lên AWS ECR#

Bước 1: Đăng Nhập AWS ECR#

Bước 2: Build Base Image (nếu cần cập nhật)#

Base image chứa Debian + Supervisor + các package cơ bản:
Lưu ý: Lặp lại cho mỗi base image của các thành phần khác (commercial, wacp, websocket) nếu cần.

Bước 3: Build & Push Image Cho Từng Thành Phần#

3.1 internal#

3.2 commercial#

3.3 wacp#

3.4 websocket#


🚀 Triển Khai Lên Production#

Bước 1: Chuẩn Bị Trên Server Production#

Bước 2: Đăng Nhập ECR Và Pull Images Mới#

Bước 3: Triển Khai Từng Thành Phần#

3.1 Triển khai internal#

3.2 Triển khai commercial#

3.3 Triển khai wacp#

3.4 Triển khai websocket#

Bước 4: Kiểm Tra Trạng Thái#


📋 Tham Khảo Cấu Hình Runtime#

Container Services#

Thành phầnServiceImage SourcePortMục đích
internal*.appECR :app-latest51011API chính + searchsync binaries
internal*.cronECR :cron-latest—Scheduled tasks
commercial*.appECR :app-latest51010Commercial API
commercial*.cronECR :cron-latest—Scheduled tasks + leftovers cleanup
wacp*.appECR :app-latest51011WACP API
wacp*.cronECR :cron-latest—Scheduled tasks
websocket*.appECR :app-latest51011WebSocket server

Volume Mounts#

Đường dẫn mount (Container)NguồnChế độMục đích
/app/.envbuild/source/.envROBiến môi trường ứng dụng
/app/tls/ca.crtbuild/conf/cert/ca.crtROTLS certificate (internal, wacp)
Lưu ý: Websocket không mount TLS cert. Wacp mount .env từ ${SPORT_SOURCE}/.env thay vì ${SPORT_ENV}.

Networks#

Thành phầnNetworks
internalprod-network, backend-network
commercialprod-network, backend-network
wacpproxy-network
websocketprod-network

Biến Môi Trường Đặc Biệt#

BiếnThành phầnServiceGiá trị mặc địnhMục đích
SP_ENABLE_CRONall (×3)cronfalseBật/tắt cron scheduler
SP_LEFTOVERS_INTERVAL_SECScommercialcron43200Interval cleanup leftovers (12h)
Modified at 2026-03-23 04:37:24
Previous
Tài liệu triển khai dự án KSport API
Next
Tài liệu triển khai dự án WSport API Game
Built with