feat(api): add entries endpoint
This commit is contained in:
parent
f52e4675e1
commit
1325cdfa78
@ -1,28 +1,23 @@
|
||||
import fastify from 'fastify';
|
||||
import cors from '@fastify/cors';
|
||||
import rateLimit from '@fastify/rate-limit';
|
||||
import v1EntriesRoutes from 'routes/v1/entries';
|
||||
import v1ReportRoutes from 'routes/v1/report';
|
||||
import environment from 'services/environment';
|
||||
|
||||
const server = fastify({ logger: true });
|
||||
|
||||
server.register(cors, {
|
||||
origin: (origin, callback) => {
|
||||
const chrome = /chrome-extension:\/\/[a-z]{32}/g;
|
||||
const moz =
|
||||
/moz-extension:\/\/[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/g;
|
||||
|
||||
if (chrome.test(origin) || moz.test(origin)) {
|
||||
callback(null, true);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(new Error('Not allowed'), false);
|
||||
},
|
||||
origin: [
|
||||
/chrome-extension:\/\/[a-z]{32}/g,
|
||||
/moz-extension:\/\/[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/g,
|
||||
'https://www.cookie-dialog-monster.com',
|
||||
],
|
||||
});
|
||||
|
||||
server.register(rateLimit, { max: 1, timeWindow: 30000 });
|
||||
|
||||
server.register(v1EntriesRoutes, { prefix: '/rest/v1' });
|
||||
server.register(v1ReportRoutes, { prefix: '/rest/v1' });
|
||||
|
||||
server.listen(environment.port, '0.0.0.0', (error, address) => {
|
||||
|
18
packages/api/src/routes/v1/entries.ts
Normal file
18
packages/api/src/routes/v1/entries.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
||||
import fetch from 'node-fetch';
|
||||
|
||||
export default (server: FastifyInstance, options: RouteShorthandOptions, done: () => void) => {
|
||||
server.get('/entries/', async (request, reply) => {
|
||||
try {
|
||||
const repositoryUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main';
|
||||
const dataUrl = `${repositoryUrl}/data/elements.txt`;
|
||||
const entriesLength = (await (await fetch(dataUrl)).text()).split('\n').length;
|
||||
|
||||
reply.send({ entries: entriesLength, success: true });
|
||||
} catch {
|
||||
reply.send({ success: false });
|
||||
}
|
||||
});
|
||||
|
||||
done();
|
||||
};
|
Loading…
Reference in New Issue
Block a user