-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
143 lines (120 loc) · 3.9 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
135
136
137
138
139
140
141
142
143
import packageData from "./package.json" assert { type: "json" };
import moment from "moment";
import fetch from "node-fetch";
import dotenv from "dotenv";
dotenv.config();
import fastify from "fastify";
import fastifySession from "@fastify/session";
import fastifyCookie from "@fastify/cookie";
import config from "./config.json" assert { type: "json" };
import features from "./features.json" assert { type: "json" };
import lang from "./lang.json" assert { type: "json" };
import db from "./controllers/databaseController";
import { getWebAnnouncement } from "./controllers/announcementController";
// Paths
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import("./controllers/discordController.js");
import("./cron/userCodeExpiryCron.js");
//
// Website Related
//
// Site Routes
import siteRoutes from "./routes";
import apiRoutes from "./api/routes";
import apiRedirectRoutes from "./api/internal_redirect";
// API token authentication
import verifyToken from "./api/routes/verifyToken";
import { getGlobalImage } from "./api/common";
import { client } from "./controllers/discordController";
//
// Application Boot
//
const buildApp = async () => {
const app = fastify({ logger: config.debug });
// When app errors, render the error on a page, do not provide JSON
app.setNotFoundHandler(async function (req, res) {
return res.view("session/notFound", {
pageTitle: `404 Not Found`,
config: config,
error: error,
req: req,
features: features,
globalImage: await getGlobalImage(),
announcementWeb: await getWebAnnouncement(),
});
});
// When app errors, render the error on a page, do not provide JSON
app.setErrorHandler(async function (error, req, res) {
res.view("session/error", {
pageTitle: `Server Error`,
config: config,
error: error,
req: req,
features: features,
globalImage: await getGlobalImage(),
announcementWeb: await getWebAnnouncement(),
});
});
// EJS Rendering Engine
await app.register(await import("@fastify/view"), {
engine: {
ejs: await import("ejs"),
},
root: path.join(__dirname, "views"),
});
await app.register(await import("@fastify/static"), {
root: path.join(__dirname, "assets"),
prefix: "/",
});
await app.register(await import("@fastify/formbody"));
await app.register((instance, options, next) => {
// API routes (Token authenticated)
instance.addHook("preValidation", verifyToken);
apiRoutes(instance, client, moment, config, db, features, lang);
next();
});
await app.register((instance, options, next) => {
// Don't authenticate the Redirect routes. These are
// protected by
apiRedirectRoutes(instance, config, lang);
next();
});
// Sessions
await app.register(fastifyCookie, {
secret: process.env.sessionCookieSecret, // for cookies signature
});
await app.register(fastifySession, {
cookieName: "sessionId",
secret: process.env.sessionCookieSecret,
cookie: { secure: false },
expires: 1800000,
});
await app.register((instance, options, next) => {
// Routes
siteRoutes(instance, client, fetch, moment, config, db, features, lang);
next();
});
app.addHook("preHandler", (req, res, next) => {
req.session.authenticated = false;
next();
});
try {
const port = process.env.PORT;
app.listen({ port: port, host: "0.0.0.0" }, (err) => {
if (err) {
app.log.error(err);
process.exit(1);
}
});
console.log(
`\n// ${packageData.name} v.${packageData.version}\nGitHub Repository: ${packageData.homepage}\nCreated By: ${packageData.author}`
);
console.log(`Site and API is listening to the port ${process.env.PORT}`);
} catch (error) {
app.log.error(`Unable to start the server:\n${error}`);
}
};
buildApp();