kopia lustrzana https://github.com/c9/core
Merge pull request +9896 from c9/fix-c9-url
restructure not to be architect pluginpull/223/head
commit
9f2ecc3c3b
|
@ -6,20 +6,19 @@
|
|||
* @copyright 2015, Ajax.org B.V.
|
||||
*/
|
||||
|
||||
var plugin = module.exports = main;
|
||||
var assert = require("assert");
|
||||
|
||||
plugin.consumes = ["error.logger"];
|
||||
plugin.provides = ["urls"];
|
||||
|
||||
var errorLogger = mockErrorLogger;
|
||||
function getHost(req) {
|
||||
if (req.headers)
|
||||
return req.headers.host;
|
||||
|
||||
function main(options, imports, register) {
|
||||
errorLogger = imports["error.logger"];
|
||||
|
||||
register(null, {
|
||||
urls: plugin
|
||||
});
|
||||
if (req.host)
|
||||
return req.host;
|
||||
|
||||
if (req.url)
|
||||
return req.url.replace(/^https?:\/\/([^/]*).*/, "$1");
|
||||
|
||||
return req;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,13 +46,12 @@ function main(options, imports, register) {
|
|||
* The URL pattern of the target service. E.g., if we want to
|
||||
* construct the base URL of the API service, this might be https://api.$DOMAIN.
|
||||
*/
|
||||
plugin.getBaseUrl = function(req, sourceBaseUrlPattern, targetBaseUrlPattern) {
|
||||
var sourceHost = req.headers && req.headers.host
|
||||
|| req.host
|
||||
|| req.url && req.url.replace(/^https?:\/\/([^/]*).*/, "$1")
|
||||
|| req;
|
||||
function getBaseUrl(req, sourceBaseUrlPattern, targetBaseUrlPattern) {
|
||||
var sourceHost = getHost(req);
|
||||
|
||||
if (typeof sourceHost !== "string")
|
||||
throw new Error("Not a valid request object: " + req);
|
||||
|
||||
if (!sourceBaseUrlPattern)
|
||||
throw new Error("getBaseUrl() requires at least two arguments");
|
||||
|
||||
|
@ -64,24 +62,29 @@ plugin.getBaseUrl = function(req, sourceBaseUrlPattern, targetBaseUrlPattern) {
|
|||
.replace("$DOMAIN", "([^/]+)");
|
||||
var hostMatch = sourceHost.match(sourceHostMatcher);
|
||||
var targetHost;
|
||||
|
||||
if (hostMatch) {
|
||||
targetHost = hostMatch[1];
|
||||
}
|
||||
else {
|
||||
errorLogger.log(new Error("Could not construct URL: request host "
|
||||
+ sourceHost + " should match " + sourceBaseUrlPattern));
|
||||
console.error(new Error("Could not construct URL: request host " + sourceHost + " should match " + sourceBaseUrlPattern));
|
||||
|
||||
targetHost = "c9.io";
|
||||
}
|
||||
|
||||
|
||||
if (targetHost.match(/^(ide|vfs)./) && !targetBaseUrlPattern)
|
||||
errorLogger.log(new Error("Warning: no targetBaseUrlPattern specified, will stay at " + targetHost), { sourceBaseUrlPattern: sourceBaseUrlPattern });
|
||||
console.error(new Error("Warning: no targetBaseUrlPattern specified, will stay at " + targetHost), {
|
||||
sourceBaseUrlPattern: sourceBaseUrlPattern
|
||||
});
|
||||
|
||||
if (targetHost.match(/^(ide|vfs)./))
|
||||
console.trace("Warning: possibly incorrect baseUrl constructed, with 'ide.' in the hostname: " + targetHost);
|
||||
console.trace("Warning: possibly incorrect baseUrl constructed, with 'ide.' in the hostname: " + targetHost);
|
||||
|
||||
return replaceDomain(targetBaseUrlPattern || sourceBaseUrlPattern, targetHost)
|
||||
.replace(/\/$/, "");
|
||||
};
|
||||
}
|
||||
|
||||
plugin.replaceDomains = function(settings, domains) {
|
||||
function replaceDomains(settings, domains) {
|
||||
domains = Array.isArray(domains) ? domains : domains.split(",");
|
||||
var primaryDomain = domains[0];
|
||||
settings.domains = domains;
|
||||
|
@ -103,14 +106,13 @@ plugin.replaceDomains = function(settings, domains) {
|
|||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function replaceDomain(url, domain) {
|
||||
return url.replace("$DOMAIN", domain);
|
||||
}
|
||||
|
||||
function mockErrorLogger() {}
|
||||
mockErrorLogger.log = function(err) {
|
||||
console.error(err.stack);
|
||||
};
|
||||
plugin.mockErrorLogger = mockErrorLogger;
|
||||
module.exports = {
|
||||
replaceDomains: replaceDomains,
|
||||
getBaseUrl: getBaseUrl,
|
||||
};
|
Ładowanie…
Reference in New Issue