-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.js
108 lines (95 loc) · 2.84 KB
/
main.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
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const express = require("express");
const rateLimit = require("express-rate-limit");
const unless = require("express-unless");
const expressLayouts = require("express-ejs-layouts");
expressLayouts.unless = unless;
const bodyParser = require("body-parser");
// database
const mongoose = require("mongoose");
mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.set("useFindAndModify", false);
const db = mongoose.connection;
db.on("error", (error) => console.error(error));
db.once("open", () => {
console.info("Connected to Mongoose");
});
const app = express();
const limiter = rateLimit({
max: process.env.RATE_LIMIT || 50,
windowMs: process.env.RATE_LIMIT_WINDOW || 10 * 1000,
message: "Too many requests, please try again later.",
});
app.enable("trust proxy");
app.use(limiter);
// body limit
app.use(express.json({ limit: "10kb" }));
app.set("view engine", "ejs");
app.set("views", __dirname + "/views");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(
expressLayouts.unless({
path: [/\/bull-board*/],
})
);
app.set("layout", "layouts/layout");
app.set("layout extractScripts", true);
// background
if (process.env.BACKGROUND == true) {
const { bullBoardServerAdapter } = require("./background");
const { ensureLoggedIn } = require("connect-ensure-login");
const passport = require("passport");
const session = require("express-session");
app.use(session({ secret: process.env.SESSION_SECRET }));
app.use(passport.initialize({}));
app.use(passport.session({}));
const authRouter = require("./routes/auth");
app.use("/login", authRouter);
app.use(
"/bull-board",
ensureLoggedIn("/login"),
bullBoardServerAdapter.getRouter()
);
console.info("BACKGROUND is up.");
}
// web
if (process.env.WEB == true) {
const webRouter = require("./web");
app.use("/", webRouter);
console.info("WEB is up.");
}
// api
if (!process.env.API_DISABLED) {
const apiLimiter = rateLimit({
max: process.env.API_RATE_LIMIT || 20,
windowMs: process.env.API_RATE_LIMIT_WINDOW || 10 * 1000,
message: "Too many requests, please try again later.",
// keyGenerator: function (req) {
// return req.ip;
// },
});
app.use("/api/", apiLimiter);
const apiRoutes = require("./routes/api");
app.use("/api/v1/", apiRoutes);
console.info("API is up.");
}
// 404 page
app.use((req, res) => {
res.status(404).render("errors/404", {
title: "404 Not Found",
});
});
const port = process.env.PORT || 8080;
app.listen(port, (err) => {
if (err) {
console.error(err);
return;
}
console.info("Listening on " + port);
});