Skip to content

Commit

Permalink
Merge branch 'iloire-feat/143-add-custom-plugins' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nleush committed Nov 3, 2016
2 parents b16c3f6 + 3af6c89 commit ec273e7
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 161 deletions.
154 changes: 154 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
var sysUtils = require('./utils');

console.log("");
console.log("Starting Iframely...");
console.log("Base URL for embeds that require hosted renders:", CONFIG.baseAppUrl);

var path = require('path');
var express = require('express');
var jsonxml = require('jsontoxml');

var NotFound = sysUtils.NotFound;

var app = express();

app.use(express.bodyParser());
app.set('view engine', 'ejs');

if (CONFIG.allowedOrigins) {
app.use(function(req, res, next) {
var origin = req.headers["origin"];

if (origin) {
if (CONFIG.allowedOrigins.indexOf('*') > -1) {
res.setHeader('Access-Control-Allow-Origin', '*');
} else {
if (CONFIG.allowedOrigins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
}
}
next();
});
}
app.disable( 'x-powered-by' );
app.use(function(req, res, next) {
res.setHeader('X-Powered-By', 'Iframely');
next();
});

app.use(sysUtils.cacheMiddleware);


require('./modules/api/views')(app);
require('./modules/debug/views')(app);
require('./modules/tests-ui/views')(app);

app.use(logErrors);
app.use(errorHandler);


function logErrors(err, req, res, next) {
if (CONFIG.RICH_LOG_ENABLED) {
console.error(err.stack);
} else {
console.log(err.message);
}

next(err);
}

var errors = [401, 403, 408];

function respondWithError(req, res, code, msg) {
var err = {
error: {
source: 'iframely',
code: code,
message: msg
}
};

var ttl;
if (code === 404) {
ttl = CONFIG.CACHE_TTL_PAGE_404;
} else if (code === 408) {
ttl = CONFIG.CACHE_TTL_PAGE_TIMEOUT;
} else {
ttl = CONFIG.CACHE_TTL_PAGE_OTHER_ERROR
}

if (req.query.format === 'xml') {

var xmlError = jsonxml(err, {
escape: true,
xmlHeader: {
standalone: true
}
});

res.sendCached('text/xml', xmlError, {
code: code,
ttl: ttl
});

} else {

res.sendJsonCached(err, {
code: code,
ttl: ttl
});
}
}

function errorHandler(err, req, res, next) {
if (err instanceof NotFound) {
respondWithError(req, res, 404, err.message);
} else {
var code = err.code || 500;
errors.map(function(e) {
if (err.message.indexOf(e) > - 1) {
code = e;
}
});

if (err.message.indexOf('timeout') > -1) {
respondWithError(req, res, 408, 'Timeout');
}
else if (code === 401) {
respondWithError(req, res, 401, 'Unauthorized');
}
else if (code === 403) {
respondWithError(req, res, 403, 'Forbidden');
}
else if (code === 410) {
respondWithError(req, res, 410, 'Gone');
}
else {
respondWithError(req, res, code, 'Server error');
}
}
}

process.on('uncaughtException', function(err) {
if (CONFIG.DEBUG) {
console.log(err.stack);
} else {
console.log(err.message);
}
});

app.get(CONFIG.relativeStaticUrl + '/*', function(req, res, next) {
var url = '/' + req.url.split('/').splice(2).join('/');
sysUtils.static(path.resolve(__dirname, 'static'), {path: url})(req, res, next);
});

app.get('/', function(req, res) {
res.writeHead(302, { Location: 'http://iframely.com'});
res.end();
});

process.title = "iframely";
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

module.exports = app;
3 changes: 3 additions & 0 deletions config.local.js.SAMPLE
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
(function() {
var config = {

// Specify a path for custom plugins. Custom plugins will override core plugins.
// CUSTOM_PLUGINS: __dirname + '/yourcustom-plugin-folder',

DEBUG: false,
RICH_LOG_ENABLED: false,

Expand Down
19 changes: 19 additions & 0 deletions config.test_with_custom_plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(function() {

var config = {

CUSTOM_PLUGINS_PATH: __dirname + '/test/fixtures/custom-plugins',

DEBUG: true,
RICH_LOG_ENABLED: true,

baseAppUrl: "https://localhost",
port: 8080,

RESPONSE_TIMEOUT: 1 * 100, //ms

BLACKLIST_DOMAINS_RE: /blacklisted.*/
};

module.exports = config;
})();
22 changes: 20 additions & 2 deletions lib/loader/pluginLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,13 @@
// ID.
pluginDeclaration.id = getFileName(bits[bits.length - 1]).toLowerCase();
if (pluginDeclaration.id in plugins) {
console.error("Duplicate plugin id (filename)", pluginPath);
return;
if (CONFIG.CUSTOM_PLUGINS_PATH && pluginPath.indexOf(CONFIG.CUSTOM_PLUGINS_PATH) > -1) {
console.error("Plugin '" + pluginDeclaration.id + "' overridden by", pluginPath);
delete plugins[pluginDeclaration.id]
} else {
console.error("Duplicate plugin id (filename)", pluginPath);
return;
}
}

// Normalize RE.
Expand Down Expand Up @@ -433,6 +438,19 @@
loadPlugins('plugins', 'meta');
loadPlugins('plugins', 'templates');

// TODO: if has multiple modules_listing - CUSTOM_PLUGINS_PATH will be loaded multiple times.
if (CONFIG.CUSTOM_PLUGINS_PATH) {
if (fs.existsSync(CONFIG.CUSTOM_PLUGINS_PATH)) {
loadPlugins(CONFIG.CUSTOM_PLUGINS_PATH, 'domains');
loadPlugins(CONFIG.CUSTOM_PLUGINS_PATH, 'custom');
loadPlugins(CONFIG.CUSTOM_PLUGINS_PATH, 'links');
loadPlugins(CONFIG.CUSTOM_PLUGINS_PATH, 'meta');
loadPlugins(CONFIG.CUSTOM_PLUGINS_PATH, 'templates');
} else {
console.warn('Custom plugin folder "' + CONFIG.CUSTOM_PLUGINS_PATH + '" not found.');
}
}

loadPlugins('lib', 'plugins', 'system');
loadPlugins('lib', 'plugins', 'validators');
}
Expand Down
163 changes: 7 additions & 156 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,158 +1,7 @@
var sysUtils = require('./utils');
var app = require('./app');

console.log("");
console.log("Starting Iframely...");
console.log("Base URL for embeds that require hosted renders:", CONFIG.baseAppUrl);

var path = require('path');
var express = require('express');
var jsonxml = require('jsontoxml');

var NotFound = sysUtils.NotFound;

var app = express();

app.use(express.bodyParser());
app.set('view engine', 'ejs');

if (CONFIG.allowedOrigins) {
app.use(function(req, res, next) {
var origin = req.headers["origin"];

if (origin) {
if (CONFIG.allowedOrigins.indexOf('*') > -1) {
res.setHeader('Access-Control-Allow-Origin', '*');
} else {
if (CONFIG.allowedOrigins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
}
}
next();
});
}
app.disable( 'x-powered-by' );
app.use(function(req, res, next) {
res.setHeader('X-Powered-By', 'Iframely');
next();
});

app.use(sysUtils.cacheMiddleware);


require('./modules/api/views')(app);
require('./modules/debug/views')(app);
require('./modules/tests-ui/views')(app);

app.use(logErrors);
app.use(errorHandler);


function logErrors(err, req, res, next) {
if (CONFIG.RICH_LOG_ENABLED) {
console.error(err.stack);
} else {
console.log(err.message);
}

next(err);
}

var errors = [401, 403, 408];

function respondWithError(req, res, code, msg) {
var err = {
error: {
source: 'iframely',
code: code,
message: msg
}
};

var ttl;
if (code === 404) {
ttl = CONFIG.CACHE_TTL_PAGE_404;
} else if (code === 408) {
ttl = CONFIG.CACHE_TTL_PAGE_TIMEOUT;
} else {
ttl = CONFIG.CACHE_TTL_PAGE_OTHER_ERROR
}

if (req.query.format === 'xml') {

var xmlError = jsonxml(err, {
escape: true,
xmlHeader: {
standalone: true
}
});

res.sendCached('text/xml', xmlError, {
code: code,
ttl: ttl
});

} else {

res.sendJsonCached(err, {
code: code,
ttl: ttl
});
}
}

function errorHandler(err, req, res, next) {
if (err instanceof NotFound) {
respondWithError(req, res, 404, err.message);
} else {
var code = err.code || 500;
errors.map(function(e) {
if (err.message.indexOf(e) > - 1) {
code = e;
}
});

if (err.message.indexOf('timeout') > -1) {
respondWithError(req, res, 408, 'Timeout');
}
else if (code === 401) {
respondWithError(req, res, 401, 'Unauthorized');
}
else if (code === 403) {
respondWithError(req, res, 403, 'Forbidden');
}
else if (code === 410) {
respondWithError(req, res, 410, 'Gone');
}
else {
respondWithError(req, res, code, 'Server error');
}
}
}

process.on('uncaughtException', function(err) {
if (CONFIG.DEBUG) {
console.log(err.stack);
} else {
console.log(err.message);
}
});

app.get(CONFIG.relativeStaticUrl + '/*', function(req, res, next) {
var url = '/' + req.url.split('/').splice(2).join('/');
sysUtils.static(path.resolve(__dirname, 'static'), {path: url})(req, res, next);
});

app.get('/', function(req, res) {
res.writeHead(302, { Location: 'http://iframely.com'});
res.end();
});

process.title = "iframely";
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

var listener = app.listen(process.env.PORT || CONFIG.port, process.env.HOST || CONFIG.host, function(){
console.log('\niframely is listening on ' + listener.address().address + ':' + listener.address().port + '\n');
var server = app.listen(process.env.PORT || CONFIG.port, process.env.HOST || CONFIG.host, function(){
console.log('\niframely is listening on ' + server.address().address + ':' + server.address().port + '\n');
});

if (CONFIG.ssl) {
Expand All @@ -167,8 +16,10 @@ console.log(' - github.com/itteco/iframely - star & contribute');
if (!CONFIG.DEBUG) {
var GracefulServer = require('graceful-cluster').GracefulServer;
new GracefulServer({
server: listener,
server: server,
log: sysUtils.log,
shutdownTimeout: CONFIG.SHUTDOWN_TIMEOUT
});
}
}

module.exports = server;
Loading

0 comments on commit ec273e7

Please sign in to comment.