Fix alias mapping in Express
This commit is contained in:
parent
97b1f05118
commit
b5267a4243
|
@ -9,6 +9,9 @@ import { log } from "./util.js";
|
||||||
// populated at runtime and updated asynchronously.
|
// populated at runtime and updated asynchronously.
|
||||||
export let langs = {};
|
export let langs = {};
|
||||||
|
|
||||||
|
// Map from language aliases and IDs to canonical language IDs.
|
||||||
|
export let aliases = {};
|
||||||
|
|
||||||
// Read languages from JSON files in /opt/riju/langs, and update the
|
// Read languages from JSON files in /opt/riju/langs, and update the
|
||||||
// global langs variable in this module. Never throw an error. If
|
// global langs variable in this module. Never throw an error. If
|
||||||
// there is a problem then just leave the languages as they previously
|
// there is a problem then just leave the languages as they previously
|
||||||
|
@ -16,6 +19,7 @@ export let langs = {};
|
||||||
async function readLangsFromDisk() {
|
async function readLangsFromDisk() {
|
||||||
try {
|
try {
|
||||||
const newLangs = {};
|
const newLangs = {};
|
||||||
|
const newAliases = {};
|
||||||
for (const filename of await fs.readdir("/opt/riju/langs")) {
|
for (const filename of await fs.readdir("/opt/riju/langs")) {
|
||||||
if (path.parse(filename).ext !== ".json") {
|
if (path.parse(filename).ext !== ".json") {
|
||||||
continue;
|
continue;
|
||||||
|
@ -31,6 +35,10 @@ async function readLangsFromDisk() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
newLangs[id] = langConfig;
|
newLangs[id] = langConfig;
|
||||||
|
newAliases[id] = id;
|
||||||
|
for (const alias of langConfig.aliases || []) {
|
||||||
|
newAliases[alias] = id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
log.info(
|
log.info(
|
||||||
`Loaded ${
|
`Loaded ${
|
||||||
|
@ -38,6 +46,7 @@ async function readLangsFromDisk() {
|
||||||
} language configuration(s) from disk`
|
} language configuration(s) from disk`
|
||||||
);
|
);
|
||||||
langs = newLangs;
|
langs = newLangs;
|
||||||
|
aliases = newAliases;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error("Failed to read languages from disk:", err);
|
log.error("Failed to read languages from disk:", err);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import ws from "express-ws";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
|
|
||||||
import * as api from "./api.js";
|
import * as api from "./api.js";
|
||||||
import { langs } from "./langs.js";
|
import { aliases, langs } from "./langs.js";
|
||||||
import { log } from "./util.js";
|
import { log } from "./util.js";
|
||||||
|
|
||||||
const host = process.env.HOST || "localhost";
|
const host = process.env.HOST || "localhost";
|
||||||
|
@ -28,10 +28,7 @@ app.get("/", (_, res) => {
|
||||||
analyticsEnabled,
|
analyticsEnabled,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
res.send(
|
res.send(503, "Encountered unexpected error while loading languages\n");
|
||||||
503,
|
|
||||||
"Still loading languages, or encountered error while doing so\n"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (const [lang, { aliases }] of Object.entries(langs)) {
|
for (const [lang, { aliases }] of Object.entries(langs)) {
|
||||||
|
@ -48,14 +45,19 @@ app.get("/:lang", (req, res) => {
|
||||||
const lowered = lang.toLowerCase();
|
const lowered = lang.toLowerCase();
|
||||||
if (lowered !== lang) {
|
if (lowered !== lang) {
|
||||||
res.redirect(301, `/${lowered}`);
|
res.redirect(301, `/${lowered}`);
|
||||||
} else if (langs[lang]) {
|
return;
|
||||||
res.render(path.resolve("frontend/pages/app"), {
|
|
||||||
config: { id: lang, ...langs[lang] },
|
|
||||||
analyticsEnabled,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.send(404, `No such language: ${lang}\n`);
|
|
||||||
}
|
}
|
||||||
|
const canonical = aliases[lang];
|
||||||
|
if (!canonical) {
|
||||||
|
res.send(404, `No such language: ${lang}\n`);
|
||||||
|
} else if (canonical !== lang) {
|
||||||
|
res.redirect(301, `/${canonical}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.render(path.resolve("frontend/pages/app"), {
|
||||||
|
config: langs[lang],
|
||||||
|
analyticsEnabled,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
app.use("/css", express.static("frontend/styles"));
|
app.use("/css", express.static("frontend/styles"));
|
||||||
app.use("/js", express.static("frontend/out"));
|
app.use("/js", express.static("frontend/out"));
|
||||||
|
|
Loading…
Reference in New Issue