<commit_before>
var express   = require('express')
  , rawBody   = require('raw-body')
  , fs        = require('fs')
  , path      = require('path')
  , extend    = require('extend')
  , pkg       = require('./package.json')
  , dbs       = {}
  , uuids     = require('./uuids')
  , histories = {}
  , app       = express();

var Pouch;
module.exports = function(PouchToUse) {
  Pouch = PouchToUse;
  require('pouchdb-all-dbs')(Pouch);
  return app;
};

function isPouchError(obj) {
  return obj.error && obj.error === true;
}

app.use('/js', express.static(__dirname + '/fauxton/js'));
app.use('/css', express.static(__dirname + '/fauxton/css'));
app.use('/img', express.static(__dirname + '/fauxton/img'));

app.use(function (req, res, next) {
  var opts = {}
    , data = ''
    , prop;

  // Normalize query string parameters for direct passing
  // into Pouch queries.
  for (prop in req.query) {
    try {
      req.query[prop] = JSON.parse(req.query[prop]);
    } catch (e) {}
  }
  // Custom bodyParsing because express.bodyParser() chokes
  // on 'malformed' requests, and also because we need the
  // rawBody for attachments
  rawBody(req, {
    length: req.headers['content-length'],
    encoding: 'binary'
  }, function (err, string) {
    if (err)
      return next(err)

    req.rawBody = string
    try {
      req.body = JSON.parse(string.toString('utf8'))
    } catch (err) {}
    next()
  })
});
app.use(function (req, res, next) {
  var _res = res;
  var send = res.send;
  res.send = function() {
    var args = Array.prototype.slice.call(arguments).map(function (arg) {
      if (typeof arg === 'object' && isPouchError(arg)) {
        var _arg = {
          error: arg.name,
          reason: arg.message
        };
        return _arg;
      }
      return arg;
    });
    send.apply(_res, args);
  };
  next();
});


// Root route, return welcome message
app.get('/', function (req, res, next) {
  res.send(200, {
    'express-pouchdb': 'Welcome!',
    'version': pkg.version
  });
});

app.get('/_session', function (req, res, next) {
  res.send({"ok":true,"userCtx":{"name":null,"roles":["_admin"]},"info":{}});
});

app.get('/_utils', function (req, res, next) {
  res.sendfile(__dirname + '/fauxton/index.html');
});

// Generate UUIDs
app.get('/_uuids', function (req, res, next) {
  var count = typeof req.query.count === 'number' ? req.query.count : 1;
  res.send(200, {
    uuids: uuids(count)
  });
});

// List all databases.
app.get('/_all_dbs', function (req, res, next) {
  Pouch.allDbs(function (err, response) {<commit_msg>(#55) - stub out _config<commit_after>
var express   = require('express')
  , rawBody   = require('raw-body')
  , fs        = require('fs')
  , path      = require('path')
  , extend    = require('extend')
  , pkg       = require('./package.json')
  , dbs       = {}
  , uuids     = require('./uuids')
  , histories = {}
  , app       = express();

var Pouch;
module.exports = function(PouchToUse) {
  Pouch = PouchToUse;
  require('pouchdb-all-dbs')(Pouch);
  return app;
};

function isPouchError(obj) {
  return obj.error && obj.error === true;
}

app.use('/js', express.static(__dirname + '/fauxton/js'));
app.use('/css', express.static(__dirname + '/fauxton/css'));
app.use('/img', express.static(__dirname + '/fauxton/img'));

app.use(function (req, res, next) {
  var opts = {}
    , data = ''
    , prop;

  // Normalize query string parameters for direct passing
  // into Pouch queries.
  for (prop in req.query) {
    try {
      req.query[prop] = JSON.parse(req.query[prop]);
    } catch (e) {}
  }
  // Custom bodyParsing because express.bodyParser() chokes
  // on 'malformed' requests, and also because we need the
  // rawBody for attachments
  rawBody(req, {
    length: req.headers['content-length'],
    encoding: 'binary'
  }, function (err, string) {
    if (err)
      return next(err)

    req.rawBody = string
    try {
      req.body = JSON.parse(string.toString('utf8'))
    } catch (err) {}
    next()
  })
});
app.use(function (req, res, next) {
  var _res = res;
  var send = res.send;
  res.send = function() {
    var args = Array.prototype.slice.call(arguments).map(function (arg) {
      if (typeof arg === 'object' && isPouchError(arg)) {
        var _arg = {
          error: arg.name,
          reason: arg.message
        };
        return _arg;
      }
      return arg;
    });
    send.apply(_res, args);
  };
  next();
});


// Root route, return welcome message
app.get('/', function (req, res, next) {
  res.send(200, {
    'express-pouchdb': 'Welcome!',
    'version': pkg.version
  });
});

app.get('/_session', function (req, res, next) {
  res.send({"ok":true,"userCtx":{"name":null,"roles":["_admin"]},"info":{}});
});

app.get('/_utils', function (req, res, next) {
  res.sendfile(__dirname + '/fauxton/index.html');
});

// Config (stub for now)
app.get('/_config', function (req, res, next) {
  res.send(200, {
    facts: { 
        "pouchdb-server has no config": true,
        "if you use pouchdb-server, you are awesome": true
      }
  });
});

app.put('/_config/:key/:value(*)', function (req, res, next) {
  res.send(200, {ok: true, 'pouchdb-server has no config': true});
});

// Generate UUIDs
app.get('/_uuids', function (req, res, next) {
  var count = typeof req.query.count === 'number' ? req.query.count : 1;
  res.send(200, {
    uuids: uuids(count)
  });
});

// List all databases.
app.get('/_all_dbs', function (req, res, next) {
  Pouch.allDbs(function (err, response) {
{'fraction_non_alphanumeric': 0.18826094857350537, 'fraction_numerical': 0.004361257495911321, 'mean_word_length': 2.870604781997187, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '150508', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1909, 'n_words': 436}