separated web and api, build improvements
wip: - separate web and api servers. - script for building static pages. - building improvements. - async localisation preloading.
This commit is contained in:
@@ -18,7 +18,7 @@ import { getJSON } from "./modules/api.js";
|
||||
import { apiJSON, checkJSONPost, getIP, languageCode } from "./modules/sub/utils.js";
|
||||
import { Bright, Cyan, Green, Red } from "./modules/sub/consoleText.js";
|
||||
import stream from "./modules/stream/stream.js";
|
||||
import loc from "./localization/manager.js";
|
||||
import loc, { loadLoc } from "./localization/manager.js";
|
||||
import { buildFront } from "./modules/build.js";
|
||||
import { changelogHistory } from "./modules/pageRender/onDemand.js";
|
||||
import { sha256 } from "./modules/sub/crypto.js";
|
||||
@@ -56,7 +56,12 @@ if (process.env.selfURL && process.env.port) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
const startTime = new Date();
|
||||
const startTimestamp = Math.floor(startTime.getTime());
|
||||
|
||||
// preload localization files and build static pages
|
||||
await loadLoc();
|
||||
await buildFront(commitHash, branch);
|
||||
|
||||
app.use('/api/:type', cors(corsConfig));
|
||||
@@ -64,7 +69,7 @@ if (process.env.selfURL && process.env.port) {
|
||||
app.use('/api/stream', apiLimiterStream);
|
||||
app.use('/api/onDemand', apiLimiter);
|
||||
|
||||
app.use('/', express.static('./min'));
|
||||
app.use('/', express.static('./build/min'));
|
||||
app.use('/', express.static('./src/front'));
|
||||
|
||||
app.use((req, res, next) => {
|
||||
@@ -164,6 +169,16 @@ if (process.env.selfURL && process.env.port) {
|
||||
res.status(j.status).json(j.body)
|
||||
}
|
||||
break;
|
||||
case 'serverInfo':
|
||||
res.status(200).json({
|
||||
version: version,
|
||||
commit: commitHash,
|
||||
branch: branch,
|
||||
url: process.env.apiURL,
|
||||
cors: process.env.cors,
|
||||
startTime: `${startTimestamp}`
|
||||
});
|
||||
break;
|
||||
default:
|
||||
let j = apiJSON(0, { t: "unknown response type" })
|
||||
res.status(j.status).json(j.body);
|
||||
@@ -174,12 +189,12 @@ if (process.env.selfURL && process.env.port) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
app.get("/api/status", (req, res) => {
|
||||
res.status(200).end()
|
||||
});
|
||||
app.get("/api", (req, res) => {
|
||||
res.redirect('/api/json')
|
||||
});
|
||||
app.get("/status", (req, res) => {
|
||||
res.status(200).end()
|
||||
});
|
||||
app.get("/", (req, res) => {
|
||||
res.sendFile(`${__dirname}/${findRendered(languageCode(req), req.header('user-agent') ? req.header('user-agent') : genericUserAgent)}`);
|
||||
});
|
||||
@@ -191,8 +206,7 @@ if (process.env.selfURL && process.env.port) {
|
||||
});
|
||||
|
||||
app.listen(process.env.port, () => {
|
||||
let startTime = new Date();
|
||||
console.log(`\n${Cyan(appName)} ${Bright(`v.${version}-${commitHash} (${branch})`)}\nStart time: ${Bright(`${startTime.toUTCString()} (${Math.floor(new Date().getTime())})`)}\n\nURL: ${Cyan(`${process.env.selfURL}`)}\nPort: ${process.env.port}\n`)
|
||||
console.log(`\n${Cyan(appName)} ${Bright(`v.${version}-${commitHash} (${branch})`)}\nStart time: ${Bright(`${startTime.toUTCString()} (${Math.floor(startTimestamp)})`)}\n\nURL: ${Cyan(`${process.env.selfURL}`)}\nPort: ${process.env.port}\n`)
|
||||
})
|
||||
} else {
|
||||
console.log(Red(`cobalt hasn't been configured yet or configuration is invalid.\n`) + Bright(`please run the setup script to fix this: `) + Green(`npm run setup`));
|
||||
|
||||
Reference in New Issue
Block a user