<commit_before>// app.js
var express = require('express');
var events  = require('events');
var http    = require('http');
var path    = require('path');
var routes  = require('./routes');

var compactModule = require('compact');

var auth   = require('./lib/auth-local.js');
var ensure = require('./lib/auth-ensure.js');
var limits = require('./lib/limits.js');
var errors = require('./lib/errors.js');
var db     = require('./lib/dataAccess.js').instance();
var notify = require('./lib/notify.js');

var sslServer = require('./lib/https-server.js');
var payment   = require('./lib/payment.js')();
var settings  = require('./lib/settings.js');

var usersRoutes = require('./routes/users');
var userRoutes 	= require('./routes/user');
var initRoutes 	= require('./routes/init');

var couchSessionStore = require('./lib/couch-session-store.js');

var ee = new events.EventEmitter();
var isReady = false;

var app = express();


// Middleware for data access
var guard = errors.guard;

var handle = function (res) {
	var fn = guard(res, function (data) {
		if (!data) {
			return res.send(204); // no content
		}
		res.send(200, data);
	}); 
	return fn;
};

var send = function (fn) {
	var middleware = function (req, res, next) {
		fn(handle(res));
	};

	return middleware;
};

var data = function (fn) {
	// A generic guard for callbacks. Call the
	// fn parameter. If there is an error, pass
	// it up to the error handler. Otherwise
	// append the result to the request object,
	// for the next middleware in line.
	var middleware = function (req, res, next) {
		fn(guard(res, function (data) {
			if (req.data) {
				// TODO: programmer error
			}
			req.data = data;
			next();
		}));
	};

	return middleware;
};

var tryToCreateHttpsServer = function (callback) {
	sslServer.create(app, callback);
};

