-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.ts
56 lines (47 loc) · 1.34 KB
/
main.ts
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
import Fastify from 'fastify';
import FastifyCors from '@fastify/cors';
import Redis from 'ioredis';
import gogoanime from './routes';
import chalk from 'chalk';
const fastify = Fastify({
maxParamLength: 1000,
logger: true,
});
export const redis =
process.env.REDIS_HOST &&
new Redis({
host: process.env.REDIS_HOST,
port: Number(process.env.REDIS_PORT),
password: process.env.REDIS_PASSWORD,
});
(async () => {
const PORT = 3000;
await fastify.register(FastifyCors, {
origin: '*',
methods: 'GET',
});
console.log(chalk.green(`Starting server on port ${PORT}... 🚀`));
if (!process.env.REDIS_HOST)
console.warn(chalk.yellowBright('Redis not found. Cache disabled.'));
await fastify.register(gogoanime, { prefix: '/' });
try {
fastify.get('*', (request, reply) => {
reply.status(404).send({
message: '',
error: '404 Error: Page not found',
});
});
fastify.listen({ port: PORT, host: '0.0.0.0' }, (e, address) => {
if (e) throw e;
console.log(`Server listening on ${address}`);
});
} catch (err: any) {
fastify.log.error(err);
process.exit(1);
}
})();
// Export the handler function for use with serverless platforms
export default async function handler(req: any, res: any) {
await fastify.ready();
fastify.server.emit('request', req, res);
}