|
| 1 | +/** |
| 2 | + * External API — API Key authenticated endpoints for AI tools / automation. |
| 3 | + * |
| 4 | + * Auth: Header `Authorization: Bearer <EXTERNAL_API_KEY>` |
| 5 | + * |
| 6 | + * Endpoints: |
| 7 | + * GET /api/external/status — server status (TPS, memory, CPU, players, mspt) |
| 8 | + * POST /api/external/command — execute MC command { "command": "say hello" } |
| 9 | + * GET /api/external/players — online player list |
| 10 | + */ |
| 11 | + |
| 12 | +import { Router, Request, Response, NextFunction } from 'express'; |
| 13 | +import { config } from '../config.js'; |
| 14 | +import { bridgeService } from '../services/bridge.js'; |
| 15 | +import { getCachedStatus } from '../services/status.js'; |
| 16 | + |
| 17 | +const router = Router(); |
| 18 | + |
| 19 | +// API Key auth middleware |
| 20 | +function apiKeyAuth(req: Request, res: Response, next: NextFunction): void { |
| 21 | + if (!config.externalApiKey) { |
| 22 | + res.status(503).json({ error: 'External API is not configured. Set EXTERNAL_API_KEY env var.' }); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const auth = req.headers.authorization; |
| 27 | + if (!auth || !auth.startsWith('Bearer ')) { |
| 28 | + res.status(401).json({ error: 'Missing Authorization header. Use: Bearer <API_KEY>' }); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const key = auth.slice(7); |
| 33 | + if (key !== config.externalApiKey) { |
| 34 | + res.status(403).json({ error: 'Invalid API key' }); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + next(); |
| 39 | +} |
| 40 | + |
| 41 | +router.use(apiKeyAuth); |
| 42 | + |
| 43 | +// GET /api/external/status |
| 44 | +router.get('/status', (_req: Request, res: Response) => { |
| 45 | + const status = getCachedStatus(); |
| 46 | + res.json({ |
| 47 | + bridge: bridgeService.getStatus(), |
| 48 | + tps: status.tps ?? null, |
| 49 | + memory: status.memory ?? null, |
| 50 | + cpu: status.cpu ?? null, |
| 51 | + players: status.players ?? null, |
| 52 | + mspt: status.mspt ?? null, |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +// POST /api/external/command |
| 57 | +router.post('/command', async (req: Request, res: Response) => { |
| 58 | + const { command } = req.body as { command?: string }; |
| 59 | + |
| 60 | + if (!command || typeof command !== 'string') { |
| 61 | + res.status(400).json({ error: 'Missing "command" field' }); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (bridgeService.getStatus() !== 'connected') { |
| 66 | + res.status(503).json({ error: 'Bridge not connected to MC server' }); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + const result = await bridgeService.sendCommand(command); |
| 72 | + res.json({ success: true, result: result.result }); |
| 73 | + } catch { |
| 74 | + res.status(500).json({ error: 'Command execution failed' }); |
| 75 | + } |
| 76 | +}); |
| 77 | + |
| 78 | +// GET /api/external/players |
| 79 | +router.get('/players', (_req: Request, res: Response) => { |
| 80 | + const status = getCachedStatus(); |
| 81 | + if (!status.players) { |
| 82 | + res.status(503).json({ error: 'Player data not available' }); |
| 83 | + return; |
| 84 | + } |
| 85 | + res.json({ |
| 86 | + online: status.players.online, |
| 87 | + max: status.players.max, |
| 88 | + list: status.players.list, |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +export default router; |
0 commit comments