cozy-proxy
Version:
Cozy Proxy redirects requests properly to the right application of the Cozy platform depending on given path. It also handles authentication to the Cozy for users and devices.
82 lines (64 loc) • 2.23 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var JsonClient, StatusChecker, User, async, controllerUrl, couchUrl, dataSystemUrl, homeUrl, proxyUrl, urlHelper;
async = require('async');
urlHelper = require('cozy-url-sdk');
JsonClient = require('request-json').JsonClient;
User = require('../models/user');
couchUrl = urlHelper.couch.url() + "/";
controllerUrl = urlHelper.controller.url() + "/";
dataSystemUrl = urlHelper.dataSystem.url() + "/";
homeUrl = urlHelper.home.url() + "/";
proxyUrl = urlHelper.proxy.url() + "/";
StatusChecker = (function() {
function StatusChecker() {}
StatusChecker.prototype.client = new JsonClient();
StatusChecker.prototype.status = {
couchdb: false,
datasystem: false,
controller: false,
home: false,
registered: false
};
StatusChecker.prototype.checkAllStatus = function(callback) {
var field, ref, value;
ref = this.status;
for (field in ref) {
value = ref[field];
this.status[field] = false;
}
return async.series([this.getChecker("couchdb", couchUrl), this.getChecker("controller", controllerUrl), this.getChecker("datasystem", dataSystemUrl), this.getChecker("home", homeUrl), this.getChecker("proxy", proxyUrl, "routes")], (function(_this) {
return function() {
return User.first(function(err, user) {
if ((user == null) || err) {
return callback(null, _this.status);
} else {
_this.status.registered = true;
return callback(null, _this.status);
}
});
};
})(this));
};
StatusChecker.prototype.getChecker = function(app, url, path) {
if (path == null) {
path = "";
}
return (function(_this) {
return function(callback) {
_this.client.host = url;
return _this.client.get(path, function(err, res, body) {
var code;
if (res != null) {
code = res.statusCode;
_this.status[app] = code === 200 || code === 401 || code === 403;
} else {
_this.status[app] = false;
}
return callback();
}, false);
};
})(this);
};
return StatusChecker;
})();
module.exports = new StatusChecker();