mirror of
				https://github.com/NginxProxyManager/nginx-proxy-manager.git
				synced 2025-11-03 17:13:33 +00:00 
			
		
		
		
	Removes the need of a config file and allows db config via environment
This commit is contained in:
		@@ -2,7 +2,10 @@
 | 
			
		||||
 | 
			
		||||
const logger = require('./logger').global;
 | 
			
		||||
 | 
			
		||||
function appStart () {
 | 
			
		||||
async function appStart () {
 | 
			
		||||
	// Create config file db settings if environment variables have been set
 | 
			
		||||
	await createDbConfigFromEnvironment();
 | 
			
		||||
 | 
			
		||||
	const migrate             = require('./migrate');
 | 
			
		||||
	const setup               = require('./setup');
 | 
			
		||||
	const app                 = require('./app');
 | 
			
		||||
@@ -10,6 +13,7 @@ function appStart () {
 | 
			
		||||
	const internalCertificate = require('./internal/certificate');
 | 
			
		||||
	const internalIpRanges    = require('./internal/ip_ranges');
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	return migrate.latest()
 | 
			
		||||
		.then(setup)
 | 
			
		||||
		.then(() => {
 | 
			
		||||
@@ -39,6 +43,87 @@ function appStart () {
 | 
			
		||||
		});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function createDbConfigFromEnvironment(){
 | 
			
		||||
	return new Promise((resolve, reject) => {
 | 
			
		||||
		const envMysqlHost  = process.env.DB_MYSQL_HOST;
 | 
			
		||||
		const envMysqlPort  = process.env.DB_MYSQL_PORT;
 | 
			
		||||
		const envMysqlUser  = process.env.DB_MYSQL_USER;
 | 
			
		||||
		const envMysqlName  = process.env.DB_MYSQL_NAME;
 | 
			
		||||
		const envSqliteFile = process.env.DB_SQLITE_FILE;
 | 
			
		||||
		if ((envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) || envSqliteFile) {
 | 
			
		||||
			const fs       = require('fs');
 | 
			
		||||
			const filename = (process.env.NODE_CONFIG_DIR || './config') + '/' + (process.env.NODE_ENV || 'default') + '.json';
 | 
			
		||||
			let configData = {};
 | 
			
		||||
 | 
			
		||||
			try {
 | 
			
		||||
				configData = require(filename);
 | 
			
		||||
			} catch (err) {
 | 
			
		||||
				// do nothing
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if (configData.database && configData.database.engine && !configData.database.fromEnv) {
 | 
			
		||||
				logger.info('Manual db configuration already exists, skipping config creation from environment variables');
 | 
			
		||||
				resolve();
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if (envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) {
 | 
			
		||||
				const newConfig = {
 | 
			
		||||
					fromEnv:  true,
 | 
			
		||||
					engine:   'mysql',
 | 
			
		||||
					host:     envMysqlHost,
 | 
			
		||||
					port:     envMysqlPort,
 | 
			
		||||
					user:     envMysqlUser,
 | 
			
		||||
					password: process.env.DB_MYSQL_PASSWORD,
 | 
			
		||||
					name:     envMysqlName,
 | 
			
		||||
				};
 | 
			
		||||
 | 
			
		||||
				if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) {
 | 
			
		||||
					// Config is unchanged, skip overwrite
 | 
			
		||||
					resolve();
 | 
			
		||||
					return;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				logger.info('Generating MySQL db configuration from environment variables');
 | 
			
		||||
				configData.database = newConfig;
 | 
			
		||||
 | 
			
		||||
			} else {
 | 
			
		||||
				const newConfig = {
 | 
			
		||||
					fromEnv: true,
 | 
			
		||||
					engine:  'knex-native',
 | 
			
		||||
					knex:    {
 | 
			
		||||
						client:     'sqlite3',
 | 
			
		||||
						connection: {
 | 
			
		||||
							filename: envSqliteFile
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
				};
 | 
			
		||||
				if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) {
 | 
			
		||||
					// Config is unchanged, skip overwrite
 | 
			
		||||
					resolve();
 | 
			
		||||
					return;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				logger.info('Generating Sqlite db configuration from environment variables');
 | 
			
		||||
				configData.database = newConfig;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Write config
 | 
			
		||||
			fs.writeFile(filename, JSON.stringify(configData, null, 2), (err) => {
 | 
			
		||||
				if (err) {
 | 
			
		||||
					logger.error('Could not write db config to config file: ' + filename);
 | 
			
		||||
					reject(err);
 | 
			
		||||
				} else {
 | 
			
		||||
					logger.info('Wrote db configuration to config file: ' + filename);
 | 
			
		||||
					resolve();
 | 
			
		||||
				}
 | 
			
		||||
			});
 | 
			
		||||
		} else {
 | 
			
		||||
			// resolve();
 | 
			
		||||
		}
 | 
			
		||||
	});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
	appStart();
 | 
			
		||||
} catch (err) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user