-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
162 lines (143 loc) · 5.31 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const express = require("express");
const compression = require("compression");
const path = require("path");
const axios = require("axios");
const app = express();
const {
getTheme,
getFaviconTheme,
getBadge,
getUserData,
getMonkeyTypeThemesData,
getMonkeyTypeBadgesData,
} = require("./public/script/monkeytypeData");
const { getOGSvg, getSvg } = require("./public/script/generateSvg");
require("dotenv").config();
app.use(compression());
app.use(express.static("public"));
app.use("/styles", express.static("dist"));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "public", "views"));
app.use("/node_modules", express.static(path.join(__dirname, "node_modules")));
app.get("/", (req, res) => {
const data = {
domain: process.env.DOMAIN,
};
res.render("index", { data });
});
app.get("/mr-command/theme", async (req, res) => {
const themesData = await getMonkeyTypeThemesData();
res.set("Content-Type", "application/json");
res.send(themesData);
});
app.get("/mr-command/badge", async (req, res) => {
const badgesData = await getMonkeyTypeBadgesData();
res.set("Content-Type", "application/json");
res.send(badgesData);
});
app.get("/mr-command/favicon", async (req, res) => {
const faviconData = getFaviconTheme();
res.render("favicon", { faviconData });
});
app.get("/mr-command/logo", async (req, res) => {
const faviconData = getFaviconTheme();
res.render("logo", { faviconData });
});
app.get("/sitemap.xml", (req, res) => {
res.sendFile(path.join(__dirname, "public/assets", "sitemap.xml"));
});
app.get("/robots.txt", (req, res) => {
res.sendFile(path.join(__dirname, "public/assets", "robots.txt"));
});
app.get(
["/og-image/:userId/:themeName", "/og-image/:userId"],
async (req, res) => {
const userId = req.params.userId;
const themeName = req.params.themeName;
req.query.lbpb == "true" ? (leaderBoards = personalBests = true) : null;
const userData = await getUserData(userId);
const theme = getTheme(themeName);
if (userData === undefined || userData.name === undefined) {
const svg = await getOGSvg(null, theme, null);
res.set("Content-Type", "image/svg+xml");
res.send(svg);
return;
}
let badge = null;
if (userData.inventory !== null && userData.inventory !== undefined) {
if (userData.inventory.badges.length !== 0) {
badge = getBadge(userData.inventory.badges[0].id);
for (let i = 0; i < userData.inventory.badges.length; i++) {
if (userData.inventory.badges[i].selected === true) {
badge = getBadge(userData.inventory.badges[i].id);
break;
}
}
}
}
const ogSvg = await getOGSvg(userData, theme, badge);
try {
const response = await axios.post(
"https://mr-api.zeabur.app/og-image",
{ og_svg: ogSvg },
{ responseType: "arraybuffer" },
);
res.setHeader("Content-Type", "image/png");
res.send(Buffer.from(response.data, "binary"));
} catch (error) {
console.error("Error converting SVG to PNG:", error);
res.status(500).send("Error converting SVG to PNG");
}
},
);
app.get(["/:userId/:themeName", "/:userId"], async (req, res) => {
const data = {
domain: process.env.DOMAIN,
userId: req.params.userId,
theme: getTheme(
req.params.themeName ? req.params.themeName : "serika_dark",
),
userData: await getUserData(req.params.userId),
svgUrl: `${process.env.DOMAIN}/generate-svg/${req.params.userId}/${req.params.themeName}?lbpb=true`,
};
res.render("user", { data });
});
app.get("/generate-svg/:userId/:themeName", async (req, res) => {
const userId = req.params.userId;
const themeName = req.params.themeName;
let leaderBoards = req.query.lb == "true" ? true : false;
let personalBests = req.query.pb == "true" ? true : false;
req.query.lbpb == "true" ? (leaderBoards = personalBests = true) : null;
const userData = await getUserData(userId);
const theme = getTheme(themeName);
if (userData === undefined || userData.name === undefined) {
const svg = await getSvg(null, theme, null, false, false);
res.set("Content-Type", "image/svg+xml");
res.send(svg);
return;
}
let badge = null;
if (userData.inventory !== null && userData.inventory !== undefined) {
if (userData.inventory.badges.length !== 0) {
badge = getBadge(userData.inventory.badges[0].id);
for (let i = 0; i < userData.inventory.badges.length; i++) {
if (userData.inventory.badges[i].selected === true) {
badge = getBadge(userData.inventory.badges[i].id);
break;
}
}
}
}
const svg = await getSvg(
userData,
theme,
badge,
leaderBoards,
personalBests,
);
res.set("Content-Type", "image/svg+xml");
res.send(svg);
});
app.listen(process.env.PORT || 3000, async () => {
console.log("Server started on port " + (process.env.PORT || 3000));
});