mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 15:11:31 +08:00 
			
		
		
		
	improved error handling of wrong port configuration, #3177
This commit is contained in:
		
							parent
							
								
									20ff5627d8
								
							
						
					
					
						commit
						6d4ef4ee3d
					
				@ -8,14 +8,12 @@ const resourceDir = require('./resource_dir');
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const configSampleFilePath = path.resolve(resourceDir.RESOURCE_DIR, "config-sample.ini");
 | 
					const configSampleFilePath = path.resolve(resourceDir.RESOURCE_DIR, "config-sample.ini");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const configFilePath = dataDir.TRILIUM_DATA_DIR + '/config.ini';
 | 
					if (!fs.existsSync(dataDir.CONFIG_INI_PATH)) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
if (!fs.existsSync(configFilePath)) {
 | 
					 | 
				
			||||||
    const configSample = fs.readFileSync(configSampleFilePath).toString('utf8');
 | 
					    const configSample = fs.readFileSync(configSampleFilePath).toString('utf8');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fs.writeFileSync(configFilePath, configSample);
 | 
					    fs.writeFileSync(dataDir.CONFIG_INI_PATH, configSample);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const config = ini.parse(fs.readFileSync(configFilePath, 'utf-8'));
 | 
					const config = ini.parse(fs.readFileSync(dataDir.CONFIG_INI_PATH, 'utf-8'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = config;
 | 
					module.exports = config;
 | 
				
			||||||
 | 
				
			|||||||
@ -64,11 +64,13 @@ const DOCUMENT_PATH = TRILIUM_DATA_DIR + path.sep + "document.db";
 | 
				
			|||||||
const BACKUP_DIR = TRILIUM_DATA_DIR + path.sep + "backup";
 | 
					const BACKUP_DIR = TRILIUM_DATA_DIR + path.sep + "backup";
 | 
				
			||||||
const LOG_DIR = TRILIUM_DATA_DIR + path.sep + "log";
 | 
					const LOG_DIR = TRILIUM_DATA_DIR + path.sep + "log";
 | 
				
			||||||
const ANONYMIZED_DB_DIR = TRILIUM_DATA_DIR + path.sep + "anonymized-db";
 | 
					const ANONYMIZED_DB_DIR = TRILIUM_DATA_DIR + path.sep + "anonymized-db";
 | 
				
			||||||
 | 
					const CONFIG_INI_PATH = TRILIUM_DATA_DIR + '/config.ini';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
    TRILIUM_DATA_DIR,
 | 
					    TRILIUM_DATA_DIR,
 | 
				
			||||||
    DOCUMENT_PATH,
 | 
					    DOCUMENT_PATH,
 | 
				
			||||||
    BACKUP_DIR,
 | 
					    BACKUP_DIR,
 | 
				
			||||||
    LOG_DIR,
 | 
					    LOG_DIR,
 | 
				
			||||||
    ANONYMIZED_DB_DIR
 | 
					    ANONYMIZED_DB_DIR,
 | 
				
			||||||
 | 
					    CONFIG_INI_PATH
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -1,15 +1,27 @@
 | 
				
			|||||||
const config = require('./config');
 | 
					const config = require('./config');
 | 
				
			||||||
const utils = require('./utils');
 | 
					const utils = require('./utils');
 | 
				
			||||||
const env = require('./env');
 | 
					const env = require('./env');
 | 
				
			||||||
 | 
					const dataDir = require('./data_dir');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function parseAndValidate(portStr, source) {
 | 
				
			||||||
 | 
					    const portNum = parseInt(portStr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!portNum || portNum < 0 || portNum >= 65536) {
 | 
				
			||||||
 | 
					        console.log(`FATAL ERROR: Invalid port value "${portStr}" from ${source}, should be a number between 0 and 65536.`);
 | 
				
			||||||
 | 
					        process.exit(-1);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return portNum;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let port;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (process.env.TRILIUM_PORT) {
 | 
					if (process.env.TRILIUM_PORT) {
 | 
				
			||||||
    module.exports = parseInt(process.env.TRILIUM_PORT);
 | 
					    port = parseAndValidate(process.env.TRILIUM_PORT, "environment variable TRILIUM_PORT");
 | 
				
			||||||
    return;
 | 
					} else if (utils.isElectron()) {
 | 
				
			||||||
 | 
					    port = env.isDev() ? 37740 : 37840;
 | 
				
			||||||
 | 
					} else {
 | 
				
			||||||
 | 
					    port = parseAndValidate(config['Network']['port'] || '3000', "Network.port in " + dataDir.CONFIG_INI_PATH);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (utils.isElectron()) {
 | 
					module.exports = port;
 | 
				
			||||||
    module.exports = env.isDev() ? 37740 : 37840;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
else {
 | 
					 | 
				
			||||||
    module.exports = config['Network']['port'] || '3000';
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user