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.
88 lines (75 loc) • 2.53 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var appManager, forwardRequest, getPathForStaticApp, getProxy, lockedpath, logger, send, urlHelper;
appManager = require('../lib/app_manager');
getProxy = require('../lib/proxy').getProxy;
urlHelper = require('cozy-url-sdk');
send = require('send');
lockedpath = require('lockedpath');
logger = require('printit')({
date: false,
prefix: 'controllers:applications'
});
getPathForStaticApp = function(appName, path, root, callback) {
logger.info("Starting static app " + appName);
if (path === '/' || path === '/public/') {
path += 'index.html';
}
return callback(lockedpath(root).join(path));
};
forwardRequest = function(req, res, errTemplate, next) {
var appHost, appName, appSchema, shouldStart, urlHelperSlug;
appName = req.params.name;
urlHelperSlug = appName.replace('data-system', 'dataSystem');
appSchema = 'http';
appHost = 'localhost';
if (urlHelper[urlHelperSlug]) {
appSchema = urlHelper[urlHelperSlug].schema();
appHost = urlHelper[urlHelperSlug].host();
}
shouldStart = -1 === req.url.indexOf('socket.io');
return appManager.ensureStarted(appName, shouldStart, function(err, result) {
var error, url;
if (!res.connection || res.connection.destroyed) {
} else if (err != null) {
error = new Error(err.msg);
error.status = err.code;
error.template = errTemplate(err);
return next(error);
} else if (result.type === 'static') {
return getPathForStaticApp(appName, req.url, result.path, function(url) {
return send(req, url).pipe(res);
});
} else {
url = appSchema + "://" + appHost + ":" + result.port;
return getProxy().web(req, res, {
target: url
});
}
});
};
module.exports.app = function(req, res, next) {
var appName, errTemplate;
appName = req.params.name;
req.url = req.url.substring(("/apps/" + appName).length);
errTemplate = function(err) {
return {
name: err.code === 404 ? 'not_found' : 'error_app'
};
};
return forwardRequest(req, res, errTemplate, next);
};
module.exports.publicApp = function(req, res, next) {
var appName, errTemplate;
appName = req.params.name;
req.url = req.url.substring(("/public/" + appName).length);
req.url = "/public" + req.url;
errTemplate = function(err) {
return {
name: 'error_public'
};
};
return forwardRequest(req, res, errTemplate, next);
};
module.exports.appWithSlash = function(req, res) {
return res.redirect(req.url + "/");
};