Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
Publish MCP to npm / Build, test, and publish MCP (push) Waiting to run
Publish SDK to npm / Build, test, and publish SDK (push) Waiting to run
Sync action catalog to docs / sync (push) Waiting to run
Publish UI to npm / Build and publish UI (push) Waiting to run
Build and Release / preflight (push) Waiting to run
81 lines
2.7 KiB
TypeScript
81 lines
2.7 KiB
TypeScript
import { Hono } from 'hono';
|
|
import type { HttpBindings } from '@hono/node-server';
|
|
import type { UpdateService } from '../update/service';
|
|
import { readUpdateSource, saveUpdateSource, testUpdateSources } from '../update/sources';
|
|
import {
|
|
normalizeUpdateSourceConfig,
|
|
type UpdateSourceConfig,
|
|
} from '@snowluma/common/update-source';
|
|
|
|
export function updateRoutes(service: UpdateService, restart: () => void) {
|
|
const app = new Hono<{ Bindings: HttpBindings }>();
|
|
app.get('/status', (c) => c.json(service.status()));
|
|
app.get('/sources', (c) => {
|
|
try {
|
|
return c.json({ config: readUpdateSource() });
|
|
} catch (error) {
|
|
return c.json({ message: error instanceof Error ? error.message : String(error) }, 500);
|
|
}
|
|
});
|
|
app.post('/sources', async (c) => {
|
|
let config: UpdateSourceConfig;
|
|
|
|
try {
|
|
config = normalizeUpdateSourceConfig(await c.req.json());
|
|
} catch (error) {
|
|
return c.json({ message: error instanceof Error ? error.message : String(error) }, 400);
|
|
}
|
|
|
|
try {
|
|
saveUpdateSource(config);
|
|
return c.json({
|
|
config,
|
|
saved: true,
|
|
applied: true,
|
|
online: true,
|
|
errors: [],
|
|
message: '更新来源已保存',
|
|
});
|
|
} catch (error) {
|
|
return c.json({ message: error instanceof Error ? error.message : String(error) }, 500);
|
|
}
|
|
});
|
|
app.post('/sources/test', async (c) => {
|
|
try {
|
|
return c.json({ results: await testUpdateSources(await c.req.json()) });
|
|
} catch (error) {
|
|
return c.json({ message: error instanceof Error ? error.message : String(error) }, 400);
|
|
}
|
|
});
|
|
app.post('/download', async (c) => {
|
|
let body: unknown;
|
|
|
|
try {
|
|
body = await c.req.json();
|
|
} catch {
|
|
return c.json({ message: '请求必须是 JSON 对象' }, 400);
|
|
}
|
|
|
|
if (!body || typeof body !== 'object' || !('version' in body)) {
|
|
return c.json({ message: '缺少更新版本' }, 400);
|
|
}
|
|
|
|
try {
|
|
service.start(body.version);
|
|
return c.json(service.status(), 202);
|
|
} catch (error) {
|
|
return c.json({ message: error instanceof Error ? error.message : String(error) }, 409);
|
|
}
|
|
});
|
|
app.post('/restart', (c) => {
|
|
try {
|
|
service.restart();
|
|
c.env.outgoing.once('finish', restart);
|
|
return c.json(service.status(), 202);
|
|
} catch (error) {
|
|
return c.json({ message: error instanceof Error ? error.message : String(error) }, 409);
|
|
}
|
|
});
|
|
return app;
|
|
}
|