This commit is contained in:
Aya Morisawa 2017-01-03 05:15:01 +09:00
parent a134358144
commit 240b10ed6e
1 changed files with 16 additions and 19 deletions

View File

@ -27,13 +27,10 @@ require('babel-polyfill');
global.config = require('./config').default(`${__dirname}/../.config/config.yml`); global.config = require('./config').default(`${__dirname}/../.config/config.yml`);
/** enum InitResult {
* Initialize state Success,
*/ Warn,
enum State { Failure
success,
warn,
failed
} }
// Set process title // Set process title
@ -58,25 +55,25 @@ function main(): void {
* Init master proccess * Init master proccess
*/ */
async function master(): Promise<void> { async function master(): Promise<void> {
let state: State; let initResult: InitResult;
try { try {
// initialize app // initialize app
state = await init(); initResult = await init();
} catch (e) { } catch (e) {
console.error(e); console.error(e);
process.exit(1); process.exit(1);
} }
switch (state) { switch (initResult) {
case State.failed: case InitResult.Failure:
Logger.error(chalk.red('Fatal error occurred during initializing :(')); Logger.error(chalk.red('Fatal error occurred during initializing :('));
process.exit(); process.exit();
return; return;
case State.warn: case InitResult.Warn:
Logger.warn(chalk.yellow('Initialized with some problem(s) :|')); Logger.warn(chalk.yellow('Initialized with some problem(s) :|'));
break; break;
case State.success: case InitResult.Success:
Logger.info(chalk.green('Successfully initialized :)')); Logger.info(chalk.green('Successfully initialized :)'));
break; break;
} }
@ -127,7 +124,7 @@ function worker(): void {
/** /**
* Init app * Init app
*/ */
async function init(): Promise<State> { async function init(): Promise<InitResult> {
let warn = false; let warn = false;
Logger.info('Welcome to Misskey!'); Logger.info('Welcome to Misskey!');
@ -142,7 +139,7 @@ async function init(): Promise<State> {
let configLogger = new Logger('Config'); let configLogger = new Logger('Config');
if (!fs.existsSync(`${__dirname}/../.config/config.yml`)) { if (!fs.existsSync(`${__dirname}/../.config/config.yml`)) {
configLogger.error('Configuration not found'); configLogger.error('Configuration not found');
return State.failed; return InitResult.Failure;
} }
configLogger.info('Successfully loaded'); configLogger.info('Successfully loaded');
@ -150,13 +147,13 @@ async function init(): Promise<State> {
if (process.platform === 'linux' && !isRoot() && config.port < 1024) { if (process.platform === 'linux' && !isRoot() && config.port < 1024) {
Logger.error('You need root privileges to listen on port below 1024 on Linux'); Logger.error('You need root privileges to listen on port below 1024 on Linux');
return State.failed; return InitResult.Failure;
} }
// Check if a port is being used // Check if a port is being used
if (await portUsed.check(config.port)) { if (await portUsed.check(config.port)) {
Logger.error(`Port ${config.port} is already used`); Logger.error(`Port ${config.port} is already used`);
return State.failed; return InitResult.Failure;
} }
// Try to connect to MongoDB // Try to connect to MongoDB
@ -167,10 +164,10 @@ async function init(): Promise<State> {
db.close(); db.close();
} catch (e) { } catch (e) {
mongoDBLogger.error(`${e}`); mongoDBLogger.error(`${e}`);
return State.failed; return InitResult.Failure;
} }
return warn ? State.warn : State.success; return warn ? InitResult.Warn : InitResult.Success;
} }
/** /**