Skip to content

refactor: restructure codebase and add REST API layer#3

Open
kennhung wants to merge 1 commit into
mainfrom
refactor-structure
Open

refactor: restructure codebase and add REST API layer#3
kennhung wants to merge 1 commit into
mainfrom
refactor-structure

Conversation

@kennhung

Copy link
Copy Markdown
Member

Summary

  • 結構重構:將所有模組搬到 src/modules/,解決 google-drive-check vs google-drive-checker 命名混淆問題,合併為 modules/google-drive/handler.ts(Slack 整合)和 checker.ts(純 Drive 邏輯)
  • 基礎層:新增 config.ts(集中所有 env var 解析)和 db.ts(MongoDB 連線工廠),app.ts 從 120 行縮到 90 行
  • REST API:新增 /api/* 端點,Bearer token 驗證,支援查看 Slack 訊息、Drive 合規記錄,以及動態調整 Bot 設定

New API Endpoints

All endpoints require Authorization: Bearer <API_KEY> header.

Method Path Description
GET /api/messages List messages (?collection=messages|changedMessages|deletedMessages&limit=50&before=<cursor>)
GET /api/messages/:id Get single message
GET /api/drive/jobs List compliance jobs (?status=pending|completed|error)
GET /api/drive/jobs/:id Get single job
GET /api/settings Get current bot settings
PATCH /api/settings Update runtime settings (feature flags, ignore lists, etc.)

New env vars required

  • API_KEY — Bearer token for API authentication
  • API_PORT — HTTP port (default: 3000)

Test plan

  • npm run build passes with no TypeScript errors
  • Bot starts and existing Slack functionality works (message storage, Drive check, reaction check)
  • curl -H "Authorization: Bearer <key>" http://localhost:3000/api/messages returns message list
  • curl -H "Authorization: Bearer <key>" http://localhost:3000/api/drive/jobs returns job list
  • curl -X PATCH -H "Authorization: Bearer <key>" -H "Content-Type: application/json" -d '{"useReactionCheck":false}' http://localhost:3000/api/settings updates settings
  • Request without token returns 401

🤖 Generated with Claude Code

- Introduce config.ts to centralize all env var parsing/validation
- Introduce db.ts as a MongoDB connection factory with typed DbCollections
- Move all modules under src/modules/, merging confusingly-named
  google-drive-check + google-drive-checker into modules/google-drive/
  (handler.ts for Slack integration, checker.ts for pure Drive logic)
- Add SettingsStore (modules/settings/store.ts) for MongoDB-backed
  runtime settings with in-memory cache
- Add REST API under /api/* (Express, Bearer token auth):
    GET/  /api/messages  (collection, limit, cursor pagination)
    GET   /api/messages/:id
    GET   /api/drive/jobs  (status filter, cursor pagination)
    GET   /api/drive/jobs/:id
    GET   /api/settings
    PATCH /api/settings  (runtime feature flag control)
- Remove unused deps: args, async, nanoid; add express + @types/express
- Add clean script; build now removes dist/ before compiling

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant