53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
const Ajv = require("ajv");
|
|
const fs = require("fs");
|
|
const docker = require("../utils/docker");
|
|
const logger = require("../services/logging");
|
|
const yaml = require("js-yaml");
|
|
const { exit } = require("process");
|
|
|
|
const configurationBase = {
|
|
plex: {
|
|
name: null,
|
|
url: null,
|
|
token: null,
|
|
filters: [], // { library, ip, deviceId, platform, product }
|
|
scrobblers: []
|
|
},
|
|
scrobble: {
|
|
minimum: {
|
|
percent: null,
|
|
duration: null
|
|
},
|
|
},
|
|
spotify: {
|
|
name: null,
|
|
client_id: null,
|
|
client_secret: null,
|
|
redirect_uri: null,
|
|
scrobblers: []
|
|
},
|
|
web: {
|
|
host: null,
|
|
port: null
|
|
}
|
|
};
|
|
|
|
const isDocker = docker.isRunningOnDocker();
|
|
const configPath = isDocker ? `${process.env.CONFIG_DIR}/config.yml` : "config.yml";
|
|
const configurationFile = yaml.load(fs.readFileSync(configPath), yaml.JSON_SCHEMA);
|
|
|
|
const ajv = new Ajv({ allErrors: true });
|
|
const schema = require("./config.schema");
|
|
const validation = ajv.compile(schema);
|
|
const valid = validation(configurationFile);
|
|
|
|
if (!valid) {
|
|
logger.error("Configuration is invalid. " + validation.errors.map(e => e.message).join(". ") + ".");
|
|
(async () => { await new Promise(resolve => setTimeout(resolve, 1000)); exit(1); })();
|
|
}
|
|
|
|
const configuration = { ...configurationBase, ...configurationFile }
|
|
|
|
configuration.web.port ||= process.env.WEB_PORT;
|
|
|
|
module.exports = configuration; |