diff --git a/package.json b/package.json index 8be2315a4..a3f67772f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zip3", - "version": "3.2.2", + "version": "3.2.3", "scripts": { "prepare": "husky install", "dev": "NODE_ENV=development node server", diff --git a/server/index.js b/server/index.js index 0ac4ff9e7..ad85ee01d 100644 --- a/server/index.js +++ b/server/index.js @@ -34,8 +34,7 @@ function shouldUseYarn() { (async () => { try { - const config = readConfig(); - await validateConfig(config); + const config = await validateConfig(readConfig()); const data = await prismaRun(config.core.database_url, ['migrate', 'status'], true); if (data.includes('Following migration have not yet been applied:')) { diff --git a/server/validateConfig.js b/server/validateConfig.js index 2ce5e2dcb..a21163045 100644 --- a/server/validateConfig.js +++ b/server/validateConfig.js @@ -1,45 +1,29 @@ const Logger = require('../src/lib/logger'); +const yup = require('yup'); -function dot(str, obj) { - return str.split('.').reduce((a,b) => a[b], obj); -} -const path = (path, type) => ({ path, type }); +const validator = yup.object({ + core: yup.object({ + secure: yup.bool().default(false), + secret: yup.string().min(8).required(), + host: yup.string().default('0.0.0.0'), + port: yup.number().default(3000), + database_url: yup.string().required(), + }).required(), + uploader: yup.object({ + route: yup.string().required(), + length: yup.number().default(6), + directory: yup.string().required(), + admin_limit: yup.number().default(104900000), + user_limit: yup.number().default(104900000), + disabled_extensions: yup.array().default([]), + }).required(), +}); module.exports = async config => { - const paths = [ - path('core.secure', 'boolean'), - path('core.secret', 'string'), - path('core.host', 'string'), - path('core.port', 'number'), - path('core.database_url', 'string'), - path('uploader.route', 'string'), - path('uploader.length', 'number'), - path('uploader.directory', 'string'), - path('uploader.admin_limit', 'number'), - path('uploader.user_limit', 'number'), - path('uploader.disabled_extentions', 'object'), - ]; - - let errors = 0; - - for (let i = 0, L = paths.length; i !== L; ++i) { - const path = paths[i]; - const value = dot(path.path, config); - if (value === undefined) { - Logger.get('config').error(`there was no ${path.path} in config which was required`); - ++errors; - } - - const type = typeof value; - if (value !== undefined && type !== path.type) { - Logger.get('config').error(`expected ${path.type} on ${path.path}, but got ${type}`); - ++errors; - } - } - - if (errors !== 0) { - Logger.get('config').error(`exiting due to ${errors} errors`); - process.exit(1); - } + try { + return await validator.validate(config, { abortEarly: false }); + } catch (e) { + throw `${e.errors.length} errors occured\n${e.errors.map(x => '\t' + x).join('\n')}`; + } }; \ No newline at end of file diff --git a/src/lib/logger.js b/src/lib/logger.js index 6b1506dc5..2f1518f86 100644 --- a/src/lib/logger.js +++ b/src/lib/logger.js @@ -19,7 +19,7 @@ class Logger { } error(error) { - console.log(this.formatMessage('ERROR', this.name, error.toString())); + console.log(this.formatMessage('ERROR', this.name, error.stack ?? error)); } formatMessage(level, name, message) { diff --git a/src/lib/readConfig.js b/src/lib/readConfig.js index a47a83993..c42a43331 100644 --- a/src/lib/readConfig.js +++ b/src/lib/readConfig.js @@ -2,7 +2,7 @@ const { existsSync, readFileSync } = require('fs'); const { join } = require('path'); const Logger = require('./logger'); -const e = (val, type, fn, required = true) => ({ val, type, fn, required }); +const e = (val, type, fn) => ({ val, type, fn }); const envValues = [ e('SECURE', 'boolean', (c, v) => c.core.secure = v), @@ -15,7 +15,7 @@ const envValues = [ e('UPLOADER_DIRECTORY', 'string', (c, v) => c.uploader.directory = v), e('UPLOADER_ADMIN_LIMIT', 'number', (c, v) => c.uploader.admin_limit = v), e('UPLOADER_USER_LIMIT', 'number', (c, v) => c.uploader.user_limit = v), - e('UPLOADER_DISABLED_EXTS', 'array', (c, v) => v ? c.uploader.disabled_extentions = v : c.uploader.disabled_extentions = [], false), + e('UPLOADER_DISABLED_EXTS', 'array', (c, v) => v ? c.uploader.disabled_extentions = v : c.uploader.disabled_extentions = []), ]; module.exports = () => { @@ -54,21 +54,16 @@ function tryReadEnv() { const envValue = envValues[i]; let value = process.env[envValue.val]; - if (envValue.required && !value) { - Logger.get('config').error(`there is no config file or required environment variables (${envValue.val})... exiting...`); - - process.exit(1); - } - - envValues[i].fn(config, value); - if (envValue.required) { + if (!value) { + envValues[i].fn(config, undefined); + } else { + envValues[i].fn(config, value); if (envValue.type === 'number') value = parseToNumber(value); else if (envValue.type === 'boolean') value = parseToBoolean(value); else if (envValue.type === 'array') value = parseToArray(value); envValues[i].fn(config, value); } } - return config; }