-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
153 lines (135 loc) · 4.71 KB
/
server.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const express = require("express");
const cors = require("cors");
const connectDB = require("./config/db");
const Sentry = require("@sentry/node");
const cookieParser = require("cookie-parser");
const { doubleCsrf } = require("csrf-csrf");
require("dotenv").config();
const isProduction = process.env.NODE_ENV === "production";
// Init app
const app = express();
/*
// init Sentry
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Sentry.Integrations.Express({ app }),
// Automatically instrument Node.js libraries and frameworks
...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
],
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
// RequestHandler creates a separate execution context, so that all
// transactions/spans/breadcrumbs are isolated across requests
app.use(Sentry.Handlers.requestHandler());
// TracingHandler creates a trace for every incoming request
app.use(Sentry.Handlers.tracingHandler());
*/
//Init Middleware
app.use(express.json({ extended: false }));
app.use(cookieParser());
app.use(cors());
// Connect Database
connectDB();
const cookiePrefix = isProduction ? "__Host-" : "";
const secureCookie = isProduction ? true : false;
const doubleCsrfUtilities = doubleCsrf({
getSecret: () => "Secret",
cookieName: `${cookiePrefix}psifi.x-csrf-token`,
cookieOptions: {
httpOnly: true,
sameSite: "lax",
path: "/",
secure: secureCookie,
},
size: 64,
ignoredMethods: ["GET", "HEAD", "OPTIONS"],
getTokenFromRequest: (req) => req.headers["x-csrf-token"],
});
const { doubleCsrfProtection, generateToken } = doubleCsrfUtilities;
// CSRF Token generation route
app.get("/api/csrf-token", (req, res) => {
const csrfToken = generateToken(res, req);
res.json({ csrfToken });
});
app.use(doubleCsrfProtection);
// Define routes
app.get("/", (req, res) => res.send("API Running"));
// User routes
app.use("/api/users", require("./routes/api/users"));
app.use("/api/auth", require("./routes/api/auth"));
app.use("/api/profile", require("./routes/api/profile"));
app.use("/api/stylesheet", require("./routes/api/stylesheet"));
// Comments/announcements/subscriptions placeholder
app.use("/api/announcements", require("./routes/api/announcements"));
app.use("/api/subscriptions", require("./routes/api/subscriptions"));
// Services routes
app.use("/api/profile/referral", require("./routes/api/profile/referralRoute"));
app.use("/api/profile/invite-tree", require("./routes/api/profile/inviteTree"));
// Communities routes
app.use(
"/api/communities",
require("./routes/api/sections/communities/communitiesRoute")
);
app.use(
"/api/contributions",
require("./routes/api/sections/communities/contributions")
);
app.use("/api/artist", require("./routes/api/sections/communities/artist"));
app.use("/api/comments", require("./routes/api/comments"));
// Forums routes
const forumsBasePath = "/api/forums";
const forumsSectionPath = "./routes/api/sections/forum";
app.use(
`${forumsBasePath}/categories`,
require(`${forumsSectionPath}/forumCategory`)
);
app.use(
`${forumsBasePath}/last-read-topic`,
require(`${forumsSectionPath}/forumLastReadTopic`)
);
app.use(`${forumsBasePath}/poll`, require(`${forumsSectionPath}/forumPoll`));
app.use(
`${forumsBasePath}/poll-vote`,
require(`${forumsSectionPath}/forumPollVote`)
);
app.use(`${forumsBasePath}/posts`, require(`${forumsSectionPath}/forumPost`));
app.use(
`${forumsBasePath}/:id/topics/notes`,
require(`${forumsSectionPath}/forumTopicNote`)
);
app.use(forumsBasePath, require(`${forumsSectionPath}/forumRoute`));
// Task Runner
app.use("/api/taskRunner", require("./routes/api/tasks/taskRunner"));
app.use(
"/api/taskRunner/fix-posts",
require("./routes/api/tasks/fixForumPosts")
);
// Mod tools
app.use("/api/tools", require("./routes/api/util/tools"));
app.use("/api/tools/permissions", require("./routes/api/util/permissions"));
app.use("/api/check-ip-ban", require("./routes/api/util/checkIpBan"));
/*
app.use(Sentry.Handlers.errorHandler());
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + "\n");
});
*/
// handle any downstream errors
app.use((err, req, res, next) => {
console.error(err.stack);
console.log("Error", err.message);
console.log(err, "117 server.js");
res.status(500).send("Server Error");
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on ${PORT}`));