2023-08-04 19:43:12 +01:00
|
|
|
import { genericUserAgent, version } from "../modules/config.js";
|
|
|
|
import { apiJSON, languageCode } from "../modules/sub/utils.js";
|
2023-05-21 20:13:05 +01:00
|
|
|
import { Bright, Cyan } from "../modules/sub/consoleText.js";
|
2023-08-04 19:43:12 +01:00
|
|
|
|
2023-05-21 20:13:05 +01:00
|
|
|
import { buildFront } from "../modules/build.js";
|
|
|
|
import findRendered from "../modules/pageRender/findRendered.js";
|
|
|
|
|
2023-08-04 19:43:12 +01:00
|
|
|
import { celebrationsEmoji } from "../modules/pageRender/elements.js";
|
|
|
|
import { changelogHistory } from "../modules/pageRender/onDemand.js";
|
2023-06-05 07:43:04 +01:00
|
|
|
|
2023-05-21 20:13:05 +01:00
|
|
|
export async function runWeb(express, app, gitCommit, gitBranch, __dirname) {
|
2023-08-04 19:43:12 +01:00
|
|
|
const startTime = new Date();
|
|
|
|
const startTimestamp = Math.floor(startTime.getTime());
|
2023-05-21 20:13:05 +01:00
|
|
|
|
2023-08-04 19:43:12 +01:00
|
|
|
await buildFront(gitCommit, gitBranch);
|
2023-06-05 07:43:04 +01:00
|
|
|
|
2023-05-21 20:13:05 +01:00
|
|
|
app.use('/', express.static('./build/min'));
|
|
|
|
app.use('/', express.static('./src/front'));
|
|
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
try { decodeURIComponent(req.path) } catch (e) { return res.redirect('/') }
|
|
|
|
next();
|
|
|
|
});
|
2023-08-04 19:43:12 +01:00
|
|
|
app.get('/onDemand', (req, res) => {
|
|
|
|
try {
|
|
|
|
if (req.query.blockId) {
|
|
|
|
let blockId = req.query.blockId.slice(0, 3);
|
|
|
|
let r, j;
|
|
|
|
switch(blockId) {
|
|
|
|
// changelog history
|
|
|
|
case "0":
|
|
|
|
r = changelogHistory();
|
|
|
|
j = r ? apiJSON(3, { t: r }) : apiJSON(0, {
|
|
|
|
t: "couldn't render this block, please try again!"
|
|
|
|
})
|
|
|
|
break;
|
|
|
|
// celebrations emoji
|
|
|
|
case "1":
|
|
|
|
r = celebrationsEmoji();
|
|
|
|
j = r ? apiJSON(3, { t: r }) : false
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
j = apiJSON(0, {
|
|
|
|
t: "couldn't find a block with this id"
|
|
|
|
})
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (j.body) {
|
|
|
|
return res.status(j.status).json(j.body);
|
|
|
|
} else {
|
|
|
|
return res.status(204).end();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return res.status(400).json({
|
|
|
|
status: "error",
|
|
|
|
text: "couldn't render this block, please try again!"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return res.status(400).json({
|
|
|
|
status: "error",
|
|
|
|
text: "couldn't render this block, please try again!"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
2023-05-21 20:13:05 +01:00
|
|
|
app.get("/status", (req, res) => {
|
2023-08-04 19:43:12 +01:00
|
|
|
return res.status(200).end()
|
2023-05-21 20:13:05 +01:00
|
|
|
});
|
|
|
|
app.get("/", (req, res) => {
|
2023-08-04 19:43:12 +01:00
|
|
|
return res.sendFile(`${__dirname}/${findRendered(languageCode(req), req.header('user-agent') ? req.header('user-agent') : genericUserAgent)}`)
|
2023-05-21 20:13:05 +01:00
|
|
|
});
|
|
|
|
app.get("/favicon.ico", (req, res) => {
|
2023-08-04 19:43:12 +01:00
|
|
|
return res.sendFile(`${__dirname}/src/front/icons/favicon.ico`)
|
2023-06-05 07:43:04 +01:00
|
|
|
});
|
2023-05-21 20:13:05 +01:00
|
|
|
app.get("/*", (req, res) => {
|
2023-08-04 19:43:12 +01:00
|
|
|
return res.redirect('/')
|
2023-05-21 20:13:05 +01:00
|
|
|
});
|
|
|
|
|
2023-12-02 16:01:58 +00:00
|
|
|
app.listen(process.env.webPort || 9001, () => {
|
2023-08-04 19:43:12 +01:00
|
|
|
console.log(`\n` +
|
|
|
|
`${Cyan("cobalt")} WEB ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` +
|
|
|
|
`Start time: ${Bright(`${startTime.toUTCString()} (${startTimestamp})`)}\n\n` +
|
|
|
|
`URL: ${Cyan(`${process.env.webURL}`)}\n` +
|
2023-12-02 16:01:58 +00:00
|
|
|
`Port: ${process.env.webPort || 9001}\n`
|
2023-08-04 19:43:12 +01:00
|
|
|
)
|
2023-05-21 20:13:05 +01:00
|
|
|
})
|
|
|
|
}
|