*/

var mongoose = require('mongoose'),
	twitter = require('twitter'), //ntwitter - allows easy JS access to twitter API's - https://github.com/AvianFlu/ntwitter
	_ = require('underscore'),
	fs = require('fs'),
	getenv = require('getenv'),

	SocketServer = null,
	Symbol = mongoose.model('Symbol'),
	State = mongoose.model('State'),
	state = require('./stateController'),

	pkg = require('package.json'),

	FAKE_TWITTER_CONNECTION = getenv.bool('FAKE_TWITTER_CONNECTION', false),
	SAVE_TWEETS_TO_FILE = false,
	SERVER_BACKOFF_TIME = 30000,
	TEST_TWEET_TIMER = 10,
	STATE_SAVE_DURATION = 600000,

	_this = this;


var TwitterController = {

	activeStream : null,

	twitterStreamingApi : null,
	tags : null,
Beispiel #2
0
// Copyright 2011-2012 mbr targeting GmbH. All Rights Reserved.

const util = require('util');

const getenv = require('getenv');
const isError = require('lodash.iserror');
const isString = require('lodash.isstring');

const logLevels = ['fatal', 'error', 'warn', 'info', 'debug'];

const logStack = getenv.bool('LOG_STACK', true);
function forward(target, prefix) {
  return (format, ...args_) => {
    const placeholders = isString(format) ?
      (format.match(/%[sdifjoO%]/g) || [])
        .filter((placeholder) => placeholder !== '%%')
        .length :
      0;
    const args = args_.slice(0, placeholders);

    const errors = (logStack ? args_ : [])
      .filter(isError)
      .map((error) => util.inspect(error, false, 3));

    const rest = args_.slice(placeholders).filter((value) => !isError(value));
    console[target]('%s', util.format(prefix + format, ...args, ...rest) +
                          ['', ...errors].join('\n'));
  };
}

exports.fatal = forward('error', 'FATAL: ');
Beispiel #3
0
'use strict';

const fs = require('fs');
const path = require('path');
const getenv = require('getenv');
const Sequelize = require('sequelize');

let basename = path.basename(module.filename),
    db = {};

require('dotenv').config({silent: true, path: path.join(__dirname, '..', '.env') });

let dbString = getenv('DATABASE_URL'),
    options = {
      logging: getenv.bool('DEBUG_LOGGING', false),
      dialectOptions: { ssl: true },
      define: { timestamps: false }
    },
    sequelize;

sequelize = new Sequelize(dbString, options);

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    let model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });
Beispiel #4
0
function Server(app, settings, callback) {
    var _ = require('underscore');
    var getenv = require('getenv');
    var DEFAULT_PORT = 3000;
    var AUTO_INDEXING = getenv.bool('AUTO_INDEXING', true);

    /* Get config and fail if error */
    var nconf = getSettings(settings.settingsFile, function(err, res) {
        if (err) callback(err);
    });
    if (!nconf) {
        return null;
    }
    var isConstruct = settings.isConstruct;

    function exists_index(elastic){
        var request = require('sync-request');
        var indexed_url = 'http://' + encodeURIComponent(elastic.rwuser) + ":" + encodeURIComponent(elastic.rwpass) + "@"+ elastic.host + ':' + elastic.port + elastic.path + elastic.index + '/_count';
        var count_current = 0;
        var res;
        try{
            res = request('GET', indexed_url)
            count_current = JSON.parse(res.getBody('utf8')).count;
            if (isNaN(count_current)){
                count_current = 0;
            }
        }catch(e){
            if(res.statusCode != 404){
                return exists_index(elastic);
            }else{
                return false
            }
        }
        return (count_current > 0);
    }
    /* Get server entrypoint commands */
    function runServer() {
        var http = require('http');
        var server = app.listen(nconf.get('http:port') || DEFAULT_PORT);
    }

    var commands = {
        'runserver': runServer,
    }
    if (app.get('managementCommands')) {
        commands = _.extend(commands, app.get('managementCommands'));
    }
    if (commands.help === undefined) {
        commands.help = function() {
            console.log("List of available commands: " + _.keys(commands));
        }
    }

    function run(cmd, app_dir, args, callback) {
        var cmdFunc = commands[cmd];
        if (cmdFunc === undefined) {
            callback(new Error("Command '" + cmd + "' unrecognized."));
            return;
        }
        cmdFunc(app_dir, args);
        callback(null, this);

        if (cmd == 'runserver' && AUTO_INDEXING === true){
            var waitForPort = require('wait-for-port');
            var elastic = nconf.get()['elastic'];
            if (!isConstruct){
              elastic.real_index = elastic.index + '_blue';
            }
            else {
              elastic.real_index = elastic.index;
            }
            waitForPort(elastic.host, elastic.port, { numRetries: 30 }, function(err) {
                if (err) throw new Error(err);
                var exists = exists_index(elastic);
                if (exists === false){
                    commands['create_index'](settings.indexing, args);
                }
            });
        }

    }

    var res = {
        run: run,
        nconf: nconf
    };

    callback(null, res);
    return res;
}