0
0
Fork 0

Tweak the config helper to pull from the environment variables instead of a file

This commit is contained in:
Oliver-Akins 2023-08-26 20:38:32 -06:00
parent d815c8d557
commit 8ed5054d25

View file

@ -1,39 +1,36 @@
import { configSchema } from "$/schemas/config"; import { configSchema } from "$/schemas/config";
import type { config } from "$/types/config"; import type { config } from "$/types/config";
import { isDev } from "$/main";
import toml from "@iarna/toml";
import fs from "fs";
/** /**
* Attempts to load the config from disk and validate it's structure. * Attempts to load the config from disk and validate it's structure.
*/ */
export function loadConfig() { export function loadConfig(): config {
let file = null; const data: config = {
database: {
if (isDev) { uri: `./data.json`,
try { },
file = fs.readFileSync(`./config.dev.toml`, `utf-8`); game: {
} catch (_) { files: {},
console.error(`Couldn't find development config, checking production`); penalties: {
}; guess: 1,
solve: 2,
duplicate: 0,
},
max_incorrect: 6,
},
server: {
port: 6969,
},
}; };
if (!file) { for (var envvar in process.env) {
try { let value = process.env[envvar];
file = fs.readFileSync(`./config.toml`, `utf-8`); if (envvar.startsWith(`FILE_`)) {
} catch (_) { data.game.files[envvar.slice(5).toLowerCase()] = value;
console.error(`Couldn't find production config. Fill out the config and run the server again`);
process.exit(1);
}; };
}; };
console.log(data.game.files)
try {
var data = toml.parse(file);
} catch (_) {
console.error(`Invalid TOML file, stopping server`);
process.exit(1);
};
let { error, value } = configSchema.validate(data, { abortEarly: false }); let { error, value } = configSchema.validate(data, { abortEarly: false });
if (error) { if (error) {