<commit_before>#!/usr/bin/env node
var express = require('express'),
    color = require('colors'),
    pros = require('./promises'),
    httpServer = require('http');

var VERSION = '0.1.6';

var sys = {__version: VERSION};
var server = express();
var main = express();

var allowCrossDomain = function (req, res, next) {
    'use strict';
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
    res.header('Access-Control-Allow_Headers', 'Content-Type');
    next();
};

// check if the requested URL contains file extensions
var checkUrl = function (req, res, next) {
    'use strict';
    // only if contains sub-levels
    if (req.method.toUpperCase() === 'GET' && req.url.indexOf('.') === -1 &&
        req.url.lastIndexOf('/') !== req.url.length - 1) {
        res.redirect(req.url + '/');
    } else {
        next();
    }
};

main.use(express.compress());
main.use(express.bodyParser());
main.use(express.methodOverride());
main.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
main.use(allowCrossDomain);
main.use(checkUrl);

var prepareFilename = function (filename) {
    'use strict';
    return filename.replace(/^\$ROOT/, sys.root);
};

var interpretSites = function (sites, app) {
    'use strict';
    if (sites) {
        sites.forEach(function (site) {
            var domain = site.url || undefined;
            var local = site.local || undefined;
            var directory;

            if (domain && local) {
                directory = prepareFilename(local);
                app.use(domain, express.static(directory));
                console.log('Registered site "'.green + domain +
                           '" with "'.green + directory + '".'.green);
            }
        });
    }
    app.use(app.router);
};

var interpretSockets = function (sockets, app) {
    'use strict';
    if (sockets) {
        sockets.forEach(function (socket) {
            var port = socket.port || 3030;
            var local = socket.local || undefined;
            var filename;
            if (local) {
                var httpd = httpServer.createServer(app);
                filename = prepareFilename(local);
                require(filename).__socket(httpd);
                console.log('Loaded WebSocket "'.green + local +
                            '" at port "'.green + port + '".'.green);
                httpd.listen(port);
            }
        });
    }
};

var interpretApis = function (apis, app) {
    'use strict';
    if (apis) {
        apis.forEach(function (api) {
            var uri = api.uri || undefined;
            var method = api.method || undefined;
            var local = api.local || undefined;
            var filename;

            if(uri && method && local) {
                filename = prepareFilename(local);
                switch (method.toUpperCase()) {
                case 'GET':
                    app.get(uri, function (req, res) {
                        require(filename).__get(req, res);
                    });
                    break;
                case 'POST':
                    app.post(uri, function (req, res) {
                        require(filename).__post(req, res);
                    });
                    break;
                case 'PUT':
                    app.put(uri, function (req, res) {
                        require(filename).__put(req, res);
                    });
                    break;
                case 'DELETE':
                    app.delete(uri, function (req, res) {
                        require(filename).__delete(req, res);
                    });
                    break;
                case 'ALL':
                    app.all(uri, function (req, res) {
                        require(filename).__all(req, res);
                    });
                    break;
                }
                console.log('Registed API "'.green + uri + '" '.green +
                            method.toUpperCase().blue +' with "'.green +
                           local + '".'.green);
            }
        });
    }
};

