-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
90 lines (76 loc) · 2.38 KB
/
main.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
import express from 'express';
import router from './routes/gogo.js';
import rateLimit from './utils/ratelimit.js';
import dotenv from 'dotenv';
import cors from 'cors';
import pino from 'pino';
import pinoHttp from 'pino-http';
import swaggerJsdoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
const host = process.env.HOST || 'localhost';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const allowedOrigins = [
'http://localhost:3000',
'http://localhost:4000',
'https://nekonode-site.sziwyz.easypanel.host',
`https://api.nekonode.net`,
`https://nekonode.net`
];
const corsOptions = {
origin: function (origin, callback) {
// Check if the origin is in the allowedOrigins array
if (allowedOrigins.includes(origin) || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], // Allow specific methods
allowedHeaders: ['Content-Type', 'Authorization'] // Allow specific headers
};
// Enable CORS with the defined options
app.use(cors(corsOptions));
const logger = pino({
level: 'info',
base: { pid: process.pid },
timestamp: pino.stdTimeFunctions.epochTime
});
// Rate limit requests
app.use(rateLimit);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(pinoHttp({ logger }));
// Define a route to render the index page
app.get('/', (req, res) => {
res.json({ message: 'Welcome to NekoNode API', docs: 'https://api.nekonode.net/docs' });
});
app.use('/api', router);
// Swagger setup
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'NekoNode API',
version: '1.0.0',
description: 'API for searching and retrieving anime details',
},
servers: [
{
url: `https://api.nekonode.net`,
},
],
},
apis: [path.join(__dirname, 'routes', '*.js')],
};
const specs = swaggerJsdoc(options);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(specs));
app.listen(port, () => {
console.log(`Server running on ${host}:${port}`);
});