<commit_before>var http = require( "http" ),
	hook = require( "../lib/hook" ),
	logger = require( "../lib/logger" );

process.on( "uncaughtException", function( error ) {
	logger.error( "Uncaught exception: " + error.stack );
});

http.createServer(function( request, response ) {
	var json = "";
	request.setEncoding( "utf8" );
	request.on( "data", function( chunk ) {
		json += chunk;
	});
	request.on( "end", function() {
		response.writeHead( 200 );
		response.end();

		try {
			json = JSON.parse( json );
		} catch( e ) {
			logger.error( "Invalid request: " + json );
			return;
		}

		hook.processHook( json, function( error ) {
			if ( error ) {
				logger.error( "Error processing hook: " + error.stack );
			}
		});
	});
}).listen( 8001 );<commit_msg>Graceful startup and shutdown of HTTP server.<commit_after>var http = require( "http" ),
	hook = require( "../lib/hook" ),
	logger = require( "../lib/logger" );

var port = (function() {
	var index = process.argv.indexOf( "-p" );
	return index === -1 ? 8001 : +process.argv[ index + 1 ];
})();

process.on( "uncaughtException", function( error ) {
	logger.error( "Uncaught exception: " + error.stack );
});

var server = http.createServer(function( request, response ) {
	var json = "";
	request.setEncoding( "utf8" );
	request.on( "data", function( chunk ) {
		json += chunk;
	});

	request.on( "end", function() {
		try {
			json = JSON.parse( json );
		} catch( e ) {
			// Invalid JSON, stop processing
			logger.error( "Invalid request: " + json );
			response.writeHead( 400 );
			response.end();
			return;
		}

		// Accept the request and close the connection
		response.writeHead( 202 );
		response.end();

		// Process the request
		hook.processHook( json, function( error ) {
			if ( error ) {
				logger.error( "Error processing hook: " + error.stack );
			}
		});
	});
});

// If another process is using this port, keep retrying
server.on( "error", function( error ) {
	if ( error.code === "EADDRINUSE" ) {
		return setTimeout(function() {
			// server.close();
			server.listen( port );
		}, 100 );
	}
});

server.listen( port );

process.on( "SIGINT", function() {
	server.close();
});
{'fraction_non_alphanumeric': 0.18400369856680537, 'fraction_numerical': 0.011095700416088766, 'mean_word_length': 3.2682445759368837, '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': '7453317', 'n_tokens_mistral': 791, 'n_tokens_neox': 710, 'n_words': 187}