<commit_before>
//------------------------------------------------------------------------------
var http = require('http'),
    url = require('url'),
    environments = {
        'Development': 0,
        'Production': 1
    },
    server = http.createServer(),
    regExp = {
        apiVersion: new RegExp('^v[0-9]+$', 'i'),
        date: new RegExp('^20[1-9][0-9][01][0-9][01][0-9]$')
    },
    errorId = 0,
    port = 3000,                        // Configuration: Port Number
    env = environments.Development,     // Configuration: Environment
    logAPICalls = false;                // Configuration: Logging
//------------------------------------------------------------------------------
function getDateStamp () {
    var today = new Date(),
        year = today.getFullYear(),
        month = today.getMonth()+1,
        day = today.getDate();

    return year.toString() 
        + (month<10 ? ('0'+month) : month) 
        + (day<10 ? ('0'+day) : day);
}
//------------------------------------------------------------------------------
function isDevelopment () {
    return env === environments.Development;
}
//------------------------------------------------------------------------------
function done (response, result, code, type) {
    code = code || 200;
    type = type || 'application/json';
    response.writeHead(code, {'Content-Type': type});
    response.write(JSON.stringify(result));
    response.end();
}
//------------------------------------------------------------------------------
function apiError (response, error, code) {
    code = code || 500;
    if(code === 500) {
        // Hide the error message in production environment
        error: isDevelopment() ? error : 
                    'Sorry, unable to service this request.'
    }
    // TODO: Log error in error file
    done(response, {
        error: error,
        errorId: errorId
    }, code);
}
//------------------------------------------------------------------------------
function notFound (response, error) {
    error = error || 'API Not found.';
    apiError(response, error, 404);
}
//------------------------------------------------------------------------------
server.on('request', function (request, response) {
    try {
        var apiCall = request.url.toLowerCase(),
            segments = apiCall.substr(1).split('?')[0].toLowerCase().split('/'),
            api = null,
            query = url.parse(request.url, true).query,
            method = (query.method || request.method).toUpperCase(),
            fileName;
        
        if(logAPICalls || isDevelopment()) {
            // TODO: Log api request in log file
        }

        switch (segments[0]) {
            case 'api':     // Format: ^/api/v[0-9]+/
                if(segments.length<3) {
                    notFound(response);
                } else {
                    if(!regExp.apiVersion.test(segments[1])) {
                        notFound(response, 'Invalid api version.');
                    } else {
                        fileName = './' + segments[0] 
                                    + '/' + segments[1]
                                    + '/' + segments[2];
                        api = require(fileName);
                        if(api) {
                            // TODO: Combine form variables with query as inputs
                            api.process(method, 
                                segments, 
                                query, 
                                request, 
                                response, 
                                isDevelopment(), 
                                function (result, error, code) {
                                    if(!error) {
                                        done(response, result);
                                    } else {
                                        apiError(response, error);
                                    }
                                }
                            );  
                        }
                    }
                }
                break;
            case 'log':     // Format: ^/log/20[1-9][0-9][01][0-9][01][0-9]$
            case 'error':   // Format: ^/error/20[1-9][0-9][01][0-9][01][0-9]$
                if(isDevelopment()) {
                    fileName = segments[1] + '/';
                    if(segments.length==3) {
                        // Date is specified
                        if(!regExp.date.test(segments[2])) {
                            notFound(response, 'Invalid date.');
                        } else {
                            fileName += segments[2];
                        }
                    } else {
                        // Default to today
                        fileName += getDateStamp();
                    }
                    // TODO: Render the error/log file                  
                } else {
                    notFound(response);
                }
                break;
            default:
                notFound(response, 
                    'Sorry, the resource you requested does not exist.');
                break;
        }
    } catch(error) {
        if(error.code && error.code=='MODULE_NOT_FOUND') {
            notFound(response);
        } else {
            apiError(response, error);
        }
    }
    
});
//------------------------------------------------------------------------------
server.listen(port);
if(isDevelopment()) {<commit_msg>1. Fixed regex error 2. Updated error messages 3. Fixed bug in log and error file rendering code<commit_after>
//------------------------------------------------------------------------------
var http = require('http'),
    url = require('url'),
    environments = {
        'Development': 0,
        'Production': 1
    },
    server = http.createServer(),
    regExp = {
        apiVersion: new RegExp('^v[0-9]+$', 'i'),
        date: new RegExp('^20[1-9][0-9][01][0-9][0-3][0-9]$')
    },
    errorId = 0,
    port = 3000,                        // Configuration: Port Number
    env = environments.Development,     // Configuration: Environment
    logAPICalls = false;                // Configuration: Logging
//------------------------------------------------------------------------------
function getDateStamp () {
    var today = new Date(),
        year = today.getFullYear(),
        month = today.getMonth()+1,
        day = today.getDate();

    return year.toString() 
        + (month<10 ? ('0'+month) : month) 
        + (day<10 ? ('0'+day) : day);
}
//------------------------------------------------------------------------------
function isDevelopment () {
    return env === environments.Development;
}
//------------------------------------------------------------------------------
function done (response, result, code, type) {
    code = code || 200;
    type = type || 'application/json';
    response.writeHead(code, {'Content-Type': type});
    response.write(JSON.stringify(result));
    response.end();
}
//------------------------------------------------------------------------------
function apiError (response, error, code) {
    code = code || 500;
    if(code === 500) {
        // Hide the error message in production environment
        error: isDevelopment() ? error : 
                    'Sorry, unable to service this request.'
    }
    // TODO: Log error in error file
    done(response, {
        error: error,
        errorId: errorId
    }, code);
}
//------------------------------------------------------------------------------
function notFound (response, error) {
    error = error || 'API Not found.';
    apiError(response, error, 404);
}
//------------------------------------------------------------------------------
server.on('request', function (request, response) {
    try {
        var apiCall = request.url.toLowerCase(),
            segments = apiCall.substr(1).split('?')[0].toLowerCase().split('/'),
            api = null,
            query = url.parse(request.url, true).query,
            method = (query.method || request.method).toUpperCase(),
            fileName;
        
        if(logAPICalls || isDevelopment()) {
            // TODO: Log api request in log file
        }

        switch (segments[0]) {
            case 'api':     // Format: ^/api/v[0-9]+/
                if(segments.length<3) {
                    notFound(response);
                } else {
                    if(!regExp.apiVersion.test(segments[1])) {
                        notFound(response, 
                            'Invalid api version. Expected: /api/v1/...');
                    } else {
                        fileName = './' + segments[0] 
                                    + '/' + segments[1]
                                    + '/' + segments[2];
                        api = require(fileName);
                        if(api) {
                            // TODO: Combine form variables with query as inputs
                            api.process(method, 
                                segments, 
                                query, 
                                request, 
                                response, 
                                isDevelopment(), 
                                function (result, error, code) {
                                    if(!error) {
                                        done(response, result);
                                    } else {
                                        apiError(response, error);
                                    }
                                }
                            );  
                        }
                    }
                }
                break;
            case 'log':     // Format: ^/log/20[1-9][0-9][01][0-9][01][0-9]$
            case 'error':   // Format: ^/error/20[1-9][0-9][01][0-9][01][0-9]$
                if(isDevelopment()) {
                    fileName = './' + segments[0] + '/';
                    if(segments.length==2) {
                        // Date is specified
                        if(!regExp.date.test(segments[1])) {
                            notFound(response, 
                                'Invalid date. Expected: /' + segments[0]
                                    + '/' + getDateStamp());
                        } else {
                            fileName += segments[1];
                        }
                    } else {
                        // Default to today
                        fileName += getDateStamp();
                    }
                    // TODO: Render the error/log file
                    done(response, 'No data:' + fileName, 200, 'text/plain');              
                } else {
                    notFound(response);
                }
                break;
            default:
                notFound(response, 
                    'Sorry, the resource you requested does not exist.');
                break;
        }
    } catch(error) {
        if(error.code && error.code=='MODULE_NOT_FOUND') {
            notFound(response);
        } else {
            apiError(response, error);
        }
    }
    
});
//------------------------------------------------------------------------------
server.listen(port);
if(isDevelopment()) {
{'fraction_non_alphanumeric': 0.22422041105598867, 'fraction_numerical': 0.01559177888022679, 'mean_word_length': 0.8628712871287129, 'pattern_counts': {'":': 0, '<': 9, '<?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': 33, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1481387', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2489, 'n_words': 546}