import { Hono } from 'hono'; import * as goatService from '../services/goat'; const app = new Hono(); // Resolve identity/handle app.get('/resolve/:identifier', async (c) => { try { const identifier = c.req.param('identifier'); const result = await goatService.resolveDid(identifier); return c.json(result); } catch (error: any) { console.error('[Goat] Resolve error:', error.message); return c.json({ error: 'Failed to resolve identifier' }, 500); } }); // Get record by AT-URI app.get('/get', async (c) => { try { const uri = c.req.query('uri'); if (!uri) { return c.json({ error: 'uri query parameter required' }, 400); } const result = await goatService.getRecord(String(uri)); return c.json(result); } catch (error: any) { console.error('[Goat] Get error:', error.message); return c.json({ error: 'Failed to get record' }, 500); } }); // List collections for a DID app.get('/ls/:did', async (c) => { try { const did = c.req.param('did'); const collections = c.req.query('collections'); // Using service function which handles list logic const result = await goatService.listCollections(did, collections === 'true'); return c.json(result); } catch (error: any) { console.error('[Goat] List error:', error.message); return c.json({ error: 'Failed to list collections' }, 500); } }); // Generic goat command execution const ALLOWED_GOAT_SUBCOMMANDS = ['resolve', 'get', 'ls', 'describe', 'version', 'help']; app.post('/exec', async (c) => { const { args = [] } = await c.req.json(); if (!Array.isArray(args) || !args.every((a: unknown) => typeof a === 'string')) { return c.json({ error: 'args must be an array of strings' }, 400); } if (args.length > 20) { return c.json({ error: 'Too many arguments (max 20)' }, 400); } if (args.some((a: string) => a.length > 1024)) { return c.json({ error: 'Argument too long (max 1024 chars)' }, 400); } // Allow empty args (shows goat help) or help/version flags if (args.length > 0) { const subcommand = args[0]; const isFlag = subcommand.startsWith('--'); if (!isFlag && !ALLOWED_GOAT_SUBCOMMANDS.includes(subcommand)) { return c.json({ error: `Subcommand '${subcommand}' not allowed. Allowed: ${ALLOWED_GOAT_SUBCOMMANDS.join(', ')}` }, 403); } } try { const result = await goatService.executeGoatCommand(args, 60000); return c.json(result); } catch (error: any) { return c.json({ error: 'Goat command failed' }, 500); } }); export default app;