32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
import { createServer } from "node:http";
|
||
import { readFile } from "node:fs/promises";
|
||
import { join } from "node:path";
|
||
import { renderLanding } from "./landing.js";
|
||
import { legalPage } from "./legal.js";
|
||
|
||
const send = (res, code, type, body) => {
|
||
res.writeHead(code, { "content-type": `${type}; charset=utf-8` });
|
||
res.end(body);
|
||
};
|
||
|
||
export function createApp(config) {
|
||
return createServer(async (req, res) => {
|
||
const path = new URL(req.url, "http://x").pathname;
|
||
try {
|
||
if (path === "/healthz") return send(res, 200, "text/plain", "ok");
|
||
if (path === "/") return send(res, 200, "text/html", renderLanding(config, "ru"));
|
||
if (path === "/en") return send(res, 200, "text/html", renderLanding(config, "en"));
|
||
const img = /^\/img\/([\w-]+\.svg)$/.exec(path);
|
||
if (img) return send(res, 200, "image/svg+xml", await readFile(join(config.publicDir, "img", img[1])));
|
||
const doc = /^\/(offer|refund)$/.exec(path);
|
||
if (doc) {
|
||
const html = await legalPage(config, doc[1]);
|
||
if (html) return send(res, 200, "text/html", html);
|
||
}
|
||
} catch (e) {
|
||
if (e.code !== "ENOENT") console.error(e);
|
||
}
|
||
send(res, 404, "text/plain", "Не найдено");
|
||
});
|
||
}
|