Spaces:
Running
Running
File size: 2,094 Bytes
1b44660 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import { zValidator } from '@hono/zod-validator';
import { $reports, desc } from '@meridian/database';
import { Hono } from 'hono';
import { z } from 'zod';
import type { HonoEnv } from '../app';
import { tryCatchAsync } from '../lib/tryCatchAsync';
import { getDb, hasValidAuthToken } from '../lib/utils';
const route = new Hono<HonoEnv>()
.get('/last-report', async c => {
// check auth token
const hasValidToken = hasValidAuthToken(c);
if (!hasValidToken) {
return c.json({ error: 'Unauthorized' }, 401);
}
const reportResult = await tryCatchAsync(
getDb(c.env.HYPERDRIVE).query.$reports.findFirst({
orderBy: desc($reports.createdAt),
})
);
if (reportResult.isErr()) {
return c.json({ error: 'Failed to fetch last report' }, 500);
}
const report = reportResult.value;
if (report === undefined) {
return c.json({ error: 'No report found' }, 404);
}
return c.json(report);
})
.post(
'/report',
zValidator(
'json',
z.object({
title: z.string(),
content: z.string(),
totalArticles: z.number(),
totalSources: z.number(),
usedArticles: z.number(),
usedSources: z.number(),
tldr: z.string(),
createdAt: z.coerce.date(),
model_author: z.string(),
clustering_params: z.object({
umap: z.object({
n_neighbors: z.number(),
}),
hdbscan: z.object({
min_cluster_size: z.number(),
min_samples: z.number(),
epsilon: z.number(),
}),
}),
})
),
async c => {
if (!hasValidAuthToken(c)) {
return c.json({ error: 'Unauthorized' }, 401);
}
const db = getDb(c.env.HYPERDRIVE);
const body = c.req.valid('json');
const reportResult = await tryCatchAsync(db.insert($reports).values(body));
if (reportResult.isErr()) {
return c.json({ error: 'Failed to insert report' }, 500);
}
return c.json({ success: true });
}
);
export default route;
|