-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (65 loc) · 2.09 KB
/
Copy pathindex.js
File metadata and controls
81 lines (65 loc) · 2.09 KB
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
const querystring = require("node:querystring");
const fs = require("node:fs");
const _ = require("lodash");
const fastify = require("fastify")({ trustProxy: true });
const Logger = require("./scripts/Logger");
const config = require("./config/config");
function log(request, ...message) {
if (process.env.EnableLogging === "true") {
return Logger.log(
request.method,
`${Logger.colors.cyan(request.ip)} -> ${Logger.colors.gray(request.hostname)}${request.url}`,
...message,
);
}
}
fastify.addContentTypeParser("application/x-www-form-urlencoded", { parseAs: "buffer" }, (req, body, done) =>
done(null, querystring.parse(body.toString())),
);
// Logging
fastify.addHook("onRequest", (request, reply, done) => {
log(request);
done();
});
function registerRoutes(path, urlPath) {
const originalPath = path;
function recurse(path, urlPath) {
fs.readdirSync(path).forEach((file) => {
if (fs.statSync(`${path}/${file}`).isDirectory()) recurse(`${path}/${file}`, urlPath);
if (file.endsWith(".js")) {
const endpoint = require(`./${path}/${file}`);
(Array.isArray(urlPath) ? _.uniq(urlPath) : [urlPath]).forEach((urlPath) => {
fastify.register(
(api, options, done) => {
try {
endpoint(api);
} catch (error) {
Logger.error("Server", `Error in ${Logger.colors.cyan(`${path}/${file}`)}:\n`, error);
}
done();
},
{ prefix: path.replace(originalPath, urlPath) },
);
});
}
});
}
return recurse(path, urlPath);
}
registerRoutes("routes/dashboard", config.dashboardPath);
registerRoutes("routes/database", config.databasePath);
fastify.setNotFoundHandler((request, reply) => {
return reply.status(404).send("Not Found!");
});
fastify.setErrorHandler((error, request, reply) => {
if (error.validation) return reply.status(400).send("-1");
Logger.error("Server", error);
return reply.status(500).send("-1");
});
fastify.listen({ port: config.port }, (error, address) => {
if (error) {
Logger.error("Server", error);
process.exit(1);
}
Logger.log("Server", `Listening at: ${Logger.colors.cyan(address)}`);
});