<commit_before>var express = require('express')
var debug = require('debug')('cacheep:routes')
var router = express.Router()
var bodyParser = require('body-parser');

var REDIS_ENABLE = true;

var cache = require('./db')

if(REDIS_ENABLE) var redisdb = require('./redis-db')

var GUN = process.env.GUN || false;
if (GUN) {
	var gun = require('./gundb')
	var data = gun.get('data')
}

/* Router Settings */

router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));

router.all('*',function(req,res,next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
    res.header("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS");
    next();
});

router.get('/ping', (req, res) => {
	if (cache) res.sendStatus(200)
	else res.sendStatus(500)
})

/* 	SET DATA (POST)
	Set Key Value or Key {Object} with optional TTL parameter.
*/

router.post('/api/set/:key/:ttl?', (req, res) => {
  try {
	if (req.body) {
		if (req.params.ttl) {
		    cache.set(req.params.key, req.body, req.params.ttl);
		    if(REDIS_ENABLE) redisdb.set(req.params.key, JSON.stringify(req.body), 'EX', req.params.ttl/1000);
                }
		else {
		    cache.set(req.params.key, req.body);
		    if(REDIS_ENABLE) redisdb.set(req.params.key, JSON.stringify(req.body));
                }
		res.sendStatus(200)
	} else { res.sendStatus(500) }
  } catch(e) {
	console.log(e)
	res.sendStatus(500)
  }
})

/* 	SET DATA (GET)
	Set Key Value or Key {Object} with optional TTL parameter.
*/

router.get('/api/set/:key/:value', (req, res) => {
  try {
	cache.set(req.params.key, req.params.value);

	if(REDIS_ENABLE) redisdb.set(req.params.key, req.params.value);
	
	res.sendStatus(200)
  } catch(e) {
	console.log(e)
	res.sendStatus(500)
  }
})

router.get('/api/set/:key/:value/:ttl', (req, res) => {
  try {
	cache.set(req.params.key, req.params.value, req.params.ttl);

  	if(REDIS_ENABLE) redisdb.set(req.params.key, req.params.value, 'EX', req.params.ttl/1000);

	res.sendStatus(200)
  } catch(e) {
	console.log(e)
	res.sendStatus(500)
  }
})

/* 	GET DATA 
	Both of these will update the "recently used"-ness of the key. 
	They do what you think. maxAge is optional and overrides the cache maxAge option if provided.
*/

router.get('/api/get/:key/:value?', (req, res) => {
  try {
	if (req.params.value) res.send( (cache.get(req.params.key))[req.params.value] )
	else res.send(cache.get(req.params.key))

  } catch(e) {
	console.log(e)
	res.sendStatus(500);
  }
})

/* 	PEEK DATA 
	Returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.<commit_msg>Update routes.js<commit_after>var express = require('express')
var debug = require('debug')('cacheep:routes')
var router = express.Router()
var bodyParser = require('body-parser');

var REDIS_ENABLE = true;

var cache = require('./db')

if(REDIS_ENABLE) var redisdb = require('./redis-db')

var GUN = process.env.GUN || false;
if (GUN) {
	var gun = require('./gundb')
	var data = gun.get('data')
}

/* Router Settings */

router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));

router.all('*',function(req,res,next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
    res.header("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS");
    next();
});

router.get('/ping', (req, res) => {
	if (cache) res.sendStatus(200)
	else res.sendStatus(500)
})

/* 	SET DATA (POST)
	Set Key Value or Key {Object} with optional TTL parameter.
*/

router.post('/api/set/:key/:ttl?', (req, res) => {
  try {
	if (req.body) {
		if (req.params.ttl) {
		    cache.set(req.params.key, req.body, req.params.ttl);
		    if(REDIS_ENABLE) redisdb.set(req.params.key, JSON.stringify(req.body), 'EX', req.params.ttl/1000);
                }
		else {
		    cache.set(req.params.key, req.body);
		    if(REDIS_ENABLE) redisdb.set(req.params.key, JSON.stringify(req.body));
                }
		res.sendStatus(200)
	} else { res.sendStatus(500) }
  } catch(e) {
	console.log(e)
	res.sendStatus(500)
  }
})

/* 	SET DATA (GET)
	Set Key Value or Key {Object} with optional TTL parameter.
*/

router.get('/api/set/:key/:value', (req, res) => {
  try {
	cache.set(req.params.key, req.params.value);

	if(REDIS_ENABLE) redisdb.set(req.params.key, req.params.value);
	
	res.sendStatus(200)
  } catch(e) {
	console.log(e)
	res.sendStatus(500)
  }
})

router.get('/api/set/:key/:value/:ttl', (req, res) => {
  try {
	cache.set(req.params.key, req.params.value, req.params.ttl);

  	if(REDIS_ENABLE) redisdb.set(req.params.key, req.params.value, 'EX', req.params.ttl/1000);

	res.sendStatus(200)
  } catch(e) {
	console.log(e)
	res.sendStatus(500)
  }
})

/* 	GET DATA 
	Both of these will update the "recently used"-ness of the key. 
	They do what you think. maxAge is optional and overrides the cache maxAge option if provided.
*/

router.get('/api/get/:key/:value?', (req, res) => {
  try {
        if (req.params.value) res.send( (cache.get(req.params.key))[req.params.value] )
        else {
            var resp = {};
            var data = cache.get(req.params.key);
            if(data === undefined ) resp.blocked = false;
            else {
                resp.blocked = true;
                resp.data = data;
            }
            res.send(resp);
        }

  } catch(e) {
        console.log(e)
        res.sendStatus(500);
  }
})


/* 	PEEK DATA 
	Returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
{'fraction_non_alphanumeric': 0.17915138816134102, 'fraction_numerical': 0.013270473197136371, 'mean_word_length': 3.2842183994016456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '1300787', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2114, 'n_words': 428}