feat: Use custom importer to import config

This commit is contained in:
Jesse Wierzbinski 2023-12-08 17:32:45 -10:00
parent e34c0aa57c
commit 1138e7be06
No known key found for this signature in database
4 changed files with 39 additions and 12 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -5,24 +5,21 @@ import { appendFile } from "fs/promises";
import { matches } from "ip-matching";
import { getFromRequest } from "~database/entities/User";
import { mkdir } from "fs/promises";
import { client } from "~database/datasource";
import type { PrismaClientInitializationError } from "@prisma/client/runtime/library";
import { HookTypes, Server } from "~plugins/types";
import { initializeRedisCache } from "@redis";
import { connectMeili } from "@meilisearch";
import routes from "~pages/routes";
import { matchRoute } from "~routes";
const timeAtStart = performance.now();
const server = new Server();
console.log(`${chalk.green(`>`)} ${chalk.bold("Starting Lysand...")}`);
server.emit(HookTypes.PreServe);
const config = getConfig();
const requests_log = Bun.file(process.cwd() + "/logs/requests.log");
// Needs to be imported after config is loaded
import { client } from "~database/datasource";
// NODE_ENV seems to be broken and output `development` even when set to production, so use the flag instead
const isProd =
process.env.NODE_ENV === "production" || process.argv.includes("--prod");
@ -215,7 +212,3 @@ console.log(
`Serving ${chalk.blue(postCount)} posts`
)}`
);
server.emit(HookTypes.PostServe, {
postCount,
});

View file

@ -83,6 +83,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.461.0",
"@iarna/toml": "^2.2.5",
"@prisma/client": "^5.6.0",
"blurhash": "^2.0.5",
"bullmq": "^4.14.4",

View file

@ -1,4 +1,22 @@
import data from "../config/config.toml";
import { parse } from "@iarna/toml";
import chalk from "chalk";
const scanConfig = async () => {
const config = Bun.file(process.cwd() + "/config/config.toml");
if (!(await config.exists())) {
console.error(
`${chalk.red(``)} ${chalk.bold(
"Error while reading config: "
)} Config file not found`
);
process.exit(1);
}
return parse(await config.text()) as ConfigType;
};
let config = await scanConfig();
export interface ConfigType {
database: {
@ -350,7 +368,7 @@ export const configDefaults: ConfigType = {
export const getConfig = () => {
return {
...configDefaults,
...(data as ConfigType),
...config,
};
};
@ -359,3 +377,18 @@ export const getHost = () => {
return url.host;
};
// Refresh config every 5 seconds
setInterval(() => {
scanConfig()
.then(newConfig => {
if (newConfig !== config) {
config = newConfig;
}
})
.catch(e => {
console.error(e);
});
}, 5000);
export { config };