-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
265 lines (241 loc) · 7.48 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Modules and dependencies
const express = require("express");
const bodyParser = require("body-parser");
const expressLayouts = require("express-ejs-layouts");
const session = require("express-session");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const mongoose = require("./config/db");
const User = require("./models/User");
const { isLoggedIn, isAdmin } = require("./middleware/middlewares");
const sgMail = require("@sendgrid/mail");
require("dotenv").config();
const Discussion = require("./models/Discussion");
const methodOverride = require("method-override");
const winston = require("winston");
const multer = require("multer");
const fs = require("fs");
const path = require("path");
const Document = require("./models/Document");
// Configure SendGrid
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
// Routers
const mainRoutes = require("./routers/main");
const briefingsRouter = require("./routers/briefingsRouter");
const questionRouter = require("./routers/questionsRouter");
const quizRouter = require("./routers/quizRouter");
const categoryRouter = require("./routers/categoryRouter");
const notesRouter = require("./routers/notesRouter");
const registerRouters = require("./routers/registerRouters");
const authRoutes = require("./routers/auth");
const adminRoutes = require("./routers/admin");
const logoutRoutes = require("./routers/logout");
const flowsRoutes = require("./routers/flows");
const fuelRoutes = require("./routers/fuelRouter");
const questionRatingRouter = require("./routers/questionRatingRouter");
const passwordResetRoutes = require("./routers/passwordReset");
const weatherRouter = require("./routers/weatherRouter");
const rvrRouter = require("./routers/rvrRouter");
const discussionRouter = require("./routers/discussionRouter");
// Express app setup
const app = express();
const port = process.env.PORT || 3000;
const logger = winston.createLogger({
level: "info",
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: "error.log", level: "error" }),
new winston.transports.File({ filename: "combined.log" }),
],
});
if (process.env.NODE_ENV !== "production") {
logger.add(
new winston.transports.Console({
format: winston.format.simple(),
})
);
}
// Middleware
app.use((req, res, next) => {
if (
req.header("x-forwarded-proto") !== "https" &&
process.env.NODE_ENV === "production"
) {
res.redirect(`https://${req.header("host")}${req.url}`);
} else {
next();
}
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
app.set("trust proxy", 1);
app.use(
session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false,
rolling: true,
cookie: {
secure: process.env.NODE_ENV === "production",
httpOnly: true,
maxAge: 43200000,
},
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(methodOverride("_method"));
const upload = multer({
dest: "public/docs",
limits: { fileSize: 5000000 }, // size limit 5MB
fileFilter: (req, file, cb) => {
const allowedMimes = [
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/pdf",
"image/jpeg",
"image/pjpeg",
"image/png",
"image/gif",
];
if (
allowedMimes.includes(file.mimetype) ||
file.mimetype.startsWith("audio/")
) {
cb(null, true);
} else {
cb(
new Error(
"Invalid file type. Only .doc, .docx, .pdf, image, and audio files are allowed."
)
);
}
},
});
// Passport configuration
passport.use(new LocalStrategy(User.authenticate()));
// passport.serializeUser(User.serializeUser());
// passport.deserializeUser(User.deserializeUser());
passport.serializeUser((user, done) => {
// console.log("Serializing user:", user);
User.serializeUser()(user, done);
});
passport.deserializeUser((id, done) => {
// console.log("Deserializing user ID:", id);
User.deserializeUser()(id, done);
});
// Make user accessible in views
app.use((req, res, next) => {
res.locals.currentUser = req.user;
next();
});
// View engine
app.set("view engine", "ejs");
app.set("views", __dirname + "/views");
app.use(expressLayouts);
app.get("/documents", isLoggedIn, async (req, res) => {
try {
const documents = await Document.find({});
res.render("documents", {
documents,
title: "Document Repository",
customCSS: "documents.css",
});
} catch (err) {
console.log(err);
res.status(500).send("An error occurred while fetching the documents.");
}
});
app.get("/docs/:filename", isLoggedIn, function (req, res) {
const filePath = path.join(__dirname, "public/docs", req.params.filename);
res.set("Content-Disposition", "inline");
res.sendFile(filePath);
});
app.post("/upload", upload.single("document"), async (req, res) => {
const documentName = req.body.name;
// req.file is the `document` file
const { originalname, mimetype, filename, path } = req.file;
// Create a new Document and save it to the database
const newDocument = new Document({
name: documentName,
filename: filename,
mimetype: mimetype,
path: path,
});
try {
await newDocument.save();
res.redirect("/documents");
} catch (err) {
console.log(err);
res.status(500).send("An error occurred while uploading the file.");
}
});
app.post("/delete-document/:id", isAdmin, async (req, res) => {
try {
const document = await Document.findById(req.params.id);
fs.unlinkSync(path.join(__dirname, "public/docs", document.filename));
await Document.findByIdAndDelete(req.params.id);
res.redirect("/documents");
} catch (err) {
console.log(err);
res.status(500).send("An error occurred while deleting the document.");
}
});
app.post("/admin/add-category", (req, res) => {
const categoriesFilePath = path.join(__dirname, "public/data/categories.js");
delete require.cache[require.resolve(categoriesFilePath)];
let categories = require(categoriesFilePath);
// Add the new category
const newCategory = {
id: categories.length + 1, // This will be renumbered later
name: req.body.categoryName,
value: req.body.categoryValue,
};
categories.push(newCategory);
// Sort and renumber the categories
categories.sort((a, b) => a.name.localeCompare(b.name));
categories.forEach((category, index) => {
category.id = index + 1;
});
// Overwrite the original file
const newFileContent = `const categories = ${JSON.stringify(
categories,
null,
2
)};\n\nmodule.exports = categories;`;
fs.writeFileSync(categoriesFilePath, newFileContent, "utf8");
// Redirect back to the admin page, or handle the result of the operation in another way
res.redirect("/admin/dashboard");
});
// Routers
app.use(mainRoutes);
app.use(adminRoutes);
app.use(briefingsRouter);
app.use(discussionRouter);
app.use(questionRouter);
app.use(quizRouter);
app.use(categoryRouter);
app.use(notesRouter);
app.use(registerRouters);
app.use(authRoutes);
app.use(logoutRoutes);
app.use(flowsRoutes);
app.use(questionRatingRouter);
app.use(fuelRoutes);
app.use(passwordResetRoutes);
app.use(rvrRouter);
app.use(weatherRouter);
app.use((err, req, res, next) => {
logger.error(err.stack);
res.status(500);
res.render("error", {
title: "Error",
customCSS: "error.css",
error: err,
});
});
// Start server
app.listen(port, () => {
console.log(`Server up on localhost:${port}`);
});