<commit_before>var koa = require('koa');
var body = require('koa-body');
var cors = require('koa-cors');
var child_process = require('child_process');
var fs = require('mz/fs');
var pathLib = require('path');

var config = require('./config.json');

var app = koa();

var services_dir = config.services_dir;

var loaded_services = {};
var pending_messages = {};

app.use(cors());

app.use(body());

app.use(function*() {

    console.log('Access path: ' + this.path);

    var path = this.path.split('/');
    var service = path[1];

    if (service) {

        if (service === '_reload') {
            if (path[2]) {
                var srv = loaded_services[path[2]];
                if (srv) {
                    srv.kill();
                    delete loaded_services[path[2]];
                    return this.body = 'Service ' + path[2] + ' reloaded';
                } else {
                    this.throw(400, 'Service ' + path[2] + ' not loaded.');
                }
            } else {
                for (var i in loaded_services) {
                    loaded_services[i].kill();
                    delete loaded_services[i];
                }
                return this.body = 'All services reloaded';
            }
        }

        var serviceInstance = loaded_services[service];

        if (!serviceInstance) {

            var thePath = pathLib.join(services_dir, service);
            if (yield fs.exists(thePath + '/index.js')) {
                serviceInstance = prepareService(thePath, service);
            } else {
                this.throw(404, 'Service ' + service + ' not found.');
            }

        }

        var id = newId();
        var prom = new Promise(function (resolve, reject) {

            pending_messages[id] = {
                id: id,
                resolve: resolve,
                reject: reject
            };

        });

        var host = 'http://' + (this.get('X-Forwarded-Host') || this.host) + '/';

        serviceInstance.send({
            id: id,
            message: {
                url: host + service + '/',
                path: path.slice(2),
                query: this.query,
                body: this.request.body
            }
        });

        var result;

        try {
            result = yield prom;
            this.body = result;
        } catch (e) {
            this.status = 500;
            this.body = e.message;
        }

    } else {
        this.body = 'Welcome to this webservices endpoint.'
    }

});

app.listen(config.port || 3000);

function prepareService(thePath, name) {

    var serviceInstance = child_process.fork(pathLib.join(__dirname, 'service.js'), {
        env: {
            WEBSERVICE_DIR: pathLib.resolve(thePath)
        }
    });

    loaded_services[name] = serviceInstance;

    serviceInstance.on('message', handleMessage);

    return serviceInstance;
}
<commit_msg>add file type for responses<commit_after>var koa = require('koa');
var body = require('koa-body');
var cors = require('koa-cors');
var child_process = require('child_process');
var fs = require('mz/fs');
var nativeFs = require('fs');
var pathLib = require('path');

var config = require('./config.json');

var app = koa();

var services_dir = config.services_dir;

var loaded_services = {};
var pending_messages = {};

app.use(cors());

app.use(body());

app.use(function*() {

    console.log('Access path: ' + this.path);

    var path = this.path.split('/');
    var service = path[1];

    if (service) {

        if (service === '_reload') {
            if (path[2]) {
                var srv = loaded_services[path[2]];
                if (srv) {
                    srv.kill();
                    delete loaded_services[path[2]];
                    return this.body = 'Service ' + path[2] + ' reloaded';
                } else {
                    this.throw(400, 'Service ' + path[2] + ' not loaded.');
                }
            } else {
                for (var i in loaded_services) {
                    loaded_services[i].kill();
                    delete loaded_services[i];
                }
                return this.body = 'All services reloaded';
            }
        }

        var serviceInstance = loaded_services[service];

        if (!serviceInstance) {

            var thePath = pathLib.join(services_dir, service);
            if (yield fs.exists(thePath + '/index.js')) {
                serviceInstance = prepareService(thePath, service);
            } else {
                this.throw(404, 'Service ' + service + ' not found.');
            }

        }

        var id = newId();
        var prom = new Promise(function (resolve, reject) {

            pending_messages[id] = {
                id: id,
                resolve: resolve,
                reject: reject
            };

        });

        var host = 'http://' + (this.get('X-Forwarded-Host') || this.host) + '/';

        serviceInstance.send({
            id: id,
            message: {
                url: host + service + '/',
                path: path.slice(2),
                query: this.query,
                body: this.request.body
            }
        });

        var result;

        try {
            result = yield prom;
            if (!result.__type) {
                this.body = result;
            } else {
                switch(result.__type) {
                    case 'file':
                        this.set('Content-Type', result.content_type || 'application/octet-stream');
                        this.set('Content-Disposition', 'attachment; filename="'+result.filename+'"');
                        if (result.content) {
                            this.body = result.content;
                        } else if (result.file) {
                            this.body = nativeFs.createReadStream(result.file);
                        }
                }
            }
        } catch (e) {
            this.status = 500;
            this.body = e.message;
        }

    } else {
        this.body = 'Welcome to this webservices endpoint.'
    }

});

app.listen(config.port || 3000);

function prepareService(thePath, name) {

    var serviceInstance = child_process.fork(pathLib.join(__dirname, 'service.js'), {
        env: {
            WEBSERVICE_DIR: pathLib.resolve(thePath)
        }
    });

    loaded_services[name] = serviceInstance;

    serviceInstance.on('message', handleMessage);

    return serviceInstance;
}

{'fraction_non_alphanumeric': 0.1324349442379182, 'fraction_numerical': 0.006195786864931847, 'mean_word_length': 1.5027131782945737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3556310', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1727, 'n_words': 384}