<commit_before>/*!
 *  Chinachu WebUI Server Service (chinachu-wui)
 *
 *  Copyright (c) 2012 <NAME> and Chinachu Project Contributors
 *  http://chinachu.akkar.in/
**/

var CONFIG_FILE         = __dirname + '/config.json';
var RULES_FILE          = __dirname + '/rules.json';
var RESERVES_DATA_FILE  = __dirname + '/data/reserves.json';
var SCHEDULE_DATA_FILE  = __dirname + '/data/schedule.json';
var RECORDING_DATA_FILE = __dirname + '/data/recording.json';
var RECORDED_DATA_FILE  = __dirname + '/data/recorded.json';
var OPERATOR_LOG_FILE   = __dirname + '/log/operator';
var WUI_LOG_FILE        = __dirname + '/log/wui';
var SCHEDULER_LOG_FILE  = __dirname + '/log/scheduler';

// 標準モジュールのロード
var path          = require('path');
var fs            = require('fs');
var util          = require('util');
var child_process = require('child_process');
var url           = require('url');
var querystring   = require('querystring');
var vm            = require('vm');
var os            = require('os');

// ディレクトリチェック
if (!fs.existsSync('./data/') || !fs.existsSync('./log/') || !fs.existsSync('./web/')) {
	util.error('必要なディレクトリが存在しないか、カレントワーキングディレクトリが不正です。');
	process.exit(1);
}

// 終了処理
process.on('SIGQUIT', function() {
	setTimeout(function() {
		process.exit(0);
	}, 0);
});

// 追加モジュールのロード
var auth     = require('http-auth');
var socketio = require('socket.io');
var chinachu = require('chinachu-common');

// 設定の読み込み
var config = require(CONFIG_FILE);

// https or http
if (config.wuiTlsKeyPath && config.wuiTlsCertPath) {
	var https = require('spdy');
	
	var tlsOption = {
		key : fs.readFileSync(config.wuiTlsKeyPath),
		cert: fs.readFileSync(config.wuiTlsCertPath)
	};
} else {
	var http = require('http');
}

