forked from zabartcc/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (84 loc) · 3.02 KB
/
app.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
91
92
93
94
95
96
97
98
99
100
101
// Core imports
import express from 'express';
import cookie from 'cookie-parser';
import cors from 'cors';
import env from 'dotenv';
import mongoose from 'mongoose';
import Redis from 'ioredis';
import aws from 'aws-sdk';
// Route Controllers
import UserController from './controllers/UserController.js';
import ControllerController from './controllers/ControllerController.js';
import OnlineController from './controllers/OnlineController.js';
import NewsController from './controllers/NewsController.js';
import EventController from './controllers/EventController.js';
import FileController from './controllers/FileController.js';
import FeedbackController from './controllers/FeedbackController.js';
import IdsController from './controllers/IdsController.js';
import TrainingController from './controllers/TrainingController.js';
import DiscordController from './controllers/DiscordController.js';
import StatsController from './controllers/StatsController.js';
// Global Dossier Model
import Dossier from './models/Dossier.js';
env.config();
// Setup Express
const app = express();
app.use((req, res, next) => {
res.stdRes = {
ret_det: {
code: 200,
message: '',
},
data: {}
};
next();
});
app.use(cookie());
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({
limit: '50mb',
extended: true,
parameterLimit: 50000
}));
app.redis = new Redis(process.env.REDIS_URI);
app.redis.on('error', err => { throw new Error(`Failed to connect to Redis: ${err}`); });
app.redis.on('connect', () => console.log('Successfully connected to Redis'));
const origins = process.env.CORS_ORIGIN.split('|');
console.log(`Allowed origins: ${origins}`)
app.use(cors({
origin: origins,
credentials: true,
}));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', process.env.NODE_ENV === "production" ? "*" : "http://localhost:8080");
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.s3 = new aws.S3({
endpoint: new aws.Endpoint('nyc3.digitaloceanspaces.com'),
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
app.dossier = Dossier;
// Connect to MongoDB
mongoose.set('toJSON', {virtuals: true});
mongoose.set('toObject', {virtuals: true});
mongoose.connect(process.env.MONGO_URI);
const db = mongoose.connection;
db.once('open', () => console.log('Successfully connected to MongoDB'));
app.use('/online', OnlineController);
app.use('/user', UserController);
app.use('/controller', ControllerController);
app.use('/news', NewsController);
app.use('/event', EventController);
app.use('/file', FileController);
app.use('/feedback', FeedbackController);
app.use('/ids', IdsController);
app.use('/training', TrainingController);
app.use('/discord', DiscordController);
app.use('/stats', StatsController);
app.listen('3000', () =>{
console.log('Listening on port 3000');
});