forked from itteco/iframely
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
120 lines (90 loc) · 2.82 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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 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 errorHandler(err, req, res, next) {
if (err instanceof NotFound) {
res.writeHead(404);
res.end(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) {
code = 408;
}
res.writeHead(code);
res.end(err.message);
}
}
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";
app.listen(CONFIG.port);
if (CONFIG.ssl) {
var options = { key: CONFIG.ssl.key, cert: CONFIG.ssl.cert };
require('https').createServer(options, app).listen(CONFIG.ssl.port);
}
console.log('');
console.log('Iframely listening on port', CONFIG.port);
console.log(' - [email protected] - if you need help');
console.log(' - twitter.com/iframely - news & updates');
console.log(' - github.com/itteco/iframely - star & contribute');