Skip to content

Instantly share code, notes, and snippets.

@joaoneto
Forked from pulkitsinghal/env.sh
Created September 14, 2013 18:26

Revisions

  1. @pulkitsinghal pulkitsinghal revised this gist Apr 18, 2013. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion env.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,13 @@
    # local env.
    $ export COUCH_HOST=https://xxx.couchdb.com
    $ export COUCH_PORT=xxx
    $ export COUCH_USERNAME=xxx
    $ export COUCH_PASSWORD=xxx
    $ export COUCH_DATABASE=xxx
    $ export COUCH_DATABASE=xxx

    # cloud env.
    $ heroku config:add COUCH_HOST=https://xxx.couchdb.com
    $ heroku config:add COUCH_PORT=xxx
    $ heroku config:add COUCH_USERNAME=xxx
    $ heroku config:add COUCH_PASSWORD=xxx
    $ heroku config:add COUCH_DATABASE=xxx
  2. @pulkitsinghal pulkitsinghal revised this gist Apr 18, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,7 @@ http.createServer(function (req, res) {
    console.log('Failed to save...');
    console.error(err);
    } else {
    console.log('Added!');
    console.log('Added!');
    console.log(res);
    }
    }
    @@ -59,8 +59,8 @@ http.createServer(function (req, res) {
    }
    );

    res.writeHead(200, "OK", {'Content-Type': 'text/html'});
    res.end();
    res.writeHead(200, "OK", {'Content-Type': 'text/html'});
    res.end();
    });
    req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
  3. @pulkitsinghal pulkitsinghal created this gist Apr 18, 2013.
    5 changes: 5 additions & 0 deletions env.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    $ export COUCH_HOST=https://xxx.couchdb.com
    $ export COUCH_PORT=xxx
    $ export COUCH_USERNAME=xxx
    $ export COUCH_PASSWORD=xxx
    $ export COUCH_DATABASE=xxx
    68 changes: 68 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    var http = require('http'),
    form2json = require('form2json'),
    cradle = require('cradle');

    cradle.setup({
    host: process.env.COUCH_HOST,
    port: process.env.COUCH_PORT,
    cache: true,
    raw: false
    });

    var adminConnection = new(cradle.Connection) (
    process.env.COUCH_HOST,
    process.env.COUCH_PORT,
    {
    auth: {
    username: process.env.COUCH_USERNAME,
    password: process.env.COUCH_PASSWORD
    }
    }
    );

    http.createServer(function (req, res) {
    var fullBody = '';
    req.on('data', function(chunk) {
    // append the current chunk of data to the fullBody variable
    fullBody += chunk.toString();
    });
    req.on('end', function() {
    var jsonBody = form2json.decode(fullBody);
    var payload = JSON.parse(jsonBody.payload);
    payload.type = jsonBody.type;

    var vendDB = adminConnection.database(process.env.COUCH_DATABASE);
    vendDB.merge(
    payload.id,
    payload,
    function (err, res) {
    if (err) {
    console.log('No previous entry, adding...');
    //console.error(err);
    vendDB.save(
    payload.id,
    payload,
    function (err, res) {
    if (err) {
    console.log('Failed to save...');
    console.error(err);
    } else {
    console.log('Added!');
    console.log(res);
    }
    }
    );
    } else {
    console.log('Found previous entry, updating...');
    console.log(res);
    }
    }
    );

    res.writeHead(200, "OK", {'Content-Type': 'text/html'});
    res.end();
    });
    req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
    });
    }).listen(8080);