feat(server): add optional fastify api
This commit is contained in:
parent
7c20dd91d9
commit
5adaaad216
5
src/cli/commands/server.ts
Normal file
5
src/cli/commands/server.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { startServer } from '../../server/index.js';
|
||||
|
||||
export async function serverStart(cwd: string, options: { port?: number } = {}): Promise<void> {
|
||||
await startServer(cwd, options.port ?? 3377);
|
||||
}
|
||||
15
src/server/index.ts
Normal file
15
src/server/index.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import Fastify from 'fastify';
|
||||
import { registerRoutes } from './routes.js';
|
||||
|
||||
export async function startServer(cwd: string, port = 3377): Promise<void> {
|
||||
const app = Fastify({ logger: false });
|
||||
await registerRoutes(app, cwd);
|
||||
|
||||
try {
|
||||
await app.listen({ port, host: '127.0.0.1' });
|
||||
console.log(`AgentHub server listening on http://127.0.0.1:${port}`);
|
||||
} catch (err) {
|
||||
app.log.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
27
src/server/routes.ts
Normal file
27
src/server/routes.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { Index } from '../core/index.js';
|
||||
|
||||
export async function registerRoutes(app: FastifyInstance, cwd: string): Promise<void> {
|
||||
app.get('/status', async () => {
|
||||
const index = new Index(cwd);
|
||||
const open = index.list('task', { status: 'open' });
|
||||
const inProgress = index.list('task', { status: 'in_progress' });
|
||||
index.close();
|
||||
return { open: open.length, inProgress: inProgress.length };
|
||||
});
|
||||
|
||||
app.get('/tasks', async () => {
|
||||
const index = new Index(cwd);
|
||||
const tasks = index.list('task');
|
||||
index.close();
|
||||
return tasks;
|
||||
});
|
||||
|
||||
app.get('/search', async (request) => {
|
||||
const { q } = request.query as { q: string };
|
||||
const index = new Index(cwd);
|
||||
const results = index.search(q);
|
||||
index.close();
|
||||
return results;
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user