-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
214 lines (181 loc) · 4.34 KB
/
index.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
// Packages
const express = require("express"),
app = express();
const fs = require("node:fs");
const logger = require("./logger");
require("dotenv").config();
// Packages for Markdown
const { JSDOM } = require("jsdom");
const marked = require("marked");
const metadataParser = require("@cmdlucas/markdown-metadata");
const dompurify = require("dompurify"),
purify = dompurify(new JSDOM("").window);
// Configure marked
marked.setOptions({
renderer: new marked.Renderer(),
highlight: function (code, lang) {
const hljs = require("highlight.js");
const language = hljs.getLanguage(lang) ? lang : "plaintext";
return hljs.highlight(code, {
language,
}).value;
},
langPrefix: "hljs language-",
pedantic: false,
gfm: true,
breaks: false,
sanitize: false,
smartLists: true,
smartypants: false,
xhtml: false,
});
// Middleware
app.use(express.json());
app.set("view engine", "ejs");
app.use(
express.static("static", {
root: __dirname,
})
);
// Endpoint Map
const endpoints = new Map();
const endpointFiles = fs
.readdirSync("./endpoints")
.filter((file) => file.endsWith(".js"));
for (const file of endpointFiles) {
const endpoint = require(`./endpoints/${file}`);
endpoints.set(endpoint.data.name, endpoint);
}
// API Endpoint Map
const apiEndpoints = new Map();
const apiEndpointFiles = fs
.readdirSync("./api")
.filter((file) => file.endsWith(".js"));
for (const file of apiEndpointFiles) {
const endpoint = require(`./api/${file}`);
apiEndpoints.set(
`${endpoint.data.category}/${endpoint.data.name}`,
endpoint
);
}
// API Documentaton Map
const apiDocs = new Map();
const apiDocFiles = fs
.readdirSync("./docs")
.filter((file) => file.endsWith(".md"));
for (const file of apiDocFiles) {
const contents = fs.readFileSync(`${__dirname}/docs/${file}`, "utf8");
const result = metadataParser.parse(contents);
const results = {
metadata: result.metadata,
content: purify.sanitize(marked.parse(result.content)),
};
apiDocs.set(file.split(".")[0], results);
}
// Endpoints
app.get("/", async (req, res) => {
const page = endpoints.get("root");
if (!page)
return res.status(404).json({
error: "This endpoint does not exist.",
});
try {
page.execute({
request: req,
response: res,
});
} catch (error) {
logger.error(`Page (${page.data.name})`, error);
return res.status(500).json({
error: error,
});
}
});
app.get("/page/:page", (req, res) => {
const page = endpoints.get(req.params["page"]);
if (!page)
return res.status(404).json({
error: "This page does not exist.",
});
try {
page.execute({
request: req,
response: res,
});
} catch (error) {
logger.error(`Page (${page.data.name})`, error);
return res.status(500).json({
error: error,
});
}
});
// API Endpoints
app.all("/api", (req, res) => {
return res.status(404).json({
error: "This endpoint does not exist!",
});
});
app.all("/api/:category/:endpoint", (req, res) => {
const endpoint = apiEndpoints.get(
`${req.params["category"]}/${req.params["endpoint"]}`
);
if (!endpoint)
return res.status(404).json({
error: "This endpoint does not exist.",
});
try {
if (endpoint.data.method != req.method)
return res.status(405).json({
error: `Method \`${req.method}\` is not allowed for this endpoint.`,
});
endpoint.execute({
request: req,
response: res,
});
} catch (error) {
logger.error(
`API Endpoint (${endpoint.data.category}/${endpoint.data.name})`,
error
);
return res.status(500).json({
error: error,
});
}
});
// API Documentation Endpoints
app.get("/docs", async (req, res) => {
let data = [];
apiDocs.forEach((content) => {
data.push({
metadata: content.metadata,
content: content.content,
});
});
res.render("all_docs", {
data: data,
});
});
app.get("/docs/:title", async (req, res) => {
const document = apiDocs.get(req.params["title"] || "null");
if (!document)
return res
.status(404)
.send(
"It seems the documentation page that you are looking for does not exist."
);
else
return res.render("doc_page", {
metadata: document.metadata,
content: document.content,
});
});
// Page not Found
app.all("*", (req, res) => {
return res.status(404).json({
error: "This endpoint does not exist.",
});
});
// Start Server
app.listen(process.env.PORT, () => {
logger.info("Express", `Server started on port ${process.env.PORT.green}`);
});