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.
86 lines (73 loc) • 2.33 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var Client, Router, logger, urlHelper, util;
util = require('util');
Client = require('request-json').JsonClient;
urlHelper = require('cozy-url-sdk');
logger = require('printit')({
date: false,
prefix: 'lib:router'
});
Router = (function() {
Router.prototype.routes = {};
function Router() {
this.client = new Client(urlHelper.home.url());
}
Router.prototype.getRoutes = function() {
return this.routes;
};
Router.prototype.displayRoutes = function(callback) {
var ref, route, slug;
ref = this.routes;
for (slug in ref) {
route = ref[slug];
if (route.type === 'static') {
logger.info(slug + " (" + route.state + ") on type " + route.type);
} else {
logger.info(slug + " (" + route.state + ") on port " + route.port);
}
}
if (callback != null) {
return callback();
}
};
Router.prototype.reset = function(callback) {
logger.info('Start resetting routes...');
this.routes = {};
return this.client.get("api/applications/", (function(_this) {
return function(error, res, apps) {
var app, err, error1, i, len, ref;
if ((error != null) || (apps.error != null)) {
logger.error("Cannot retrieve applications list.");
logger.error(util.inspect(error) || apps.msg);
return callback(error || apps.msg);
}
try {
ref = apps.rows;
for (i = 0, len = ref.length; i < len; i++) {
app = ref[i];
_this.routes[app.slug] = {};
if (app.type === 'static') {
_this.routes[app.slug].type = app.type;
_this.routes[app.slug].path = app.path;
} else {
if (app.port != null) {
_this.routes[app.slug].port = app.port;
}
}
if (app.state != null) {
_this.routes[app.slug].state = app.state;
}
}
logger.info("Routes have been successfully reset.");
return callback();
} catch (error1) {
err = error1;
logger.error("Oops, something went wrong during routes reset.");
return callback(err);
}
};
})(this));
};
return Router;
})();
module.exports = new Router();