Use accesses

This commit is contained in:
syuilo 2017-04-05 09:58:29 +09:00
parent 45034d6426
commit 0bba9b1e6c
3 changed files with 37 additions and 29 deletions

View File

@ -56,6 +56,10 @@ interface Source {
secretKey: string;
};
accesslog?: string;
accesses?: {
enable: boolean;
port: number;
};
twitter?: {
consumer_key: string;
consumer_secret: string;

View File

@ -11,26 +11,22 @@ import * as fs from 'fs';
import * as os from 'os';
import * as cluster from 'cluster';
import * as debug from 'debug';
import Logger from './utils/logger';
import * as chalk from 'chalk';
//import portUsed = require('tcp-port-used');
import isRoot = require('is-root');
import { master } from 'accesses';
import Logger from './utils/logger';
import ProgressBar from './utils/cli/progressbar';
import EnvironmentInfo from './utils/environmentInfo';
import MachineInfo from './utils/machineInfo';
import DependencyInfo from './utils/dependencyInfo';
import { path as configPath } from './config';
import { Config, path as configPath } from './config';
import loadConfig from './config';
const clusterLog = debug('misskey:cluster');
enum InitResult {
Success,
Warn,
Failure
}
process.title = 'Misskey';
// Start app
@ -51,27 +47,26 @@ function main() {
* Init master process
*/
async function masterMain() {
let initResult: InitResult;
let config: Config;
try {
// initialize app
initResult = await init();
config = await init();
} catch (e) {
console.error(e);
process.exit(1);
}
switch (initResult) {
case InitResult.Success:
Logger.info(chalk.green('Successfully initialized :)'));
break;
case InitResult.Warn:
Logger.warn(chalk.yellow('Initialized with some problem(s) :|'));
break;
case InitResult.Failure:
Logger.error(chalk.red('Fatal error occurred during initializing :('));
process.exit();
return;
if (config == null) {
Logger.error(chalk.red('Fatal error occurred during initializing :('));
process.exit();
}
Logger.info(chalk.green('Successfully initialized :)'));
// Init accesses
if (config.accesses && config.accesses.enable) {
master();
}
spawnWorkers(() => {
@ -90,9 +85,7 @@ function workerMain() {
/**
* Init app
*/
async function init() {
let warn = false;
async function init(): Promise<Config> {
Logger.info('Welcome to Misskey!');
Logger.info(chalk.bold('Misskey <aoi>'));
Logger.info('Initializing...');
@ -104,7 +97,7 @@ async function init() {
let configLogger = new Logger('Config');
if (!fs.existsSync(configPath)) {
configLogger.error('Configuration not found');
return InitResult.Failure;
return null;
}
const config = loadConfig();
@ -114,14 +107,14 @@ async function init() {
if (process.platform === 'linux' && !isRoot() && config.port < 1024) {
Logger.error('You need root privileges to listen on port below 1024 on Linux');
return InitResult.Failure;
return null;
}
// Check if a port is being used
/* https://github.com/stdarg/tcp-port-used/issues/3
if (await portUsed.check(config.port)) {
Logger.error(`Port ${config.port} is already used`);
return InitResult.Failure;
return null;
}
*/
@ -133,10 +126,10 @@ async function init() {
db.close();
} catch (e) {
mongoDBLogger.error(e);
return InitResult.Failure;
return null;
}
return warn ? InitResult.Warn : InitResult.Success;
return config;
}
function spawnWorkers(onComplete: any) {

View File

@ -8,6 +8,7 @@ import * as https from 'https';
import * as cluster from 'cluster';
import * as express from 'express';
import * as morgan from 'morgan';
import Accesses from 'accesses';
import vhost = require('vhost');
import config from './conf';
@ -20,6 +21,16 @@ app.disable('x-powered-by');
app.set('trust proxy', 'loopback');
// Log
if (config.accesses && config.accesses.enable) {
const accesses = new Accesses({
appName: 'Misskey',
port: config.accesses.port,
hashIp: true
});
app.use(accesses.express);
}
app.use(morgan(process.env.NODE_ENV == 'production' ? 'combined' : 'dev', {
// create a write stream (in append mode)
stream: config.accesslog ? fs.createWriteStream(config.accesslog) : null