Chatroom DM (Multi-tenant)

Dịch vụ DM đa-tenant dùng MySQL + Redis Stream + Express. Cung cấp API để:


Tính năng chính


Kiến trúc thành phần


Yêu cầu hệ thống


Cài đặt

# 1) Cài dependencies
npm install

# 2) Tạo file .env và cấu hình (xem mục Cấu hình)
# Windows tạo nhanh file rỗng:
copy NUL .env

# 3) Tạo bảng/migrate
npm run migrate

Cấu hình (.env)

Tham số và giá trị mặc định (tham chiếu src/config.js):

NODE_ENV=development
PORT=3000

DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASS=
DB_NAME=chatroom

REDIS_URL=redis://127.0.0.1:6379
STREAM_KEY=msg_stream
STREAM_GROUP=backup_group
BACKUP_INTERVAL_MS=3000
CACHE_ROOM_SIZE=200

Lưu ý:


Chạy dịch vụ

# Khởi động Redis (tùy môi trường)
redis-server

# API server
npm run dev

# Worker backup (nên chạy song song với server)
npm run worker

Migrations (sql/)

Chạy tất cả migrations:

npm run migrate

API nhanh (xem chi tiết trong openapi.yaml)

Health

curl -s http://localhost:3000/api/v1/health

Tạo/Lấy phòng DM (quyền admin)

curl -X POST "http://localhost:3000/api/v1/tenants/1/rooms" \
  -H "x-tenant-id: 1" -H "x-private-key: <admin-key>" \
  -H "Content-Type: application/json" \
  -d '{"user_a_id":101,"user_b_id":202}'

Liệt kê phòng của 1 user

curl "http://localhost:3000/api/v1/tenants/1/rooms?user_id=101&limit=20"

Gửi tin nhắn vào phòng

curl -X POST "http://localhost:3000/api/v1/tenants/1/rooms/10/messages" \
  -H "x-tenant-id: 1" -H "x-public-key: <public-key>" \
  -H "x-room-user-token: <token-cua-user>" \
  -H "Content-Type: application/json" \
  -d '{"sender_id":101,"content_type":"text","content_text":"hello"}'

Đọc tin nhắn (prefer=db|cache)

curl "http://localhost:3000/api/v1/tenants/1/rooms/10/messages?limit=20&prefer=db" \
  -H "x-tenant-id: 1" -H "x-public-key: <public-key>" \
  -H "x-room-user-token: <token>"

Upsert read state

curl -X PUT "http://localhost:3000/api/v1/tenants/1/rooms/10/read-state" \
  -H "x-tenant-id: 1" -H "x-public-key: <public-key>" \
  -H "x-room-user-token: <token>" \
  -H "Content-Type: application/json" \
  -d '{"user_id":101,"last_read_message_id":12345}'

Đếm chưa đọc

curl "http://localhost:3000/api/v1/tenants/1/rooms/10/unread-count?user_id=101" \
  -H "x-tenant-id: 1" -H "x-public-key: <public-key>" \
  -H "x-room-user-token: <token>"

Quản trị status:


Phân trang & bộ nhớ đệm


Mô hình dữ liệu chính


Cấu trúc thư mục

src/
  server.js           # khởi động HTTP server, mount routes, middlewares
  config.js           # đọc biến môi trường
  db.js               # MySQL pool
  redis.js            # Redis client + group init
  routes/             # định tuyến REST
  services/           # nghiệp vụ (room, message, readstate)
  middlewares/        # error handler, auth (nếu cần mở rộng)
  utils/              # pagination, validate
  worker/backupWorker.js  # backup tin nhắn từ Redis stream sang MySQL
sql/                  # migrations
scripts/migrate.js    # chạy migrations theo thứ tự
openapi.yaml          # mô tả OpenAPI 3.1

Ghi chú bảo mật


Giấy phép

MIT (tuỳ chỉnh theo nhu cầu của bạn).