@opengis/fastify-table
Version:
core-plugins
50 lines (49 loc) • 1.85 kB
JavaScript
import path from "node:path";
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "url";
import config from "../../../../../config.js";
import { handlebars } from "../../../../helpers/index.js";
import pgClients from "../../../../plugins/pg/pgClients.js";
import getTemplate from "../../../../plugins/table/funcs/getTemplate.js";
// relative default template filepath
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const headers = {
"Content-Type": "text/html; charset=UTF-8",
"Cache-Control": "no-cache",
"Accept-CH": "Viewport-Width, Width",
};
export default async function loginTemplate(req, reply) {
const { pg = pgClients.client } = req;
const body = await getTemplate("page", "login");
const { rows = [] } = config.pg && pg?.pk?.["admin.properties"]
? await pg.query("select property_key as key, property_text as val from admin.properties where property_key is not null")
: {};
const settings = rows.reduce((p, { key, val }) => {
const [k1, k2] = key.split(".");
p[k1] = p[k1] || {};
p[k1][k2] = val;
return p;
}, {});
if (!body) {
const defaultBody = await readFile(path.join(dirname, "../../../../templates/page/login.html"), "utf8");
const html = await handlebars.compile(defaultBody)({
settings,
req,
protocol: req.protocol,
hostname: req.hostname,
port: process.env.PORT,
config,
});
return reply.headers(headers).send(html);
}
const html = await handlebars.compile(body)({
settings,
req,
protocol: req.protocol,
hostname: req.hostname,
port: process.env.PORT,
config,
});
return reply.headers(headers).send(html);
}