Chuyển tới nội dung chính

Quickstart

TL;DR
  • Cần Docker, Go 1.27+, JDK 25 (JAVA_HOME trỏ tới JDK 25), Node 22 (chỉ cho trang docs này).
  • Cách nhanh nhất: make stack-up chạy toàn bộ trong container, trong các service của Votee chỉ gateway :8000 mở ra ngoài (PostgreSQL, Kafka, Redis vẫn mở 15432, 19092, 16379); make smoke chạy luồng đầy đủ từ bên trong network. Để gỡ lỗi từng process: make infra-up && make migrate && make kafka-topics, rồi chạy từng process trong terminal riêng.
  • Client luôn gọi gateway localhost:8000; /internal/** không đi qua gateway và cần X-Votee-Internal-Token.
  • Port host tránh đụng service khác: PostgreSQL 15432, Kafka 19092, Redis 16379, gateway 8000.
  • Cần biết trước: Tổng quan Votee. Không bắt buộc đọc hết nghiệp vụ trước khi chạy thử.
  • Từ khoá: docker compose, make stack-up, smoke test, gateway, Idempotency-Key.

Trang này đưa hệ thống chạy trên máy để xem các khái niệm ở Tổng quan hoạt động thật. Mục 3 đi lại một vòng đời tối giản: tạo campaign, vote, xem ranking, rồi mua credit. Mỗi lệnh curl ở đó tương ứng với một chặng của hành trình một vote.

1. Hạ tầng​

make infra-up # PostgreSQL 17, Kafka (KRaft), Redis và gateway Envoy (:8000), chờ healthy
make migrate # schema voting + ranking (goose, embedded)
make kafka-topics # topic + .dlq, auto-create của broker đã tắt

deploy/postgres/init/01-schemas.sql tạo một role và một schema cho mỗi bounded context (voting_service → voting, ranking; campaign_service → campaign; credit_service → credit; identity_service → identity; payment_service → payment). Volume tạo trước khi có identity/payment cần make infra-reset (xoá dữ liệu local) hoặc tạo tay hai role/schema đó; migration V2 của credit-service từ chối chạy khi các bảng credit còn dòng cũ. Spring service tự chạy Flyway cho schema của mình khi khởi động.

2. Các process​

Toàn bộ trong container (cô lập)​

make stack-up # build image, chạy service, worker và gateway; service Votee chỉ mở :8000 (và 127.0.0.1:8001 cho docs) ra ngoài; hạ tầng giữ các cổng ở trên
make smoke # luồng đầy đủ chạy từ bên trong network của stack
make stack-down

Swagger UI của từng service (chỉ dev): http://voting.localhost:8001/swagger-ui/index.html, cũng có campaign, credit, identity, payment. Các port :8080 đến :8084 không được publish nên gọi từ host sẽ bị từ chối; lời gọi /internal/** làm từ bên trong network, ví dụ docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.stack.yml exec campaign-service curl -H "X-Votee-Internal-Token: dev-internal-token-change-me" .... Xem API gateway.

Từng process trên host (gỡ lỗi)​

Ở chế độ này mỗi service mở port riêng trên máy; đây không phải chế độ cô lập. Gateway (localhost:8000) vẫn chạy và dùng được.

Mỗi lệnh một terminal (thứ tự khuyến nghị; process nào cũng chịu được dependency khởi động muộn):

make run-identity # identity-service :8083 (tạo admin dev admin@votee.local)
make run-campaign # campaign-service :8081
make run-payment # payment-service :8084 (provider giả)
make run-credit # credit-service :8082
make run-api # voting-api :8080, internal 127.0.0.1:8090
make run-relay # outbox → Kafka
make run-projector # live ranking
make run-sync # campaign snapshot → Voting Core
make run-finalizer # final tally
make run-sweeper # abandon paid attempt bị kẹt
JDK 25

Spring service target Java 25. Nếu java mặc định là bản khác, đặt JAVA_HOME tới JDK 25 trước khi chạy make run-identity, make run-campaign, make run-payment, make run-credit, make java-test.

3. Chạy thử một luồng​

# Mọi lời gọi công khai đi qua gateway :8000. Đăng nhập admin (tạo bởi make run-identity) và một người mua
ADMIN=$(curl -s localhost:8000/v1/auth/login -H 'Content-Type: application/json' \
-d '{"email":"admin@votee.local","password":"admin-password-change-me"}' | jq -r .access_token)
curl -s localhost:8000/v1/accounts -H 'Content-Type: application/json' -d '{"email":"fan@example.com","password":"a long password"}'
# Mã xác minh 6 số nằm trong thư ở Mailpit (http://localhost:18025); chưa xác minh thì login trả 403 EMAIL_NOT_VERIFIED
CODE=$(curl -s 'localhost:18025/api/v1/search?query=to:fan@example.com' | jq -r '.messages[0].Subject' | awk '{print $NF}')
curl -s localhost:8000/v1/accounts/email-verification -H 'Content-Type: application/json' \
-d "{\"email\":\"fan@example.com\",\"code\":\"$CODE\"}"
FAN=$(curl -s localhost:8000/v1/auth/login -H 'Content-Type: application/json' \
-d '{"email":"fan@example.com","password":"a long password"}' | jq -r .access_token)

# Tạo campaign (DRAFT) với 2 candidate, mở ngay bây giờ, đóng sau 1 giờ
curl -s localhost:8000/v1/campaigns -H 'Content-Type: application/json' -H "Authorization: Bearer $ADMIN" -d '{
"name": "Demo", "timezone": "Asia/Ho_Chi_Minh",
"starts_at": "2026-11-20T00:00:00Z", "ends_at": "2026-11-21T00:00:00Z",
"rules": {"free_votes_per_period": 10, "quota_period": "DAY", "max_votes_per_request": 5,
"free_vote_points": 1, "paid_vote_price": 10, "paid_vote_points": 10},
"candidates": ["Alice", "Bob"]}'

curl -s -X POST localhost:8000/v1/campaigns/<campaign_id>/schedule -H "Authorization: Bearer $ADMIN"

# Vote (cùng Idempotency-Key gửi lại → replay, header Idempotent-Replayed: true)
curl -si localhost:8000/v1/campaigns/<campaign_id>/votes \
-H "Authorization: Bearer $FAN" -H 'Idempotency-Key: k-1' -H 'Content-Type: application/json' \
-d '{"candidate_id": "<candidate_id>", "vote_count": 2}'

curl -s 'localhost:8000/v1/campaigns/<campaign_id>/ranking?kind=live'
curl -s localhost:8000/v1/campaigns/<campaign_id>/me/quota -H "Authorization: Bearer $FAN" # còn bao nhiêu vote
curl -s 'localhost:8000/v1/public/campaigns?status=ACTIVE' # danh sách công khai, không cần token

# Mua credit: admin bán gói, người mua tạo order, provider giả xác nhận, credit-service nhận PaymentConfirmed
curl -s localhost:8000/v1/campaigns/<campaign_id>/packages -H 'Content-Type: application/json' -H "Authorization: Bearer $ADMIN" \
-d '{"name":"100 credits","price":10000,"currency":"VND","credits":100}'
curl -s localhost:8000/v1/orders -H 'Content-Type: application/json' -H "Authorization: Bearer $FAN" \
-d '{"campaign_id":"<campaign_id>","package_id":"<package_id>"}'
# /internal/** không đi qua gateway: gọi thẳng service và mang token nội bộ (make run-* đặt sẵn giá trị dev)
curl -s -X POST localhost:8084/internal/v1/fake-provider/orders/<order_id>/succeed -H 'X-Votee-Internal-Token: dev-internal-token-change-me'
curl -s localhost:8000/v1/accounts/<account_id>/campaigns/<campaign_id> # <account_id> = sub của token

Điều chỉnh starts_at/ends_at theo thời điểm chạy. Danh sách endpoint đầy đủ ở HTTP APIs (EN).

4. Test​

make test # Go: unit + integration trên PostgreSQL/Kafka/Redis thật (-race)
make java-test # Spring: unit + integration
make smoke # full flow trong stack: gọi qua gateway, /internal/** từ bên trong network
cd voting-service && SMOKE_GATEWAY_URL=http://localhost:8000 go test -tags smoke -count=1 ./internal/smoke # cùng luồng, services chạy trên host

Chi tiết từng suite: Testing strategy.

5. Trang docs này​

make docs-install # npm ci trong website/
make docs-dev # http://localhost:3000, hot reload
make docs-build # build tĩnh, fail nếu có broken link