diff options
author | Raphael <mail@raphaelkabo.com> | 2023-05-13 19:24:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-13 19:24:13 +0100 |
commit | 3ec47e9c512ebf7432a4fa139bc0478f9ba98d3b (patch) | |
tree | a54618c5dc9f84a8ba06f9c8c33d244f5f26f779 /src/lib | |
parent | f1a5a9b9a0f8357e25e3b2c8d72d745093bda9c3 (diff) | |
parent | 365398359f2d80345a61f76f3ecf0fd6eb9bdde6 (diff) |
Merge pull request #98 from lowercasename/rk/new-config
TOML config library and migration from CommonJS to ES modules
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/config.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/lib/config.ts b/src/lib/config.ts new file mode 100644 index 0000000..d235145 --- /dev/null +++ b/src/lib/config.ts @@ -0,0 +1,56 @@ +import fs from "fs"; +import toml from "toml"; + +interface GathioConfig { + general: { + domain: string; + port: string; + email: string; + site_name: string; + is_federated: boolean; + email_logo_url: string; + show_kofi: boolean; + mail_service: "nodemailer" | "sendgrid"; + }; + database: { + mongodb_url: string; + }; + nodemailer?: { + smtp_server: string; + smtp_port: string; + smtp_username: string; + smtp_password: string; + }; + sendgrid?: { + api_key: string; + }; +} + +export const publicConfig = () => { + const config = getConfig(); + return { + domain: config.general.domain, + siteName: config.general.site_name, + isFederated: config.general.is_federated, + emailLogoUrl: config.general.email_logo_url, + showKofi: config.general.show_kofi, + }; +}; + +// Attempt to load our global config. Will stop the app if the config file +// cannot be read (there's no point trying to continue!) +export const getConfig = (): GathioConfig => { + try { + const config = toml.parse( + fs.readFileSync("./config/config.toml", "utf-8") + ) as GathioConfig; + return config; + } catch { + console.error( + "\x1b[31mConfiguration file not found! Have you renamed './config/config-example.toml' to './config/config.toml'?" + ); + process.exit(1); + } +}; + +export default getConfig; |