{"size": 2379, "ext": "js", "max_stars_repo_path": "inbeat/inbeat-gain/listener/server.js", "max_stars_repo_name": "KIZI/InBeat", "max_stars_repo_stars_event_min_datetime": "2015-06-24T01:16:31.000Z", "max_stars_repo_stars_event_max_datetime": "2020-01-18T13:07:54.000Z", "max_issues_repo_path": "inbeat/inbeat-gain/listener/server.js", "max_issues_repo_name": "KIZI/InBeat", "max_issues_repo_issues_event_min_datetime": "2015-06-10T09:59:40.000Z", "max_issues_repo_issues_event_max_datetime": "2015-06-10T09:59:40.000Z", "max_forks_repo_path": "inbeat/inbeat-gain/listener/server.js", "max_forks_repo_name": "KIZI/InBeat", "max_forks_repo_forks_event_min_datetime": "2017-08-10T16:24:37.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-02T01:52:27.000Z"}
{"max_stars_count": 3, "max_issues_count": 1, "max_forks_count": 2, "avg_line_length": 28.6626506024, "max_line_length": 70, "alphanum_fraction": 0.4514501892}
/**
 * InBeat - Interest Beat
 * @author Jaroslav Kuchař (https://github.com/jaroslav-kuchar)
 * 
 * Use of this source code is governed by a license that
 * can be found in the LICENSE file. 
 * 
 */

// http server
var http = require('http');
// db
var db = require('./db/mongo');
// interceptor
var restInterceptor = require('./interceptor/rest');

var config = require("../config");
var logger = config.Logger;

var port = config["inbeat-gain"]["listener-port"];

// start simple server
http.createServer(function(req, res) {
    if (req.method == 'POST') {
        // if request -> process data
        restInterceptor.processRequest(req, function(error, data) {
            if (error) {
                // throw error;
                logger.warn('rest interceptor: ' + error);
                res.writeHead(400, {
                    'Content-Type': 'text/plain'
                });
                res.write(error);
                res.end();
            }
            // if data ok
            if (data) {
                // save
                db.add(data, function(error) {
                    // throw error
                    if (error) {
                        // throw error;
                        logger.warn('rest db: ' + error);
                        res.writeHead(400, {
                            'Content-Type': 'text/plain'
                        });
                        res.write(error);
                        res.end();
                    } else {
                        res.writeHead(201, {
                            'Content-Type': 'text/plain'
                        });
                        res.write('');
                        res.end();
                    }
                });
            }
        });
    } else {
        var error = 'Wrong HTTP method, only HTTP POST is supported.';
        logger.warn(error);
        res.writeHead(405, {
            'Content-Type': 'text/plain',
            'Allow': 'POST'
        });
        res.write(error);
        res.end();
    }
}).listen(port, function() {
    // for unit tests - child process...
    if (process.send) {
        process.send('listening');
    }
});

process.on('SIGINT', function() {
    console.log('\nshutting down from  SIGINT (Crtl-C)');
    process.exit();
});

// log start of server
console.log('InBeat: GAIN listener listening on: ' + port + '');