var loadFromJson = function (json) {
    'use strict';

    // set top level domain
    sys.top = json.top || 'http://127.0.0.1';
    main.use(express.vhost(sys.top, server));
    console.log('Top domain: '.green + sys.top);

    // set httpPort
    sys.httpPort = json.port || 3000;
    console.log('HTTP Port: '.green + sys.httpPort);

    sys.root = json.root || '';
    console.log('Root: '.green + sys.root);

    sys.log = json.log || 0;
    if (sys.log === 1) {
        server.use(express.logger());
    }

    sys.favicon = json.favicon || undefined;
    if (sys.favicon) {
        server.use(express.favicon(prepareFilename(sys.favicon)));
        console.log('Favicon: '.green + sys.favicon);<commit_msg>Minor changes in config file interpreting<commit_after>#!/usr/bin/env node
var express = require('express'),
    color = require('colors'),
    pros = require('./promises'),
    httpServer = require('http');

var VERSION = '0.1.6';

var sys = {__version: VERSION};
var server = express();
var main = express();

var allowCrossDomain = function (req, res, next) {
    'use strict';
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
    res.header('Access-Control-Allow_Headers', 'Content-Type');
    next();
};

// check if the requested URL contains file extensions
var checkUrl = function (req, res, next) {
    'use strict';
    // only if contains sub-levels
    if (req.method.toUpperCase() === 'GET' && req.url.indexOf('.') === -1 &&
        req.url.lastIndexOf('/') !== req.url.length - 1) {
        res.redirect(req.url + '/');
    } else {
        next();
    }
};

main.use(express.compress());
main.use(express.bodyParser());
main.use(express.methodOverride());
main.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
main.use(allowCrossDomain);
main.use(checkUrl);

var prepareFilename = function (filename) {
    'use strict';
    return filename.replace(/^\$ROOT/, sys.root);
};

var interpretSites = function (sites, app) {
    'use strict';
    if (sites) {
        sites.forEach(function (site) {
            var domain = site.url || undefined;
            var local = site.local || undefined;
            var directory;

            if (domain && local) {
                directory = prepareFilename(local);
                app.use(domain, express.static(directory));
                console.log('Registered site "'.green + domain +
                           '" with "'.green + directory + '".'.green);
            }
        });
    }
    app.use(app.router);
};

var interpretSockets = function (sockets, app) {
    'use strict';
    if (sockets) {
        sockets.forEach(function (socket) {
            var port = socket.port || 3030;
            var local = socket.local || undefined;
            var filename;
            if (local) {
                var httpd = httpServer.createServer(app);
                filename = prepareFilename(local);
                require(filename).__socket(httpd);
                console.log('Loaded WebSocket "'.green + local +
                            '" at port "'.green + port + '".'.green);
                httpd.listen(port);
            }
        });
    }
};

var interpretApis = function (apis, app) {
    'use strict';
    if (apis) {
        apis.forEach(function (api) {
            var uri = api.uri || undefined;
            var method = api.method || undefined;
            var local = api.local || undefined;
            var filename;

            if(uri && method && local) {
                filename = prepareFilename(local);
                switch (method.toUpperCase()) {
                case 'GET':
                    app.get(uri, function (req, res) {
                        require(filename).__get(req, res);
                    });
                    break;
                case 'POST':
                    app.post(uri, function (req, res) {
                        require(filename).__post(req, res);
                    });
                    break;
                case 'PUT':
                    app.put(uri, function (req, res) {
                        require(filename).__put(req, res);
                    });
                    break;
                case 'DELETE':
                    app.delete(uri, function (req, res) {
                        require(filename).__delete(req, res);
                    });
                    break;
                case 'ALL':
                    app.all(uri, function (req, res) {
                        require(filename).__all(req, res);
                    });
                    break;
                }
                console.log('Registed API "'.green + uri + '" '.green +
                            method.toUpperCase().blue +' with "'.green +
                           local + '".'.green);
            }
        });
    }
};

var loadFromJson = function (json) {
    'use strict';

    // set top level domain
    sys.top = json.top || '127.0.0.1';
    main.use(express.vhost(sys.top, server));
    console.log('Top domain: '.green + sys.top);

    // set httpPort
    sys.httpPort = json.port || process.env.PORT;
    console.log('HTTP Port: '.green + sys.httpPort);

    sys.root = json.root || '';
    console.log('Root: '.green + sys.root);

    sys.log = json.log || 0;
    if (sys.log === 1) {
        server.use(express.logger());
    }

    sys.favicon = json.favicon || undefined;
    if (sys.favicon) {
        server.use(express.favicon(prepareFilename(sys.favicon)));
        console.log('Favicon: '.green + sys.favicon);
{'fraction_non_alphanumeric': 0.1474735000514562, 'fraction_numerical': 0.0039106720181125865, 'mean_word_length': 1.632177681473456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2299141', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2646, 'n_words': 610}