<commit_before>const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const passport = require('passport');
const config = require('../../config.json');
const createRouteHandler = require('../express/promisehandler');
const acaoHandler = require('../express/accesscontrol');
const path = require('path');

const database = require('../database');

router.use(bodyParser.json());
router.use(acaoHandler(config));

const BasicStrategy = require('passport-http').BasicStrategy;

passport.use(new BasicStrategy(
  function(userid, password, done) {
    if (userid.trim() === 'apikey' && password.trim() === config.api_key.trim()) {
      done(null, {});
    } else {
      done(new Error("Invalid API key"));
    }
  })
);

router.post(/^\/v1\/channel\/?$/,
  passport.authenticate('basic', { session: false }),
  createRouteHandler((req, res) => {

  const requestBody = req.body;
  return database.createNewChannel(requestBody)
    .then((dbResponse) => {
      res.json(dbResponse);
    });
}));

router.get(/^\/v1\/channel\/?$/,
  createRouteHandler((req, res) => {

  const requestBody = req.body;
  return database.channels.read()
    .then((channels) => {
      res.set('Content-Range', `0-${channels.length}/${channels.length}`);
      res.json(channels);
    });
}));

router.post(/^\/v1\/channel\/(.*)\/beacons\/?$/,
  passport.authenticate('basic', { session: false }),
  createRouteHandler((req, res) => {

  const channelName = req.params[0];
  const requestBody = req.body;
  return database.createNewBeacon(channelName, requestBody)
    .then((beacons) => {
      res.json(beacons);
    });

}));


router.get(/^\/v1\/channel\/(.*)\/beacons\/?$/, createRouteHandler((req, res) => {
  const channelName = req.params[0];
  return database.getAllBeaconsForChannel(channelName)
    .then((beacons) => {
      res.set('Content-Range', `0-${beacons.length}/${beacons.length}`);
      res.json(beacons);
    });
}));

router.get(/^\/v1\/channel\/(.*)\/beacons\/(.*)\/location\/?$/, createRouteHandler((req, res) => {
  const channelName = req.params[0];
  const slug = req.params[1];

  return database.getBeaconInfo(slug, channelName)
    .then((response) => {
      res.json(response.location);
    });
}));

router.get(/^\/v1\/channel\/(.*)\/beacons\/(.*)\/?$/, createRouteHandler((req, res) => {
  const channelName = req.params[0];
  const slug = req.params[1];

  return database.getBeaconInfo(slug, channelName)
    .then((response) => {
      res.json(response);
    });
}));

router.patch(/^\/v1\/channel\/(.*)\/beacons\/(.*)\/?$/,
  passport.authenticate('basic', { session: false }),
  createRouteHandler((req, res) => {

  const channelName = req.params[0];
  const slug = req.params[1];
  const requestBody = req.body;

  return database.beacons.patch(slug, requestBody)
    .then((response) => {
      res.json(response);
    });
}));

router.get(/^\/v1\/search\/shortid\/([a-zA-Z0-9_\-~]*)\/?$/, createRouteHandler((req, res) => {
  const shortId = req.params[0];

  return database.getCanonicalUrlForShortId(shortId)
    .then((response) => {
      res.json(response);
    });
}));

router.get(/^\/v1\/search\/beacons\/(.*),(.*),(.*)\/?$/, createRouteHandler((req, res) => {
  const latitude = Number(req.params[0]);
  const longitude = Number(req.params[1]);
  const radius = Number(req.params[2]);

  return database.searchBeacons(latitude, longitude, radius)
    .then((dbResponse) => {
      res.json(dbResponse);
    });
}));

router.post(/^\/v1\/search\/slugs\/?$/, createRouteHandler((req, res) => {
  const requestBody = req.body;

  return database.searchSlugs(requestBody)
    .then((response) => {;
      res.json(response);
    });
}));

