Example #1
0
lib.setup = function(_config) {
    d("Starting up..");
    config = _config || config;

    config.key_prefix = _config && _config.key_prefix  ? _config.key_prefix : key_prefix;

    return compose.setup(config.compose)

        .then(redisConnection)
        .then(createApi)

        .catch(function(e) {
            console.error("Error occured!");
            console.error(e);
        })

        .finally(function() {
            d("Done!");
        });
};
Example #2
0
var compose_request = function(type, req, res) {

    var authorization = req.headers.authorization || null;
    var soid = null;
    var stream = null;
    var channel = null;

    var body = null;

    var _empty = function(o) { return Object.keys(o).length === 0; };

    var _fail = function(v, msg) {

        v = v || 500;
        msg = msg || "Request failed"

        console.warn(msg);
        res.status(v).send(msg).end();
    };

    var _failLog = function(e) {
        console.warn(e);
        _fail();
    };

    var _success = function() {
        res.status(200).end();
    };

    var _successResponse = function(streamData) {
        res.send(streamData.get());
    };

    /**
     * "X-" prefixes are deprecated, so going with custom ones unprefixed
     * http://tools.ietf.org/html/rfc6648
     */

    // check soid in headers
    if(req.headers.soid) {
        soid = req.headers.soid;
    }

    // check stream in headers
    if(req.headers.stream) {
        stream = req.headers.stream;
    }

    // check channel in headers
    if(req.headers.channel) {
        channel = req.headers.channel;
    }

    if(!_empty(req.body)) {
        body = req.body;
    }
    else if(!_empty(req.query)) {
        body = req.query;
    }
    else if (!_empty(req.params)) {
        body = req.params;
    }

    if(body) {
        if(body.soid) {
            soid = body.soid;
            delete body.soid;
        }

        if(body.stream) {
            stream = body.stream;
            delete body.stream;
        }

        if(body.channel) {
            channel = body.channel;
            delete body.channel;
        }

        if(body.data) {
            body = body.data;
        }

    }
    // check auth header
    if(!authorization) {
        return _fail(403, "Missing api key");
    }

    if(!soid) {
        return _fail(400, "Missing soid");
    }
    if(!stream) {
        return _fail(400, "Missing stream");
    }

    compose.setup({
        apiKey: req.headers.authorization,
        debug: composeDebug,
        transport: 'http'
    }).then(function(api) {

        return api.load(soid).then(function(so) {

            console.log("Requested SO id %s\n stream ", soid, stream);
            var streamObject = so.getStream(stream);

            console.log(streamObject);

            if(type === 'set') {
                streamObject.push(body)
                    .then(_success).catch(_failLog);
            }

            if(type === 'get') {
                streamObject.pull("lastUpdate")
                    .then(function(data) {

                        _successResponse( channel ?  r.get(channel) : r );
                    }).catch(_failLog);
            }

        }).catch(_failLog);

    })
    .catch(_failLog);

};