<commit_before>import * as express from "express"
import * as bodyParser from "body-parser"

import * as aiRepo from './repo/artistImageRepo'
import * as fiRepo from './repo/flatImageRepo'
import {PixivRepo} from './repo/model'

import * as Proto from '../common/proto'

import * as yargs from 'yargs'
import * as log4js from 'log4js'

import {IServerConfig, RepositoryType} from './proto'

let logger = log4js.getLogger('Startup');

let defaults :IServerConfig = {
	path: 'pixivRepository',
	repoType: RepositoryType.ArtistBreakdown,
	port: 50415,
	verboseLogging: false
}

export function initServer(config:IServerConfig) {
	let verboseLogging = (config.verboseLogging !== undefined)? config.verboseLogging : defaults.verboseLogging;
	if (verboseLogging) {
		log4js.setGlobalLogLevel(log4js.levels.ALL);
		logger.warn('Setting to verbose mode');
	} else {
		log4js.setGlobalLogLevel(log4js.levels.INFO);
	}

	let path = config.path || defaults.path;
	logger.info(`Setting repo to path [${path}]`);

	function decideRepo() : PixivRepo {
		switch (config.repoType) {
			case RepositoryType.ArtistBreakdown:
				logger.info('Using [root/artist/images*] repo format');
				return new aiRepo.ArtistImageRepo(path);
			case RepositoryType.LooseImages:
				logger.info('Using [root/images*] repo format');
				return new fiRepo.ImageRepo(path);
			default:
				logger.warn('No repository format specified, using default');
				logger.info('Using [root/artist/images*] repo format');
				return new aiRepo.ArtistImageRepo(path);
		}
	}
	// pixiv assistant repo
	let pas = decideRepo();

	let appLogger = log4js.getLogger('App');
	let app = express();

	app.use(bodyParser.urlencoded({ extended: false }));
	app.use(bodyParser.json());

	app.all('/ping', (req, res) => {
		appLogger.debug('Message Received | Ping');
		res.json({success: true, data:true});
	});

	app.all('/supports/:action', (req, res) => {
		let action: string = req.params.action;
		appLogger.debug('Message Received | Supports action [', action, ']');
		res.json({success: true, data: pas.supports(action)});
	});

	app.post('/:action', (req, res) => {
		let action: string = req.params.action;
		let message: any = req.body;
		appLogger.debug('Message Received | Perform action [', action, ']');
		
		pas.dispatch(action, message)<commit_msg>fixed a bug in server dispatching<commit_after>import * as express from "express"
import * as bodyParser from "body-parser"

import * as aiRepo from './repo/artistImageRepo'
import * as fiRepo from './repo/flatImageRepo'
import {PixivRepo} from './repo/model'

import * as Proto from '../common/proto'

import * as yargs from 'yargs'
import * as log4js from 'log4js'

import {IServerConfig, RepositoryType} from './proto'

let logger = log4js.getLogger('Startup');

let defaults :IServerConfig = {
	path: 'pixivRepository',
	repoType: RepositoryType.ArtistBreakdown,
	port: 50415,
	verboseLogging: false
}

export function initServer(config:IServerConfig) {
	let verboseLogging = (config.verboseLogging !== undefined)? config.verboseLogging : defaults.verboseLogging;
	if (verboseLogging) {
		log4js.setGlobalLogLevel(log4js.levels.ALL);
		logger.warn('Setting to verbose mode');
	} else {
		log4js.setGlobalLogLevel(log4js.levels.INFO);
	}

	let path = config.path || defaults.path;
	logger.info(`Setting repo to path [${path}]`);

	function decideRepo() : PixivRepo {
		switch (config.repoType) {
			case RepositoryType.ArtistBreakdown:
				logger.info('Using [root/artist/images*] repo format');
				return new aiRepo.ArtistImageRepo(path);
			case RepositoryType.LooseImages:
				logger.info('Using [root/images*] repo format');
				return new fiRepo.ImageRepo(path);
			default:
				logger.warn('No repository format specified, using default');
				logger.info('Using [root/artist/images*] repo format');
				return new aiRepo.ArtistImageRepo(path);
		}
	}
	// pixiv assistant repo
	let pas = decideRepo();

	let appLogger = log4js.getLogger('App');
	let app = express();

	app.use(bodyParser.urlencoded({ extended: false }));
	app.use(bodyParser.json());

	app.all('/ping', (req, res) => {
		appLogger.debug('Message Received | Ping');
		res.json({success: true, data:true});
	});

	app.all('/supports/:action', (req, res) => {
		let action: string = req.params.action;
		appLogger.debug('Message Received | Supports action [', action, ']');
		res.json({success: true, data: pas.supports(action)});
	});

	app.post('/:action', (req, res) => {
		let action: string = req.params.action;
		let message: any = req.body;
		appLogger.debug('Message Received | Perform action [', action, ']');

		Promise.resolve(pas.dispatch(action, message))
{'fraction_non_alphanumeric': 0.1486341148634115, 'fraction_numerical': 0.005592600559260056, 'mean_word_length': 4.691554467564259, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2264706', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1529, 'n_words': 366}