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);
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");
// Nothing to do
}
const config = {
@ -48,11 +47,23 @@ const config = {
},
};
const staticUsersConfigured = Object.keys(staticUsers).length > 0;
export function ensureNecessaryConfigs() {
if (config.relay_urls.length === 0) {
log(`No relays configured. Please add at least one relay to RELAY_URLS.`);
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 (
config.ldap.url && config.ldap.bindDN && config.ldap.password &&
@ -67,9 +78,13 @@ if (config.ldapEnabled) {
log(`LDAP not enabled`, "blue");
}
if (Object.keys(staticUsers).length === 0 && !config.ldapEnabled) {
log(`Neither static users nor LDAP configured. Nothing to serve.`);
if (!staticUsersConfigured && !config.ldapEnabled) {
log(
`Neither static users nor LDAP configured. Nothing to serve.`,
"yellow",
);
Deno.exit(1);
}
}
export default config;

View File

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