feat(api): add data route
This commit is contained in:
parent
c448588b56
commit
960a020cf9
@ -3,6 +3,7 @@ import cors from '@fastify/cors';
|
||||
import rateLimit from '@fastify/rate-limit';
|
||||
import v1EntriesRoutes from 'routes/v1/entries';
|
||||
import v1ReportRoutes from 'routes/v1/report';
|
||||
import v2DataRoutes from 'routes/v2/data';
|
||||
import environment from 'services/environment';
|
||||
|
||||
const server = fastify({ logger: true });
|
||||
@ -19,6 +20,7 @@ server.register(rateLimit, { max: 1, timeWindow: 30000 });
|
||||
|
||||
server.register(v1EntriesRoutes, { prefix: '/rest/v1' });
|
||||
server.register(v1ReportRoutes, { prefix: '/rest/v1' });
|
||||
server.register(v2DataRoutes, { prefix: '/rest/v2' });
|
||||
|
||||
server.listen({ host: '0.0.0.0', port: environment.port }, (error, address) => {
|
||||
if (error) {
|
||||
|
36
packages/api/src/routes/v2/data.ts
Normal file
36
packages/api/src/routes/v2/data.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
||||
import fetch from 'node-fetch';
|
||||
|
||||
export default (server: FastifyInstance, options: RouteShorthandOptions, done: () => void) => {
|
||||
server.get('/data/', async (request, reply) => {
|
||||
try {
|
||||
const dataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data';
|
||||
const classesUrl = `${dataUrl}/classes.txt`;
|
||||
const elementsUrl = `${dataUrl}/elements.txt`;
|
||||
const fixesUrl = `${dataUrl}/fixes.txt`;
|
||||
const skipsUrl = `${dataUrl}/skips.txt`;
|
||||
|
||||
const results = await Promise.all([
|
||||
fetch(classesUrl),
|
||||
fetch(elementsUrl),
|
||||
fetch(fixesUrl),
|
||||
fetch(skipsUrl),
|
||||
]);
|
||||
|
||||
reply.send({
|
||||
data: {
|
||||
classes: (await results[0].text()).split('\n'),
|
||||
fixes: (await results[1].text()).split('\n'),
|
||||
elements: (await results[2].text()).split('\n'),
|
||||
skips: (await results[3].text()).split('\n'),
|
||||
},
|
||||
success: true,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
reply.send({ success: false });
|
||||
}
|
||||
});
|
||||
|
||||
done();
|
||||
};
|
Loading…
Reference in New Issue
Block a user