<commit_before>/*
 * POST create a route
 */

var db = require('../db')

var mongo = db.mongo;
var mongo_db = db.mongo_db;

exports.create = function(req, res) {
  var name = req.params.name;
  mongo.open(function(err, client) {
    var db = client.db(mongo_db);
    db.collection('endpoints').count({ name: name }, function(err, found){
      if (!found) {
        var password = <PASSWORD>;
        db.collection('endpoints').insert({
          name: name,
          password: password, // TODO hash+salt
          contents: '#!/bin/sh\ntouch /tmp/cheshire'
        }, function(err, result) {
          res.send('200', 'Endpoint successfully created.');
        });
      } else {
        res.send(400, 'endpoint already exists'); // TODO is 400 the right response?
      }
      client.close();
    });
  });
};

exports.read = function(req, res) {
  var name = req.params.name;
  mongo.open(function(err, client) {
    var db = client.db(mongo_db);
    db.collection('endpoints').findOne({ name: name }, function(err, result){
      if (result) {
        res.send(200, result.contents);
      } else {
        res.send(404, 'echo Endpoint ' + name + ' not found');
      }
      client.close();
    });
  });
};

exports.update = function(req, res) {
  var name = req.params.name;
  var password = <PASSWORD>;
  var contents = req.body.contents;
  mongo.open(function(err, client) {
    var db = client.db(mongo_db);
    db.collection('endpoints').findOne({ name: name }, function(err, result){
      if (result.password != password) {
        client.close();
        res.send(403, 'Incorrect password for endpoint');
      } else {
        db.collection('endpoints').update(
          { name: name },
          { $set: { contents: contents } },
          function(err, result){
            client.close();
            res.send(200, 'okay');
        });
      }<commit_msg>Remove script after it is GETed once<commit_after>/*
 * POST create a route
 */

var db = require('../db')

var mongo = db.mongo;
var mongo_db = db.mongo_db;

exports.create = function(req, res) {
  var name = req.params.name;
  mongo.open(function(err, client) {
    var db = client.db(mongo_db);
    db.collection('endpoints').count({ name: name }, function(err, found){
      if (!found) {
        var password = <PASSWORD>;
        db.collection('endpoints').insert({
          name: name,
          password: password, // TODO hash+salt
          contents: '#!/bin/sh\ntouch /tmp/cheshire'
        }, function(err, result) {
          client.close();
          res.send('200', 'Endpoint successfully created.');
        });
      } else {
        client.close();
        res.send(400, 'endpoint already exists'); // TODO is 400 the right response?
      }
    });
  });
};

exports.read = function(req, res) {
  var name = req.params.name;
  mongo.open(function(err, client) {
    var db = client.db(mongo_db);
    db.collection('endpoints').findOne({ name: name }, function(err, result){
      if (result) {
        var contents = result.contents;
        db.collection('endpoints').update({
          name: name
        }, {
          $set: { contents: '' }
        },function(err, result) {
          res.send(200, contents);
          client.close();
        });
      } else {
        res.send(404, 'echo Endpoint ' + name + ' not found');
        client.close();
      }
    });
  });
};

exports.update = function(req, res) {
  var name = req.params.name;
  var password = <PASSWORD>;
  var contents = req.body.contents;
  mongo.open(function(err, client) {
    var db = client.db(mongo_db);
    db.collection('endpoints').findOne({ name: name }, function(err, result){
      if (result.password != password) {
        client.close();
        res.send(403, 'Incorrect password for endpoint');
      } else {
        db.collection('endpoints').update(
          { name: name },
          { $set: { contents: contents } },
          function(err, result){
            client.close();
            res.send(200, 'okay');
        });
      }
{'fraction_non_alphanumeric': 0.16198757763975155, 'fraction_numerical': 0.010434782608695653, 'mean_word_length': 2.2705117790414295, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4396040', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1238, 'n_words': 286}