-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
123 lines (112 loc) · 3.57 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
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const compression = require("compression");
const helmet = require("helmet");
const cors = require("cors");
const Sentry = require("@sentry/node");
const kue = require("kue");
const {
NODE_ENV,
PORT,
SENTRY_DSN,
ALLOWED_ORIGINS
} = require("./config/index");
const { notFound, sendErrors } = require("./config/errorHandler");
const { logRequestMiddleware } = require("./middlewares/log");
const { globalRateLimiter } = require("./config/rateLimit");
const app = express();
require("dotenv").config();
require("./config/dbconnection");
module.exports = () => {
Sentry.init({
dsn: SENTRY_DSN,
attachStacktrace: true,
debug: true,
environment: NODE_ENV
});
if (NODE_ENV !== "development") app.use(Sentry.Handlers.requestHandler());
app.use(compression());
app.use(helmet());
app.set("trust proxy", true);
app.use(cors({ exposedHeaders: "x-auth-token", origin: ALLOWED_ORIGINS }));
app.use(globalRateLimiter);
app.use(express.static(path.join(__dirname, "public")));
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 1000000
})
);
app.use(
bodyParser.json({
limit: "50mb",
extended: true,
parameterLimit: 1000000
})
);
app.use("/kue-cli", kue.app);
app.use(logRequestMiddleware);
//load Schemas
const Log = require("./models/Log");
const User = require("./models/User");
const Participant = require("./models/Participant");
const Event = require("./models/Event");
const Attendance = require("./models/Attendance");
const Feedback = require("./models/Feedback");
const ResetToken = require("./models/ResetToken");
const Subscriber = require("./models/Subscriber");
const Subscription = require("./models/Subscription");
const Todo = require("./models/Todo");
const Group = require("./models/Group");
const Task = require("./models/Task");
const TaskAssignee = require("./models/TaskAssignee");
const Comment = require("./models/Comment");
const Reply = require("./models/Reply");
//Routes
app.use("/api/v1", require("./routes/api/v1/index"));
app.use("/api/v1/users", require("./routes/api/v1/users"));
app.use("/api/v1/events", require("./routes/api/v1/events"));
app.use("/api/v1/groups", require("./routes/api/v1/groups"));
app.use("/api/v1/tasks", require("./routes/api/v1/tasks"));
app.use("/api/v1/subscriptions", require("./routes/api/v1/subscriptions"));
app.use("/api/v1/todos", require("./routes/api/v1/todos"));
app.use("/api/v1/comments", require("./routes/api/v1/comments"));
app.use("/api/v1/replies", require("./routes/api/v1/replies"));
app.use("*", notFound);
//Error Handlers
app.use(sendErrors);
// Allowing headers
app.use((req, res, next) => {
let origin = req.headers.origin;
if (
ALLOWED_ORIGINS.includes(origin) ||
(ALLOWED_ORIGINS[2] && ALLOWED_ORIGINS[2].test(origin)) ||
(ALLOWED_ORIGINS[3] && ALLOWED_ORIGINS[3].test(origin)) ||
(ALLOWED_ORIGINS[4] && ALLOWED_ORIGINS[4].test(origin))
)
res.header("Access-Control-Allow-Origin", origin);
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, x-auth-token"
);
res.header("Access-Control-Allow-Credentials", true);
res.header(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
);
next();
});
//Setting up server
(async () => {
try {
await app.listen(PORT);
console.info(
`NODE_ENV: ${NODE_ENV}\nServer is up and running on Port ${PORT}`
);
} catch (err) {
console.info("Error in running server.", err);
}
})();
};