router.post(/^\/v1\/search\/url\/?$/,
  passport.authenticate('basic', { session: false }),
  createRouteHandler((req, res) => {

  const requestBody = req.body;

  return database.searchUrls(requestBody)
    .then((response) => {
      res.json(response)
    });<commit_msg>Remove authentication on the search/urls endpoint (#28)<commit_after>const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const passport = require('passport');
const config = require('../../config.json');
const createRouteHandler = require('../express/promisehandler');
const acaoHandler = require('../express/accesscontrol');
const path = require('path');

const database = require('../database');

router.use(bodyParser.json());
router.use(acaoHandler(config));

const BasicStrategy = require('passport-http').BasicStrategy;

passport.use(new BasicStrategy(
  function(userid, password, done) {
    if (userid.trim() === 'apikey' && password.trim() === config.api_key.trim()) {
      done(null, {});
    } else {
      done(new Error("Invalid API key"));
    }
  })
);

router.post(/^\/v1\/channel\/?$/,
  passport.authenticate('basic', { session: false }),
  createRouteHandler((req, res) => {

  const requestBody = req.body;
  return database.createNewChannel(requestBody)
    .then((dbResponse) => {
      res.json(dbResponse);
    });
}));

router.get(/^\/v1\/channel\/?$/,
  createRouteHandler((req, res) => {

  const requestBody = req.body;
  return database.channels.read()
    .then((channels) => {
      res.set('Content-Range', `0-${channels.length}/${channels.length}`);
      res.json(channels);
    });
}));

router.post(/^\/v1\/channel\/(.*)\/beacons\/?$/,
  passport.authenticate('basic', { session: false }),
  createRouteHandler((req, res) => {

  const channelName = req.params[0];
  const requestBody = req.body;
  return database.createNewBeacon(channelName, requestBody)
    .then((beacons) => {
      res.json(beacons);
    });

}));


router.get(/^\/v1\/channel\/(.*)\/beacons\/?$/, createRouteHandler((req, res) => {
  const channelName = req.params[0];
  return database.getAllBeaconsForChannel(channelName)
    .then((beacons) => {
      res.set('Content-Range', `0-${beacons.length}/${beacons.length}`);
      res.json(beacons);
    });
}));

router.get(/^\/v1\/channel\/(.*)\/beacons\/(.*)\/location\/?$/, createRouteHandler((req, res) => {
  const channelName = req.params[0];
  const slug = req.params[1];

  return database.getBeaconInfo(slug, channelName)
    .then((response) => {
      res.json(response.location);
    });
}));

router.get(/^\/v1\/channel\/(.*)\/beacons\/(.*)\/?$/, createRouteHandler((req, res) => {
  const channelName = req.params[0];
  const slug = req.params[1];

  return database.getBeaconInfo(slug, channelName)
    .then((response) => {
      res.json(response);
    });
}));

router.patch(/^\/v1\/channel\/(.*)\/beacons\/(.*)\/?$/,
  passport.authenticate('basic', { session: false }),
  createRouteHandler((req, res) => {

  const channelName = req.params[0];
  const slug = req.params[1];
  const requestBody = req.body;

  return database.beacons.patch(slug, requestBody)
    .then((response) => {
      res.json(response);
    });
}));

router.get(/^\/v1\/search\/shortid\/([a-zA-Z0-9_\-~]*)\/?$/, createRouteHandler((req, res) => {
  const shortId = req.params[0];

  return database.getCanonicalUrlForShortId(shortId)
    .then((response) => {
      res.json(response);
    });
}));

router.get(/^\/v1\/search\/beacons\/(.*),(.*),(.*)\/?$/, createRouteHandler((req, res) => {
  const latitude = Number(req.params[0]);
  const longitude = Number(req.params[1]);
  const radius = Number(req.params[2]);

  return database.searchBeacons(latitude, longitude, radius)
    .then((dbResponse) => {
      res.json(dbResponse);
    });
}));

router.post(/^\/v1\/search\/slugs\/?$/, createRouteHandler((req, res) => {
  const requestBody = req.body;

  return database.searchSlugs(requestBody)
    .then((response) => {;
      res.json(response);
    });
}));

router.post(/^\/v1\/search\/url\/?$/, createRouteHandler((req, res) => {

  const requestBody = req.body;

  return database.searchUrls(requestBody)
    .then((response) => {
      res.json(response)
    });
{'fraction_non_alphanumeric': 0.21827284105131414, 'fraction_numerical': 0.007008760951188986, 'mean_word_length': 3.6190751445086704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '5919641', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2696, 'n_words': 405}