var bogart = require('bogart')
    ,path  = require('path');

require('dotenv').load();

var viewEngine = bogart.viewEngine('mustache', path.join(bogart.maindir(), 'views'));
var root = require("path").join(__dirname, "public");
var router = bogart.router();

router.get('/', function(req) { 


  // var articles = nano.db.use('articles');

  // var articlelist = bogart.promisify(articles.view);

  // return articlelist('article_list', 'articleView').then(function(data) {
  //   return viewEngine.respond('posts.html', {
  //     locals: {
  //       pagetitle: 'all posts',
  //       postlist: data.rows
  //     }
  //   });
  // });



    // var article = { };

    var MongoClient = require('mongodb').MongoClient;

    var url = 'mongodb://';
    if (process.env.DB_USER) {
      url = url + process.env.DB_USER+':'+process.env.DB_PASS+'@';
    }
    url = url + process.env.DB_HOST+':'+process.env.DB_PORT+'/'+process.env.DB_NAME
    // Connect to the db
    MongoClient.connect(url, function(err, db) {
      if(err) { return console.dir(err); }

      var collection = db.collection('webhooks');

      collection.find().toArray(function(err, items) {
        db.close();
        return viewEngine.respond('index.html', items);
      });

      // var stream = collection.find({mykey:{$ne:2}}).stream();
      // stream.on("data", function(item) {});
      // stream.on("end", function() {});

      // collection.findOne({mykey:1}, function(err, item) {});

    });

    // MongoClient.connect(url, function(err, db) {
    //     // Find all data
    //     collection.find({})
    //       // .limit(1)
    //       .toArray(function(err, datathings) {
    //         db.close();
    //         if (err != null) {
    //           return viewEngine.respond('index.html', datathings);
    //         }
    //         else {
    //           if (d.length > 0) {
    //             d[0].count = d[0].errordata.length;
    //           }
    //           return viewEngine.respond('index.html', datathings);
    //         }
    //     });
    // });

});

router.post('/payload', function(req) {
    var MongoClient = require('mongodb').MongoClient;

    var url = 'mongodb://';
    if (process.env.DB_USER) {
      url = url + process.env.DB_USER+':'+process.env.DB_PASS+'@';
    }
    url = url + process.env.DB_HOST+':'+process.env.DB_PORT+'/'+process.env.DB_NAME

    MongoClient.connect(url, function(err, db) {
      var collection = db.collection('webhooks');
      collection.insert(req.body, function(err, result) {
          if(err)
              throw err;
       
          console.log("entry saved");
      });
    });
    return bogart.json({ webhook: 'thanks' });
});

var app = bogart.app();
app.use(bogart.batteries({ secret: 'xGljGo7f4g/a1QveU8VRxhZP5Hwi2YWelejBq5h4ynM'})); // A batteries included JSGI stack including streaming request body parsing, session, flash, and much more.
app.use(bogart.middleware.directory(root));
app.use(router); // Our router
console.log('Loading ... ');
app.start(4567);
console.log('done.\n');
@@ -290,384 +290,60 @@
  %7B %0A
-%0A%0A  // var articles = nano.db.use('articles');%0A%0A  // var articlelist = bogart.promisify(articles.view);%0A%0A  // return articlelist('article_list', 'articleView').then(function(data) %7B%0A  //   return viewEngine.respond('posts.html', %7B%0A  //     locals: %7B%0A  //       pagetitle: 'all posts',%0A  //       postlist: data.rows%0A  //     %7D%0A  //   %7D);%0A  // %7D);%0A%0A%0A%0A    // var article = %7B %7D;%0A
+    console.log('GET / - Find the saved webhooks.');
 %0A   
@@ -610,16 +610,54 @@
 DB_NAME%0A
+    console.log('Connect to ' + url);%0A
     // C
@@ -764,16 +764,58 @@
 (err); %7D
+%0A      console.log('DB connection valid');
 %0A%0A      
@@ -910,24 +910,61 @@
 r, items) %7B%0A
+        console.log('found', items);%0A
         db.c
@@ -1046,859 +1046,119 @@
     
-  // var stream = collection.find(%7Bmykey:%7B$ne:2%7D%7D).stream();%0A      // stream.on(%22data%22, function(item) %7B%7D);%0A      // stream.on(%22end%22, function() %7B%7D);%0A%0A      // collection.findOne(%7Bmykey:1%7D, function(err, item) %7B%7D);%0A%0A    %7D);%0A%0A    // MongoClient.connect(url, function(err, db) %7B%0A    //     // Find all data%0A    //     collection.find(%7B%7D)%0A    //       // .limit(1)%0A    //       .toArray(function(err, datathings) %7B%0A    //         db.close();%0A    //         if (err != null) %7B%0A    //           return viewEngine.respond('index.html', datathings);%0A    //         %7D%0A    //         else %7B%0A    //           if (d.length %3E 0) %7B%0A    //             d%5B0%5D.count = d%5B0%5D.errordata.length;%0A    //           %7D%0A    //           return viewEngine.respond('index.html', datathings);%0A    //         %7D%0A    //     %7D);%0A    // %7D);%0A%0A%7D);%0A%0Arouter.post('/payload', function(req) %7B
+%7D);%0A%7D);%0A%0Arouter.post('/payload', function(req) %7B%0A    console.log('POST /payload - Save the incoming webhook.');
 %0A   

Add logging