Added configuration validation. Minor config change.

This commit is contained in:
Tom
2024-12-04 22:28:33 +00:00
parent 79b27b8e32
commit 1b91e330c1
6 changed files with 174 additions and 74 deletions

110
config/config.schema.js Normal file
View File

@@ -0,0 +1,110 @@
const schema = {
type: 'object',
required: [],
properties: {
plex: {
type: 'object',
required: ['url', 'token'],
properties: {
url: {
type: 'string'
},
token: {
type: 'string'
},
filters: {
type: 'array',
minItems: 0,
items: {
type: 'object',
properties: {
library: {
type: 'array',
items: {
type: 'string'
},
minItems: 0,
},
ip: {
type: 'array',
items: {
type: 'string'
},
minItems: 0,
},
deviceId: {
type: 'array',
items: {
type: 'string'
},
minItems: 0,
},
platform: {
type: 'array',
items: {
type: 'string'
},
minItems: 0,
},
product: {
type: 'array',
items: {
type: 'string'
},
minItems: 0,
},
}
}
}
}
},
scrobble: {
type: 'object',
properties: {
minimum: {
type: 'object',
properties: {
percent: {
type: 'number'
},
duration: {
type: 'number'
}
}
}
}
},
spotify: {
type: 'object',
required: ['client_id', 'client_secret', 'redirect_uri'],
properties: {
client_id: {
type: 'string'
},
client_secret: {
type: 'string'
},
redirect_uri: {
type: 'string'
}
}
},
web: {
type: 'object',
required: [],
properties: {
host: {
type: 'string'
},
port: {
type: 'number'
}
}
}
}
}
module.exports = schema;

View File

@@ -1,26 +1,19 @@
const config = require('config');
const Ajv = require("ajv");
const fs = require("fs");
const logger = require("../services/logging");
const yaml = require("js-yaml");
const configuration = {
const configurationBase = {
plex: {
url: null,
token: null
token: null,
filters: [] // { library, ip, deviceId, platform, product }
},
scrobble: {
minimum: {
percent: null,
duration: null
},
plex: {
delay: null,
filters: []
/* A filter will have the following properties:
library: [""],
ip: [""],
deviceId: [""],
platform: [""],
product: [""]
*/
},
},
spotify: {
client_id: null,
@@ -33,27 +26,18 @@ const configuration = {
}
};
if (config.has("plex.url"))
configuration.plex.url = config.get("plex.url");
if (config.has("plex.token"))
configuration.plex.token = config.get("plex.token");
const configurationFile = yaml.load(fs.readFileSync('config/config.yml'), yaml.JSON_SCHEMA);
const configuration = { ...configurationBase, ...configurationFile }
if (config.has("scrobble.plex.delay"))
configuration.scrobble.plex.delay = config.get("scrobble.plex.delay");
if (config.has("scrobble.plex.filters"))
configuration.scrobble.plex.filters = config.get("scrobble.plex.filters");
const ajv = new Ajv({ allErrors: true });
const schema = require("./config.schema");
const { exit } = require("process");
const validation = ajv.compile(schema);
const valid = validation(configurationFile);
if (config.has("scrobble.minimum.duration"))
configuration.scrobble.minimum.duration = config.get("scrobble.minimum.duration");
if (config.has("scrobble.minimum.percent"))
configuration.scrobble.minimum.percent = config.get("scrobble.minimum.percent");
if (config.has("spotify"))
configuration.spotify = config.get("spotify");
if (config.has("web.host"))
configuration.web.host = config.get("web.host");
if (config.has("web.port"))
configuration.web.port = config.get("web.port");
if (!valid) {
logger.error("Configuration is invalid. " + validation.errors.map(e => e.message).join(". ") + ".");
(async () => { await new Promise(resolve => setTimeout(resolve, 1000)); exit(1); })();
}
module.exports = configuration;