1. Pattern Detection Utility (src/utils/patterns.ts) - Centralize tool category patterns (domain, deposit, server, etc.) - Add memory category patterns (company, tech, role) - Add region detection (korea, japan, singapore, us) - Add tech stack detection (postgres, redis, nodejs, etc.) - Export detectToolCategories(), detectRegion(), detectTechStack() 2. API Route Modularization (src/routes/api/) - deposit.ts: /balance, /deduct with apiKeyAuth middleware - chat.ts: /test, /chat with session handling - contact.ts: Contact form with CORS middleware - metrics.ts: Circuit Breaker status endpoint 3. Updates - tools/index.ts: Use detectToolCategories from patterns.ts - api.ts: Compose sub-routers (899 → 53 lines, 94% reduction) Benefits: - Single source of truth for patterns - Better code organization - Easier maintenance and testing Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import { Hono } from 'hono';
|
|
import { Env } from '../types';
|
|
import { depositRouter } from './api/deposit';
|
|
import { chatRouter } from './api/chat';
|
|
import { contactRouter } from './api/contact';
|
|
import { metricsRouter } from './api/metrics';
|
|
|
|
/**
|
|
* API Router (Hono)
|
|
*
|
|
* Organized into sub-modules for maintainability:
|
|
* - /deposit/* - Deposit balance & deduction (deposit.ts)
|
|
* - /test, /chat - Test & Chat APIs (chat.ts)
|
|
* - /contact - Contact form (contact.ts)
|
|
* - /metrics - Circuit Breaker metrics (metrics.ts)
|
|
*
|
|
* Manual Test:
|
|
* 1. wrangler dev
|
|
* 2. Test deposit balance:
|
|
* curl http://localhost:8787/api/deposit/balance?telegram_id=123 \
|
|
* -H "X-API-Key: your-secret"
|
|
* 3. Test deposit deduct:
|
|
* curl -X POST http://localhost:8787/api/deposit/deduct \
|
|
* -H "X-API-Key: your-secret" \
|
|
* -H "Content-Type: application/json" \
|
|
* -d '{"telegram_id":"123","amount":1000,"reason":"test"}'
|
|
* 4. Test API (dev only):
|
|
* curl -X POST http://localhost:8787/api/test \
|
|
* -H "Content-Type: application/json" \
|
|
* -d '{"text":"hello","secret":"your-secret"}'
|
|
* 5. Chat API (production-ready):
|
|
* curl -X POST http://localhost:8787/api/chat \
|
|
* -H "Authorization: Bearer your-webhook-secret" \
|
|
* -H "Content-Type: application/json" \
|
|
* -d '{"message":"서버 추천해줘","chat_id":821596605,"user_id":821596605,"username":"web-tester"}'
|
|
* 6. Test contact (from allowed origin):
|
|
* curl -X POST http://localhost:8787/api/contact \
|
|
* -H "Origin: https://hosting.anvil.it.com" \
|
|
* -H "Content-Type: application/json" \
|
|
* -d '{"email":"test@example.com","message":"test message"}'
|
|
* 7. Test metrics (Circuit Breaker status):
|
|
* curl http://localhost:8787/api/metrics \
|
|
* -H "Authorization: Bearer your-webhook-secret"
|
|
*/
|
|
const api = new Hono<{ Bindings: Env }>();
|
|
|
|
// Mount sub-routers
|
|
api.route('/deposit', depositRouter);
|
|
api.route('/', chatRouter); // /test, /chat
|
|
api.route('/contact', contactRouter);
|
|
api.route('/metrics', metricsRouter);
|
|
|
|
export { api as apiRouter };
|