forked from itteco/iframely
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'iloire-feat/143-add-custom-plugins' into develop
- Loading branch information
Showing
9 changed files
with
344 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.