<commit_before>#!/usr/bin/node

// Add an additional directory, the 'lib' directory, to the 'require' search path.
require.paths.unshift(__dirname + "/lib");
require.paths.unshift('.');

var _ = require('underscore');
var express = require('express');
var events = require('events');
var utils = require('utils');
var auth_enforcer = require('auth_enforcer');
var header_fixer = require('fix_headers');
var info_handler = require('info-handler');
var node_handler = require('node-handler');
var perms_handler = require('perms-handler');
var query_handler = require('query-handler');
var ns_handler = require('namespace-handler');
var schema_handler = require('schema-handler');
var fs = require('fs');
var logger = utils.get_logger();

console.log("OSDF_ROOT: " + utils.get_osdf_root());

// This event emitter is instrumental in providing us
// a way of knowing when all our handlers are ready.
var eventEmitter = new events.EventEmitter();

wait_for_initialization();

// This is the function that launches the app when all
// initialization is complete.
function launch() {
    var app = express.createServer(
        express.logger(),
        header_fixer.remove_powered_by(),
        auth_enforcer.authenticate(),
        express.bodyParser()
    );

    app.get('/nodes/:id', node_handler.get_node);
    app.get('/nodes/:id/ver/:ver', node_handler.get_node);
    app.post('/nodes', node_handler.insert_node);
    app.put('/nodes/:id', node_handler.update_node);
    app.delete('/nodes/:id', node_handler.delete_node);
    app.get('/nodes/:id/out', node_handler.get_out_linkage);
    app.get('/nodes/:id/in', node_handler.get_in_linkage);

    app.post('/nodes/query', query_handler.post_query);
    app.get('/nodes/query/:token', query_handler.getQueryResults);
    app.get('/nodes/query/:token/page/:page', query_handler.getQueryResults);

    app.get('/info', info_handler.info);

    app.get('/namespaces', ns_handler.get_all_namespaces);
    app.get('/namespaces/:ns', ns_handler.get_namespace);

    app.get('/namespaces/:ns/schemas', schema_handler.get_all_schemas);
    app.get('/namespaces/:ns/schemas/:schema', schema_handler.get_schema);
    app.post('/namespaces/:ns/schemas', schema_handler.post_schema);
    app.delete('/namespaces/:ns/schemas/:schema', schema_handler.delete_schema);

    require('config');
    var c = Config.get_instance(utils.get_config());
    var bind_address = c.value("global", "bind_address");
    var port = c.value("global", "port");

    app.listen(port, bind_address);
    // Show some details about the server after it's up and running.
    show_ready(app.address().address, app.address().port);
}

// Display server details when we have started up.
function show_ready(address, port) {<commit_msg>Set UID, important or when we are started as root.<commit_after>#!/usr/bin/node

// Add an additional directory, the 'lib' directory, to the 'require' search path.
require.paths.unshift(__dirname + "/lib");
require.paths.unshift('.');

var _ = require('underscore');
var express = require('express');
var events = require('events');
var utils = require('utils');
var auth_enforcer = require('auth_enforcer');
var header_fixer = require('fix_headers');
var info_handler = require('info-handler');
var node_handler = require('node-handler');
var perms_handler = require('perms-handler');
var query_handler = require('query-handler');
var ns_handler = require('namespace-handler');
var schema_handler = require('schema-handler');
var fs = require('fs');
var logger = utils.get_logger();

console.log("OSDF_ROOT: " + utils.get_osdf_root());

// This event emitter is instrumental in providing us
// a way of knowing when all our handlers are ready.
var eventEmitter = new events.EventEmitter();

wait_for_initialization();

// This is the function that launches the app when all
// initialization is complete.
function launch() {
    var app = express.createServer(
        express.logger(),
        header_fixer.remove_powered_by(),
        auth_enforcer.authenticate(),
        express.bodyParser()
    );

    app.get('/nodes/:id', node_handler.get_node);
    app.get('/nodes/:id/ver/:ver', node_handler.get_node);
    app.post('/nodes', node_handler.insert_node);
    app.put('/nodes/:id', node_handler.update_node);
    app.delete('/nodes/:id', node_handler.delete_node);
    app.get('/nodes/:id/out', node_handler.get_out_linkage);
    app.get('/nodes/:id/in', node_handler.get_in_linkage);

    app.post('/nodes/query', query_handler.post_query);
    app.get('/nodes/query/:token', query_handler.getQueryResults);
    app.get('/nodes/query/:token/page/:page', query_handler.getQueryResults);

    app.get('/info', info_handler.info);

    app.get('/namespaces', ns_handler.get_all_namespaces);
    app.get('/namespaces/:ns', ns_handler.get_namespace);

    app.get('/namespaces/:ns/schemas', schema_handler.get_all_schemas);
    app.get('/namespaces/:ns/schemas/:schema', schema_handler.get_schema);
    app.post('/namespaces/:ns/schemas', schema_handler.post_schema);
    app.delete('/namespaces/:ns/schemas/:schema', schema_handler.delete_schema);

    require('config');
    var c = Config.get_instance(utils.get_config());
    var bind_address = c.value("global", "bind_address");
    var port = c.value("global", "port");

    // Check that we have some valid settings.
    if (bind_address == null || bind_address.length == 0) {
        console.log("The 'bind_address' setting is not configured.");
        process.exit(1);
    }

    if (port == null || port.length == 0) {
        console.log("The 'port' setting is not configured.");
        process.exit(1);
    }

    app.listen(port, bind_address);
    
    // If we are being started via sys-v style init scripts we are probably being
    // invoked as root. If we need to listen on a well known port, we need to be
    // launched as root to bind to the port, but then drop down to another UID.
    if (process.getuid() == 0) {
        // Who do we drop privileges to?
        var user = c.value("global", "user");
        if (user === null) {
            console.log("The 'user' setting is not configured.");
            process.exit(1);
        }

        console.log("Launched as root. Switching to " + user); 
        process.setuid(user);
    }

    // Show some details about the server after it's up and running.
    show_ready(app.address().address, app.address().port);
}

// Display server details when we have started up.
function show_ready(address, port) {
{'fraction_non_alphanumeric': 0.15237507711289328, 'fraction_numerical': 0.0009253547193090684, 'mean_word_length': 3.9655436447166923, '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': '5148212', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1968, 'n_words': 468}