Tweak the config helper to pull from the environment variables instead of a file
This commit is contained in:
parent
d815c8d557
commit
8ed5054d25
1 changed files with 23 additions and 26 deletions
|
|
@ -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: {
|
||||||
|
uri: `./data.json`,
|
||||||
|
},
|
||||||
|
game: {
|
||||||
|
files: {},
|
||||||
|
penalties: {
|
||||||
|
guess: 1,
|
||||||
|
solve: 2,
|
||||||
|
duplicate: 0,
|
||||||
|
},
|
||||||
|
max_incorrect: 6,
|
||||||
|
},
|
||||||
|
server: {
|
||||||
|
port: 6969,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
if (isDev) {
|
for (var envvar in process.env) {
|
||||||
try {
|
let value = process.env[envvar];
|
||||||
file = fs.readFileSync(`./config.dev.toml`, `utf-8`);
|
if (envvar.startsWith(`FILE_`)) {
|
||||||
} catch (_) {
|
data.game.files[envvar.slice(5).toLowerCase()] = value;
|
||||||
console.error(`Couldn't find development config, checking production`);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
console.log(data.game.files)
|
||||||
if (!file) {
|
|
||||||
try {
|
|
||||||
file = fs.readFileSync(`./config.toml`, `utf-8`);
|
|
||||||
} catch (_) {
|
|
||||||
console.error(`Couldn't find production config. Fill out the config and run the server again`);
|
|
||||||
process.exit(1);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
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) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue