-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
134 lines (113 loc) · 3.85 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Core imports
import express from 'express';
import * as Sentry from "@sentry/node";
import * as Tracing from '@sentry/tracing';
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';
import activityHelper from './helpers/controllerActivityHelper.js';
// 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();
if(process.env.NODE_ENV === 'production') {
Sentry.init({
dsn: "https://[email protected]/5837848",
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({
app,
}),
],
tracesSampleRate: 0.5,
});
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
} else {
app.Sentry = {
captureException(e) {
console.log(e);
},
captureMessage(m) {
console.log(m);
}
};
}
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('|');
app.use(cors({
origin: origins,
credentials: true,
}));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
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('sfo3.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);
// Uncomment to activate activity emails for controllers. Reset DB activity date fields before activating.
// Disabled per the ATM 9/19/24.
// activityHelper.registerControllerActivityChecking();
if(process.env.NODE_ENV === 'production') app.use(Sentry.Handlers.errorHandler());
app.listen('3000', () =>{
console.log('Listening on port 3000');
});