From 33b6c232c9dfc7043076f7e0de37e040d1da5921 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 17 Jan 2017 08:19:34 +0900 Subject: [PATCH] #31 --- gulpfile.ts | 2 +- src/conf.ts | 3 +++ src/config.ts | 7 +++---- src/index.ts | 14 ++++++++------ src/server.ts | 2 +- 5 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 src/conf.ts diff --git a/gulpfile.ts b/gulpfile.ts index b08a51780..f3645bd9b 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -38,7 +38,7 @@ if (!fs.existsSync('./.config/config.yml')) { (global as any).MISSKEY_CONFIG_PATH = '.config/config.yml'; import { IConfig } from './src/config'; -const config = eval(require('typescript').transpile(require('fs').readFileSync('./src/config.ts').toString())) as IConfig; +const config = eval(require('typescript').transpile(require('fs').readFileSync('./src/config.ts').toString()))() as IConfig; const tsProject = ts.createProject('tsconfig.json'); diff --git a/src/conf.ts b/src/conf.ts new file mode 100644 index 000000000..b04a4c859 --- /dev/null +++ b/src/conf.ts @@ -0,0 +1,3 @@ +import load from './config'; + +export default load(); diff --git a/src/config.ts b/src/config.ts index 916aaab58..342e2db78 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,6 +6,8 @@ import * as fs from 'fs'; import * as yaml from 'js-yaml'; import * as isUrl from 'is-url'; +export const path = (global as any).MISSKEY_CONFIG_PATH ? (global as any).MISSKEY_CONFIG_PATH : `${__dirname}/../.config/config.yml`; + /** * ユーザーが設定する必要のある情報 */ @@ -62,10 +64,7 @@ interface Mixin { export type IConfig = ISource & Mixin; -export default load(); - -function load() { - const path = (global as any).MISSKEY_CONFIG_PATH ? (global as any).MISSKEY_CONFIG_PATH : `${__dirname}/../.config/config.yml`; +export default function load() { const config = yaml.safeLoad(fs.readFileSync(path, 'utf8')) as ISource; const mixin: Mixin = {} as Mixin; diff --git a/src/index.ts b/src/index.ts index 48ca660be..b025463e3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,12 +21,13 @@ import EnvironmentInfo from './utils/environmentInfo'; import MachineInfo from './utils/machineInfo'; import DependencyInfo from './utils/dependencyInfo'; +import { path as configPath } from './config'; +import loadConfig from './config'; + // Init babel require('babel-core/register'); require('babel-polyfill'); -global.config = require('./config').default(`${__dirname}/../.config/config.yml`); - enum InitResult { Success, Warn, @@ -76,6 +77,8 @@ async function masterMain(): Promise { return; } + const config = loadConfig(); + spawnWorkers(() => { Logger.info(chalk.bold.green(`Now listening on port ${config.port}`)); @@ -103,9 +106,6 @@ async function masterMain(): Promise { * Init worker proccess */ function workerMain(): void { - // Register config - global.config = config; - // Init mongo initdb().then(db => { global.db = db; @@ -134,11 +134,13 @@ async function init(): Promise { new DependencyInfo().showAll(); let configLogger = new Logger('Config'); - if (!fs.existsSync(`${__dirname}/../.config/config.yml`)) { + if (!fs.existsSync(configPath)) { configLogger.error('Configuration not found'); return InitResult.Failure; } + const config = loadConfig(); + configLogger.info('Successfully loaded'); configLogger.info(`maintainer: ${config.maintainer}`); diff --git a/src/server.ts b/src/server.ts index 1a7d8e59b..63a9b18b6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -8,7 +8,7 @@ import * as https from 'https'; import * as express from 'express'; import vhost = require('vhost'); -import config from './config'; +import config from './conf'; /** * Init app