Improve config processing

This commit is contained in:
Râu Cao 2024-10-24 15:17:11 +02:00
parent 0096f3cae3
commit b7974c8610
Signed by: raucao
GPG Key ID: 37036C356E56CC51
2 changed files with 38 additions and 21 deletions

View File

@ -28,10 +28,9 @@ try {
const parsedContent = parseYaml(fileContent); const parsedContent = parseYaml(fileContent);
if (parsedContent !== null && typeof parsedContent === "object") { if (parsedContent !== null && typeof parsedContent === "object") {
staticUsers = parsedContent as { [key: string]: string }; staticUsers = parsedContent as { [key: string]: string };
log(`Serving content for pubkeys in users.yaml`, "blue");
} }
} catch { } catch {
log(`Could not find or parse a users.yaml config`, "yellow"); // Nothing to do
} }
const config = { const config = {
@ -48,12 +47,24 @@ const config = {
}, },
}; };
if (config.relay_urls.length === 0) { const staticUsersConfigured = Object.keys(staticUsers).length > 0;
log(`No relays configured. Please add at least one relay to RELAY_URLS.`);
Deno.exit(1);
}
if (config.ldapEnabled) { export function ensureNecessaryConfigs() {
if (config.relay_urls.length === 0) {
log(
`No relays configured. Please add at least one relay to RELAY_URLS.`,
"yellow",
);
Deno.exit(1);
}
if (staticUsersConfigured) {
log(`Serving content for pubkeys in users.yaml`, "blue");
} else {
log(`Could not find or parse a users.yaml config`, "gray");
}
if (config.ldapEnabled) {
if ( if (
config.ldap.url && config.ldap.bindDN && config.ldap.password && config.ldap.url && config.ldap.bindDN && config.ldap.password &&
config.ldap.searchDN config.ldap.searchDN
@ -63,13 +74,17 @@ if (config.ldapEnabled) {
log(`The LDAP config is incomplete`); log(`The LDAP config is incomplete`);
Deno.exit(1); Deno.exit(1);
} }
} else { } else {
log(`LDAP not enabled`, "blue"); log(`LDAP not enabled`, "blue");
} }
if (Object.keys(staticUsers).length === 0 && !config.ldapEnabled) { if (!staticUsersConfigured && !config.ldapEnabled) {
log(`Neither static users nor LDAP configured. Nothing to serve.`); log(
`Neither static users nor LDAP configured. Nothing to serve.`,
"yellow",
);
Deno.exit(1); Deno.exit(1);
}
} }
export default config; export default config;

View File

@ -1,6 +1,6 @@
import { Application, Router, send } from "@oak/oak"; import { Application, Router, send } from "@oak/oak";
import { createSubtrTmpDirectories } from "./utils.ts"; import { createSubtrTmpDirectories } from "./utils.ts";
import config from "./config.ts"; import config, { ensureNecessaryConfigs } from "./config.ts";
import naddrHandler from "./handlers/naddr.ts"; import naddrHandler from "./handlers/naddr.ts";
import nprofileHandler from "./handlers/nprofile.ts"; import nprofileHandler from "./handlers/nprofile.ts";
import npubHandler from "./handlers/npub.ts"; import npubHandler from "./handlers/npub.ts";
@ -71,6 +71,8 @@ router.get("/assets/:path*", async (ctx) => {
} }
}); });
ensureNecessaryConfigs();
await createSubtrTmpDirectories(); await createSubtrTmpDirectories();
const app = new Application(); const app = new Application();