diff --git a/bun.lockb b/bun.lockb index a757b99e..bf2b9bda 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/index.ts b/index.ts index 8a8b924e..db74de9f 100644 --- a/index.ts +++ b/index.ts @@ -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, -}); diff --git a/package.json b/package.json index cb9c4cbb..dc939b4a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/utils/config.ts b/utils/config.ts index 50b76eb1..d2257dd3 100644 --- a/utils/config.ts +++ b/utils/config.ts @@ -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 };