substr/config.ts
2024-10-23 14:02:38 +02:00

75 lines
2.0 KiB
TypeScript

import { load } from "@std/dotenv";
import { parse as parseYaml } from "jsr:@std/yaml";
import { log } from "./log.ts";
const dirname = Deno.cwd();
await load({ envPath: `${dirname}/.env`, export: true });
let userConfigPath: string = "";
let staticUsers: { [key: string]: string } = {};
const defaultUserConfigPaths = [
"/etc/substr/users.yaml",
`${dirname}/users.yaml`,
];
for (const path of defaultUserConfigPaths) {
const fileInfo = await Deno.lstat(path).catch((_e) => undefined);
if (fileInfo && fileInfo.isFile) {
userConfigPath = path;
break;
}
}
try {
const fileContent = await Deno.readTextFile(userConfigPath);
const parsedContent = parseYaml(fileContent);
if (parsedContent !== null && typeof parsedContent === "object") {
staticUsers = parsedContent as { [key: string]: string };
log(`Serving content for pubkeys in users.yaml`, "blue");
}
} catch {
log(`Could not find or parse a users.yaml config`, "yellow");
}
const config = {
port: parseInt(Deno.env.get("PORT") || "8000"),
base_url: Deno.env.get("BASE_URL") || `http://localhost:8000`,
relay_urls: Deno.env.get("RELAY_URLS")?.split(",") || [],
staticUsers,
ldapEnabled: !!Deno.env.get("LDAP_URL"),
ldap: {
url: Deno.env.get("LDAP_URL"),
bindDN: Deno.env.get("LDAP_BIND_DN"),
password: Deno.env.get("LDAP_PASSWORD"),
searchDN: Deno.env.get("LDAP_SEARCH_DN"),
},
};
if (config.relay_urls.length === 0) {
log(`No relays configured. Please add at least one relay to RELAY_URLS.`);
Deno.exit(1);
}
if (config.ldapEnabled) {
if (
config.ldap.url && config.ldap.bindDN && config.ldap.password &&
config.ldap.searchDN
) {
log(`Serving content for pubkeys from ${config.ldap.url}`, "blue");
} else {
log(`The LDAP config is incomplete`);
Deno.exit(1);
}
} else {
log(`LDAP not enabled`, "blue");
}
if (Object.keys(staticUsers).length === 0 && !config.ldapEnabled) {
log(`Neither static users nor LDAP configured. Nothing to serve.`);
Deno.exit(1);
}
export default config;