core/web: update response functions & clean up

This commit is contained in:
wukko 2024-05-16 22:35:49 +06:00
parent b3e36bd7ab
commit f9ce6ba8cc
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2

View file

@ -1,12 +1,14 @@
import { version, env } from "../modules/config.js";
import { apiJSON, languageCode } from "../modules/sub/utils.js";
import { Bright, Cyan } from "../modules/sub/consoleText.js"; import { Bright, Cyan } from "../modules/sub/consoleText.js";
import { languageCode } from "../modules/sub/utils.js";
import { version, env } from "../modules/config.js";
import { buildFront } from "../modules/build.js"; import { buildFront } from "../modules/build.js";
import findRendered from "../modules/pageRender/findRendered.js"; import findRendered from "../modules/pageRender/findRendered.js";
import { celebrationsEmoji } from "../modules/pageRender/elements.js"; import { celebrationsEmoji } from "../modules/pageRender/elements.js";
import { changelogHistory } from "../modules/pageRender/onDemand.js"; import { changelogHistory } from "../modules/pageRender/onDemand.js";
import { createResponse } from "../modules/processing/request.js";
export async function runWeb(express, app, gitCommit, gitBranch, __dirname) { export async function runWeb(express, app, gitCommit, gitBranch, __dirname) {
const startTime = new Date(); const startTime = new Date();
@ -20,33 +22,40 @@ export async function runWeb(express, app, gitCommit, gitBranch, __dirname) {
app.use((req, res, next) => { app.use((req, res, next) => {
try { decodeURIComponent(req.path) } catch (e) { return res.redirect('/') } try { decodeURIComponent(req.path) } catch (e) { return res.redirect('/') }
next(); next();
}); })
app.get('/onDemand', (req, res) => { app.get('/onDemand', (req, res) => {
try { try {
if (req.query.blockId) { if (req.query.blockId) {
let blockId = req.query.blockId.slice(0, 3); let blockId = req.query.blockId.slice(0, 3);
let r, j; let blockData;
switch(blockId) { switch(blockId) {
// changelog history // changelog history
case "0": case "0":
r = changelogHistory(); let history = changelogHistory();
j = r ? apiJSON(3, { t: r }) : apiJSON(0, { if (history) {
blockData = createResponse("success", { t: history })
} else {
blockData = createResponse("error", {
t: "couldn't render this block, please try again!" t: "couldn't render this block, please try again!"
}) })
}
break; break;
// celebrations emoji // celebrations emoji
case "1": case "1":
r = celebrationsEmoji(); let celebration = celebrationsEmoji();
j = r ? apiJSON(3, { t: r }) : false if (celebration) {
blockData = createResponse("success", { t: celebration })
}
break; break;
default: default:
j = apiJSON(0, { blockData = createResponse("error", {
t: "couldn't find a block with this id" t: "couldn't find a block with this id"
}) })
break; break;
} }
if (j.body) { if (blockData?.body) {
return res.status(j.status).json(j.body); return res.status(blockData.status).json(blockData.body);
} else { } else {
return res.status(204).end(); return res.status(204).end();
} }
@ -56,25 +65,25 @@ export async function runWeb(express, app, gitCommit, gitBranch, __dirname) {
text: "couldn't render this block, please try again!" text: "couldn't render this block, please try again!"
}); });
} }
} catch (e) { } catch {
return res.status(400).json({ return res.status(400).json({
status: "error", status: "error",
text: "couldn't render this block, please try again!" text: "couldn't render this block, please try again!"
}) })
} }
}); })
app.get("/status", (req, res) => {
return res.status(200).end()
});
app.get("/", (req, res) => { app.get("/", (req, res) => {
return res.sendFile(`${__dirname}/${findRendered(languageCode(req))}`) return res.sendFile(`${__dirname}/${findRendered(languageCode(req))}`)
}); })
app.get("/favicon.ico", (req, res) => { app.get("/favicon.ico", (req, res) => {
return res.sendFile(`${__dirname}/src/front/icons/favicon.ico`) return res.sendFile(`${__dirname}/src/front/icons/favicon.ico`)
}); })
app.get("/*", (req, res) => { app.get("/*", (req, res) => {
return res.redirect('/') return res.redirect('/')
}); })
app.listen(env.webPort, () => { app.listen(env.webPort, () => {
console.log(`\n` + console.log(`\n` +