<commit_before>#!/usr/bin/env node
// Licensed under the Apache 2.0 License. See footer for details.

var express = require('express'),
    http = require('http'),
    path = require('path'),
    cloudant = require('cloudant'),
    program = require('commander'),
    dotenv = require('dotenv'),
    httpProxy = require('http-proxy'),
    url = require('url'),
    pkg = require(path.join(__dirname, 'package.json'));

dotenv.load();

var app = express();

(function(app) {
  if (process.env.VCAP_SERVICES) {
    var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
    if (vcapServices.cloudantNoSQLDB) {
      vcapServices.cloudantNoSQLDB.map(function(service) {
        if (service.name && service.credentials) {
          app.set(service.name, cloudant({
            username: service.credentials.username,
            password: <PASSWORD>,
            account: service.credentials.username
          }));
        }
      });
    }
  }
})(app);

program
  .version(pkg.version)
  .option('-p, --port <port>', 'port on which to listen (defaults to 3000)', parseInt);

var createServer = true;
program
  .command('db <method>')
  .description('create (put) or delete the database')
  .action(function(method, options) {
    createServer = false;
    switch (method) {
      case 'put':
        app.get('cloudant-location-tracker-db').db.create('location-tracker', function(err, body) {
          if (!err) {
            console.log('Database created');
          } else {
            if (412 == err.status_code) {
              console.log('Database already exists');
            } else {
              console.error('Error creating database');
            }
          }
        });
        break;
      case 'delete':
        app.get('cloudant-location-tracker-db').db.destroy('location-tracker', function(err, body) {
          if (!err) {
            console.log('Database deleted');
          } else {
            if (404 == err.status_code) {
              console.log('Database does not exist');
            } else {
              console.error('Error deleting database');
            }
          }
        });
        break;
    }
  }).on('--help', function() {
    console.log('  Examples:');
    console.log();
    console.log('    $ db put');
    console.log('    $ db delete');
    console.log();
  });

program.parse(process.argv);

if (createServer) {
  // Proxy requests for `/api/` to the Cloudant database server
  var apiProxy = httpProxy.createProxyServer();
  app.all('/api/*', function(req, res) {
    if (!app.get('cloudant-location-tracker-db')) {
      return res.status(500).json({ error: 'No database server configured' })<commit_msg>Provide command to set initial database permissions<commit_after>#!/usr/bin/env node
// Licensed under the Apache 2.0 License. See footer for details.

var express = require('express'),
    http = require('http'),
    path = require('path'),
    cloudant = require('cloudant'),
    program = require('commander'),
    dotenv = require('dotenv'),
    httpProxy = require('http-proxy'),
    url = require('url'),
    pkg = require(path.join(__dirname, 'package.json'));

dotenv.load();

var app = express();

(function(app) {
  if (process.env.VCAP_SERVICES) {
    var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
    if (vcapServices.cloudantNoSQLDB) {
      vcapServices.cloudantNoSQLDB.map(function(service) {
        if (service.name && service.credentials) {
          app.set(service.name, cloudant({
            username: service.credentials.username,
            password: <PASSWORD>,
            account: service.credentials.username
          }));
        }
      });
    }
  }
})(app);

program
  .version(pkg.version)
  .option('-p, --port <port>', 'port on which to listen (defaults to 3000)', parseInt);

var createServer = true;
program
  .command('db <method>')
  .description('create (put) or delete the database')
  .action(function(method, options) {
    createServer = false;
    switch (method) {
      case 'put':
        app.get('cloudant-location-tracker-db').db.create('location-tracker', function(err, body) {
          if (!err) {
            console.log('Database created');
          } else {
            if (412 == err.status_code) {
              console.log('Database already exists');
            } else {
              console.error('Error creating database');
            }
          }
        });
        break;
      case 'delete':
        app.get('cloudant-location-tracker-db').db.destroy('location-tracker', function(err, body) {
          if (!err) {
            console.log('Database deleted');
          } else {
            if (404 == err.status_code) {
              console.log('Database does not exist');
            } else {
              console.error('Error deleting database');
            }
          }
        });
        break;
    }
  }).on('--help', function() {
    console.log('  Examples:');
    console.log();
    console.log('    $ db put');
    console.log('    $ db delete');
    console.log();
  });

program
  .command('api <endpoint>')
  .description('Call an API endpoint with preconfigured settings')
  .action(function(endpoint, options) {
    createServer = false;
    switch (endpoint) {
      case 'set_permissions':
        app.get('cloudant-location-tracker-db').set_permissions({
          database: 'location-tracker',
          username: 'nobody',
          roles: ['_reader']
        }, function(err, result) {
          if (!err) {
            console.log('Permissions set');
          } else {
            console.error('Error setting permission');
          }
        });
        break;
    }
  }).on('--help', function() {
    console.log('  Examples:');
    console.log();
    console.log('    $ api set_permissions');
    console.log();
  });

program.parse(process.argv);

if (createServer) {
  // Proxy requests for `/api/` to the Cloudant database server
  var apiProxy = httpProxy.createProxyServer();
  app.all('/api/*', function(req, res) {
    if (!app.get('cloudant-location-tracker-db')) {
      return res.status(500).json({ error: 'No database server configured' })
{'fraction_non_alphanumeric': 0.15424751345181803, 'fraction_numerical': 0.004891570194032285, 'mean_word_length': 2.3391399020141534, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '6654263', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1775, 'n_words': 393}