Skip to content

Commit

Permalink
parent domain removed
Browse files Browse the repository at this point in the history
  • Loading branch information
generalhenry committed Jan 15, 2014
1 parent 45a6e62 commit 9923dab
Show file tree
Hide file tree
Showing 12 changed files with 1,139 additions and 1,204 deletions.
43 changes: 18 additions & 25 deletions lib/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var configs = require('./configs');
var containers = require('./models/containers');
var request = require('request');
var users = require('./models/users');
var error = require('./error');
function onFirstRun (cb) {
var week = new Date(Date.now() - 1000 * 60 * 60 * 24 * 7);
var unsavedAndWayExpired = {
Expand All @@ -11,10 +12,10 @@ function onFirstRun (cb) {
};
containers.remove(unsavedAndWayExpired, function (err) {
if (err) {
return cb(err);
cb(err);
} else {
console.log('PURGE VERY EXPIRED DB CONTAINERS');
return cb();
cb();
}
});
}
Expand All @@ -23,7 +24,7 @@ function hasRegisteredOwner (container) {
}
function getOwners (domain, containers, cb) {
var userIds = containers.map(function getOwnerId (container) {
return container.owner.toString();
container.owner.toString();
});
var query = { _id: { $in: userIds } };
var fields = {
Expand Down Expand Up @@ -62,21 +63,17 @@ function cleanupContainersNotIn (domain, whitelist, cb) {
method: 'POST',
json: whiteServicesTokens,
pool: false
}, function (err, res, body) {
if (err) {
return domain.emit('error', err);
}, domain.intercept(function (res, body) {
if (res.statusCode !== 200) {
cb({
status: 502,
message: 'whitelist not accepted by harbourmaster',
body: body
});
} else {
if (res.statusCode !== 200) {
return cb({
status: 502,
message: 'whitelist not accepted by harbourmaster',
body: body
});
} else {
return cb();
}
cb();
}
});
}));
}
], cb);
}
Expand All @@ -98,9 +95,9 @@ module.exports = function (req, res) {
var domain = req.domain;
users.findUser(domain, { _id: req.user_id }, domain.intercept(function (user) {
if (!user) {
cb({ message: 'permission denied: no user' });
cb(error(403, 'permission denied: no user'));
} else if (!user.isModerator) {
cb({ message: 'permission denied' });
cb(error(403, 'permission denied'));
} else {
containers.listSavedContainers(domain, function (containers) {
getOwners(domain, containers, domain.intercept(function () {
Expand All @@ -111,11 +108,7 @@ module.exports = function (req, res) {
}
}));
}
], function (err) {
if (err) {
return sendError(err);
} else {
return res.json(200, { message: 'successfuly sent prune request to harbourmaster and cleaned mongodb' });
}
});
], req.domain.intercept(function () {
res.json(200, { message: 'successfuly sent prune request to harbourmaster and cleaned mongodb' });
}));
};
44 changes: 18 additions & 26 deletions lib/domains.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
var configs = require('./configs');
var domain = require('domain');
var dockerExp = /^HTTP response code is (\d\d\d) which indicates an error: (.+)$/;
module.exports = function (parentDomain) {
return function (req, res, next) {
var d = domain.create();
req.domain = d;
if (parentDomain) {
parentDomain.add(d);
req.parentDomain = parentDomain;
}
d.add(req);
d.add(res);
d.on('error', function (e) {
if (parentDomain && configs.throwErrors && false) {
throw e;
} else if (e.message && dockerExp.test(e.message)) {
var parts = dockerExp.exec(e.message);
var code = parts[1];
var message = parts[2];
if (code >= 500) {
code = 502;
}
res.json(code, { message: message });
} else {
next(e);
module.exports = function (req, res, next) {
var d = domain.create();
req.domain = d;
d.add(req);
d.add(res);
d.on('error', function (e) {
if (e.message && dockerExp.test(e.message)) {
var parts = dockerExp.exec(e.message);
var code = parts[1];
var message = parts[2];
if (code >= 500) {
code = 502;
}
});
return d.run(next);
};
res.json(code, { message: message });
} else {
next(e);
}
});
d.run(next);
};
54 changes: 19 additions & 35 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,16 @@ var configs = require('./configs');
var domains = require('./domains');
var express = require('express');
var http = require('http');
var impexp = require('./rest/impexp');
var mongoose = require('mongoose');
var nodetime = require('nodetime');
var rollbar = require('rollbar');
var runnables = require('./rest/runnables');
var users = require('./rest/users');
var channels = require('./rest/channels');
var categories = require('./rest/categories');
var specifications = require('./rest/specifications');
var implementations = require('./rest/implementations');
var campaigns = require('./rest/campaigns');
var hour = 1000 * 60 * 60;
mongoose.connect(configs.mongo);
if (configs.rollbar) {
rollbar.init(configs.rollbar.key, configs.rollbar.options);
}
function App(configs, domain) {
function App() {
var self = this;
this.configs = configs;
this.domain = domain;
this.started = false;
this.create();
setTimeout(function () {
Expand All @@ -38,16 +28,12 @@ App.prototype.start = function (cb) {
cb();
} else {
this.listener = function (err) {
if (self.domain && configs.throwErrors) {
self.domain.emit('error', err);
} else {
self.stop(function () {
self.cleanup();
});
}
self.stop(function () {
self.cleanup();
});
};
process.on('uncaughtException', this.listener);
return this.server.listen(this.configs.port, this.configs.ipaddress || '0.0.0.0', function (err) {
return this.server.listen(configs.port, configs.ipaddress || '0.0.0.0', function (err) {
if (err) {
cb(err);
} else {
Expand Down Expand Up @@ -77,20 +63,20 @@ App.prototype.stop = function (cb) {
App.prototype.create = function () {
var self = this;
var app = express();
app.use(domains(this.domain));
app.use(domains);
if (configs.logExpress) {
app.use(express.logger());
}
app.use(express.json());
app.use(express.urlencoded());
app.use(users(this.domain));
app.use(impexp(this.domain));
app.use(runnables(this.domain));
app.use(channels(this.domain));
app.use(categories(this.domain));
app.use(specifications(this.domain));
app.use(implementations(this.domain));
app.use(campaigns(this.domain));
app.use(require('./rest/users'));
app.use(require('./rest/impexp'));
app.use(require('./rest/runnables'));
app.use(require('./rest/channels'));
app.use(require('./rest/categories'));
app.use(require('./rest/specifications'));
app.use(require('./rest/implementations'));
app.use(require('./rest/campaigns'));
app.use(app.router);
if (configs.nodetime) {
app.use(nodetime.expressErrorHandler());
Expand All @@ -102,13 +88,11 @@ App.prototype.create = function () {
if (configs.logErrorStack && false) {
console.log(err.stack);
}
if (!err.domain && configs.throwErrors && req.parentDomain) {
req.parentDomain.emit('error', err);
} else {
res.json(err.code || 500, {
message: err.msg || 'something bad happened :(',
error: err.message
});
res.json(err.code || 500, {
message: err.msg || 'something bad happened :(',
error: err.message
});
if (err.code === 500 || err.code === undefined) {
self.stop(function () {
self.cleanup();
});
Expand Down
84 changes: 40 additions & 44 deletions lib/rest/campaigns.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,46 @@ var domains = require('../domains');
var Email = require('email').Email;
var configs = require('../configs');
var mailchimpApi = configs.mailchimp != null ? new mailchimp.MailChimpAPI(configs.mailchimp.key) : void 0;
module.exports = function (parentDomain) {
var app = express();
app.use(domains(parentDomain));
if (mailchimpApi != null) {
Object.keys(configs.mailchimp.lists).forEach(function (list) {
app.post('/campaigns/' + list, function (req, res) {
var opts = {
id: configs.mailchimp.lists[list],
email_address: req.body.EMAIL,
merge_vars: req.body,
send_welcome: false,
update_existing: false,
double_optin: false
};
mailchimpApi.listSubscribe(opts, function (err) {
if (err) {
res.json(400, { message: err.message });
} else {
res.json(201, req.body);
}
});
var app = module.exports = express();
if (mailchimpApi != null) {
Object.keys(configs.mailchimp.lists).forEach(function (list) {
app.post('/campaigns/' + list, function (req, res) {
var opts = {
id: configs.mailchimp.lists[list],
email_address: req.body.EMAIL,
merge_vars: req.body,
send_welcome: false,
update_existing: false,
double_optin: false
};
mailchimpApi.listSubscribe(opts, function (err) {
if (err) {
res.json(400, { message: err.message });
} else {
res.json(201, req.body);
}
});
});
}
app.post('/request/improve', function (req, res, next) {
var requestEmailBody = 'Improve Description: \n[\n\t' + req.body.description + '\n]\n\n' + 'sender url: \n[\n\t' + req.body.url + '\n]';
var requestEmail = new Email({
from: '[email protected]',
to: '[email protected]',
cc: [
'[email protected]',
'[email protected]'
],
subject: 'New Improve Request',
body: requestEmailBody
});
requestEmail.send(function (err) {
if (!err) {
res.json({ response: 'thanks. Will get back to you soon.' });
} else {
console.error('ERROR sending new request mail', err);
res.json({ response: 'Sorry. Will get back to you soon.' });
}
});
});
return app;
};
}
app.post('/request/improve', function (req, res, next) {
var requestEmailBody = 'Improve Description: \n[\n\t' + req.body.description + '\n]\n\n' + 'sender url: \n[\n\t' + req.body.url + '\n]';
var requestEmail = new Email({
from: '[email protected]',
to: '[email protected]',
cc: [
'[email protected]',
'[email protected]'
],
subject: 'New Improve Request',
body: requestEmailBody
});
requestEmail.send(function (err) {
if (!err) {
res.json({ response: 'thanks. Will get back to you soon.' });
} else {
console.error('ERROR sending new request mail', err);
res.json({ response: 'Sorry. Will get back to you soon.' });
}
});
});
Loading

0 comments on commit 9923dab

Please sign in to comment.