// ファイル更新監視: ./data/rules.json
var rules = [];
chinachu.jsonWatcher(
	RULES_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		rules = data;
		if (io) io.sockets.emit('rules', rules);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// ファイル更新監視: ./data/schedule.json
var schedule = [];
chinachu.jsonWatcher(
	SCHEDULE_DATA_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		schedule = data;
		if (io) io.sockets.emit('schedule', schedule);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// ファイル更新監視: ./data/reserves.json
var reserves = [];
chinachu.jsonWatcher(
	RESERVES_DATA_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		reserves = data;
		if (io) io.sockets.emit('reserves', reserves);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// ファイル更新監視: ./data/recording.json
var recording = [];
chinachu.jsonWatcher(
	RECORDING_DATA_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		recording = data;
		if (io) io.sockets.emit('recording', recording);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// ファイル更新監視: ./data/recorded.json
var recorded = [];
chinachu.jsonWatcher(
	RECORDED_DATA_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		recorded = data;
		if (io) io.sockets.emit('recorded', recorded);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// BASIC認証
if (config.wuiUsers && (config.wuiUsers.length > 0)) {
	var basic = auth({
		authRealm: 'Authentication.',
		authList : config.wuiUsers
	});
}

var status = {
	connectedCount: 0,
	feature: {
		previewer   : !!config.wuiPreviewer,
		streamer    : !!config.wuiStreamer,
		filer       : !!config.wuiFiler,
		configurator: !!config.wuiConfigurator
	},
	system: {
		core: os.cpus().length
	}
};

//
// http server
//
if (http)  { var app = http.createServer(httpServer); }
if (https) { var app = https.createServer(tlsOption, httpServer); }
app.listen(config.wuiPort, config.wuiHost || '::');

function httpServer(req, res) {
	if (req.method === 'GET') {
		
		httpServerMain(req, res, url.parse(req.url, true).query);
		
	} else if (req.method === 'POST') {
		
		var postBody = '';
		
		req.on('data', function(chunk) {
			postBody += chunk.toString();
		});
		
		req.on('end', function() {
			httpServerMain(req, res, querystring.parse(postBody));
		});
		
	}
}

function httpServerMain(req, res, query) {
	// http request logging
	var log = function(statusCode) {
		util.log([
			/*statusCode*/ statusCode,
			/*method+url*/ req.method + ':' + req.url,
			/*remoteAddr*/ req.headers['x-forwarded-for'] || req.client.remoteAddress,<commit_msg>Revert "Revert "WUI: app.listenの第二引数に意図した値が設定できない不具合を修正""<commit_after>/*!
 *  Chinachu WebUI Server Service (chinachu-wui)
 *
 *  Copyright (c) 2012 <NAME> and Chinachu Project Contributors
 *  http://chinachu.akkar.in/
**/

var CONFIG_FILE         = __dirname + '/config.json';
var RULES_FILE          = __dirname + '/rules.json';
var RESERVES_DATA_FILE  = __dirname + '/data/reserves.json';
var SCHEDULE_DATA_FILE  = __dirname + '/data/schedule.json';
var RECORDING_DATA_FILE = __dirname + '/data/recording.json';
var RECORDED_DATA_FILE  = __dirname + '/data/recorded.json';
var OPERATOR_LOG_FILE   = __dirname + '/log/operator';
var WUI_LOG_FILE        = __dirname + '/log/wui';
var SCHEDULER_LOG_FILE  = __dirname + '/log/scheduler';

// 標準モジュールのロード
var path          = require('path');
var fs            = require('fs');
var util          = require('util');
var child_process = require('child_process');
var url           = require('url');
var querystring   = require('querystring');
var vm            = require('vm');
var os            = require('os');

// ディレクトリチェック
if (!fs.existsSync('./data/') || !fs.existsSync('./log/') || !fs.existsSync('./web/')) {
	util.error('必要なディレクトリが存在しないか、カレントワーキングディレクトリが不正です。');
	process.exit(1);
}

// 終了処理
process.on('SIGQUIT', function() {
	setTimeout(function() {
		process.exit(0);
	}, 0);
});

// 追加モジュールのロード
var auth     = require('http-auth');
var socketio = require('socket.io');
var chinachu = require('chinachu-common');

// 設定の読み込み
var config = require(CONFIG_FILE);

// https or http
if (config.wuiTlsKeyPath && config.wuiTlsCertPath) {
	var https = require('spdy');
	
	var tlsOption = {
		key : fs.readFileSync(config.wuiTlsKeyPath),
		cert: fs.readFileSync(config.wuiTlsCertPath)
	};
} else {
	var http = require('http');
}

// ファイル更新監視: ./data/rules.json
var rules = [];
chinachu.jsonWatcher(
	RULES_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		rules = data;
		if (io) io.sockets.emit('rules', rules);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// ファイル更新監視: ./data/schedule.json
var schedule = [];
chinachu.jsonWatcher(
	SCHEDULE_DATA_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		schedule = data;
		if (io) io.sockets.emit('schedule', schedule);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// ファイル更新監視: ./data/reserves.json
var reserves = [];
chinachu.jsonWatcher(
	RESERVES_DATA_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		reserves = data;
		if (io) io.sockets.emit('reserves', reserves);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// ファイル更新監視: ./data/recording.json
var recording = [];
chinachu.jsonWatcher(
	RECORDING_DATA_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		recording = data;
		if (io) io.sockets.emit('recording', recording);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// ファイル更新監視: ./data/recorded.json
var recorded = [];
chinachu.jsonWatcher(
	RECORDED_DATA_FILE
	,
	function _onUpdated(err, data, mes) {
		if (err) {
			util.error(err);
			return;
		}
		
		recorded = data;
		if (io) io.sockets.emit('recorded', recorded);
		util.log(mes);
	}
	,
	{ create: [], now: true }
);

// BASIC認証
if (config.wuiUsers && (config.wuiUsers.length > 0)) {
	var basic = auth({
		authRealm: 'Authentication.',
		authList : config.wuiUsers
	});
}

var status = {
	connectedCount: 0,
	feature: {
		previewer   : !!config.wuiPreviewer,
		streamer    : !!config.wuiStreamer,
		filer       : !!config.wuiFiler,
		configurator: !!config.wuiConfigurator
	},
	system: {
		core: os.cpus().length
	}
};

//
// http server
//
if (http)  { var app = http.createServer(httpServer); }
if (https) { var app = https.createServer(tlsOption, httpServer); }

app.listen(config.wuiPort, (typeof config.wuiHost === 'undefined') ? '::' : config.wuiHost);

function httpServer(req, res) {
	if (req.method === 'GET') {
		
		httpServerMain(req, res, url.parse(req.url, true).query);
		
	} else if (req.method === 'POST') {
		
		var postBody = '';
		
		req.on('data', function(chunk) {
			postBody += chunk.toString();
		});
		
		req.on('end', function() {
			httpServerMain(req, res, querystring.parse(postBody));
		});
		
	}
}

function httpServerMain(req, res, query) {
	// http request logging
	var log = function(statusCode) {
		util.log([
			/*statusCode*/ statusCode,
			/*method+url*/ req.method + ':' + req.url,
			/*remoteAddr*/ req.headers['x-forwarded-for'] || req.client.remoteAddress,
{'fraction_non_alphanumeric': 0.1742857142857143, 'fraction_numerical': 0.001978021978021978, 'mean_word_length': 3.400870406189555, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6554357', 'n_tokens_mistral': 3835, 'n_tokens_neox': 3584, 'n_words': 663}