var defineRoutes = function () {
	app.post('/auth/signin', auth.signin);
	app.get('/auth/signout', auth.signout);

	// Search engine things
	app.get('/sitemap.txt', routes.sitemap);

	// User routes (account actions. requires login access)
	app.get("/data/user", ensure.auth, userRoutes.user);
	app.put("/data/user/name", ensure.auth, userRoutes.updateName);
	app.put("/data/user/email", ensure.auth, userRoutes.updateEmail);
	app.put("/data/user/notificationEmail", ensure.auth, userRoutes.updateNotificationEmail)
	app.put("/data/user/password", ensure.auth, userRoutes.updatePassword);


	// User routes (circle actions. requires admin access)
	app.get("/data/:circleId/members", ensure.circleAdmin, function (req, res) {
		var circleId = req.params.circleId;
		db.users.findByCircleId(circleId, handle(res));
	});

	app.put("/data/:circleId/member/remove", ensure.circleAdmin, function (req, res) {
		var circleId = req.params.circleId;
		var reqUser = req.body;
		db.users.removeMembership(reqUser, circleId, handle(res));
	});

	app.post("/data/:circleId/member", ensure.circleAdmin, function (req, res) {
		var circleId = req.params.circleId;
		var member = req.body;
		db.users.addMembership(member, circleId, handle(res));
	});

	app.get("/data/:circleId/members/names", ensure.circle, function (req, res) {
		var circleId = req.params.circleId;
		db.users.findNamesByCircleId(circleId, handle(res));
	});

	// Init routes
	app.put("/data/initialize", initRoutes.init);


	// Settings!
	app.get("/data/settings", send(db.settings.get)); // public

	// TODO: This is not used. Assess.
	app.get("/data/settings/private", 
		ensure.mainframe, send(db.settings.getPrivate)); 

	app.get("/data/settings/authorized", 
		ensure.mainframe, send(db.settings.getAuthorized));

	app.put("/data/setting", ensure.mainframe, function (req, res) {
		var data = req.body;

		var invalidateSettingsCache = function () {
			app.set('settings', null);
		};

		var onSettingsUpdate = function (setting) {
			if (setting.name === 'ssl-key-path' || setting.name === 'ssl-cert-path') {
				// TODO: Tell the client if we started the server?
				tryToCreateHttpsServer();
			}
			if (setting.name === 'stripe-secret-key') {
				payment.setApiKey(setting.value);
			}

			invalidateSettingsCache();
			res.send(200);
		};
<commit_msg>Cache: 6 minutes: /data/settings<commit_after>// app.js
var express = require('express');
var events  = require('events');
var http    = require('http');
var path    = require('path');
var routes  = require('./routes');

var compactModule = require('compact');

var auth   = require('./lib/auth-local.js');
var ensure = require('./lib/auth-ensure.js');
var limits = require('./lib/limits.js');
var errors = require('./lib/errors.js');
var db     = require('./lib/dataAccess.js').instance();
var notify = require('./lib/notify.js');

var sslServer = require('./lib/https-server.js');
var payment   = require('./lib/payment.js')();
var settings  = require('./lib/settings.js');

var usersRoutes = require('./routes/users');
var userRoutes 	= require('./routes/user');
var initRoutes 	= require('./routes/init');

var couchSessionStore = require('./lib/couch-session-store.js');

var ee = new events.EventEmitter();
var isReady = false;

var app = express();


// Middleware for data access
var guard = errors.guard;

var handle = function (res) {
	var fn = guard(res, function (data) {
		if (!data) {
			return res.send(204); // no content
		}
		res.send(200, data);
	}); 
	return fn;
};

var send = function (fn) {
	var middleware = function (req, res, next) {
		fn(handle(res));
	};

	return middleware;
};

var data = function (fn) {
	// A generic guard for callbacks. Call the
	// fn parameter. If there is an error, pass
	// it up to the error handler. Otherwise
	// append the result to the request object,
	// for the next middleware in line.
	var middleware = function (req, res, next) {
		fn(guard(res, function (data) {
			if (req.data) {
				// TODO: programmer error
			}
			req.data = data;
			next();
		}));
	};

	return middleware;
};

// caching middleware
var cache = function (ms) {
	var fn = function (req, res, next) {
		res.setHeader("Cache-Control", "max-age=" + ms);
		next();
	};
	return fn;
};
var sixMinutes = 5 * 60;

var tryToCreateHttpsServer = function (callback) {
	sslServer.create(app, callback);
};

var defineRoutes = function () {
	app.post('/auth/signin', auth.signin);
	app.get('/auth/signout', auth.signout);

	// Search engine things
	app.get('/sitemap.txt', routes.sitemap);

	// User routes (account actions. requires login access)
	app.get("/data/user", ensure.auth, userRoutes.user);
	app.put("/data/user/name", ensure.auth, userRoutes.updateName);
	app.put("/data/user/email", ensure.auth, userRoutes.updateEmail);
	app.put("/data/user/notificationEmail", ensure.auth, userRoutes.updateNotificationEmail)
	app.put("/data/user/password", ensure.auth, userRoutes.updatePassword);


	// User routes (circle actions. requires admin access)
	app.get("/data/:circleId/members", ensure.circleAdmin, function (req, res) {
		var circleId = req.params.circleId;
		db.users.findByCircleId(circleId, handle(res));
	});

	app.put("/data/:circleId/member/remove", ensure.circleAdmin, function (req, res) {
		var circleId = req.params.circleId;
		var reqUser = req.body;
		db.users.removeMembership(reqUser, circleId, handle(res));
	});

	app.post("/data/:circleId/member", ensure.circleAdmin, function (req, res) {
		var circleId = req.params.circleId;
		var member = req.body;
		db.users.addMembership(member, circleId, handle(res));
	});

	app.get("/data/:circleId/members/names", ensure.circle, function (req, res) {
		var circleId = req.params.circleId;
		db.users.findNamesByCircleId(circleId, handle(res));
	});

	// Init routes
	app.put("/data/initialize", initRoutes.init);


	// Settings!
	app.get("/data/settings", cache(sixMinutes), send(db.settings.get)); // public

	// TODO: This is not used. Assess.
	app.get("/data/settings/private", 
		ensure.mainframe, send(db.settings.getPrivate)); 

	app.get("/data/settings/authorized", 
		ensure.mainframe, send(db.settings.getAuthorized));

	app.put("/data/setting", ensure.mainframe, function (req, res) {
		var data = req.body;

		var invalidateSettingsCache = function () {
			app.set('settings', null);
		};

		var onSettingsUpdate = function (setting) {
			if (setting.name === 'ssl-key-path' || setting.name === 'ssl-cert-path') {
				// TODO: Tell the client if we started the server?
				tryToCreateHttpsServer();
			}
			if (setting.name === 'stripe-secret-key') {
				payment.setApiKey(setting.value);
			}

			invalidateSettingsCache();
			res.send(200);
		};

{'fraction_non_alphanumeric': 0.17224711153029945, 'fraction_numerical': 0.0025937278943645365, 'mean_word_length': 3.8752873563218393, 'pattern_counts': {'":': 0, '<': 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': '1482540', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2774, 'n_words': 664}