forked from itteco/iframely
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
169 lines (136 loc) · 3.45 KB
/
app.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var sysUtils = require('./utils');
console.log("");
console.log("Starting Iframely...");
console.log("Base URL for embeds that require hosted renders:", CONFIG.baseAppUrl);
if (!CONFIG.baseAppUrl) {
console.warn('Warning: CONFIG.baseAppUrl not set, default value used');
}
var path = require('path');
var express = require('express');
var jsonxml = require('jsontoxml');
var NotFound = sysUtils.NotFound;
var app = express();
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);
}
function respondWithError(req, res, code, msg, messages) {
var err = {
error: {
source: 'iframely',
code: code,
message: msg
}
};
if (messages) {
err.error.messages = messages;
}
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
});
}
}
var proxyErrorCodes = [401, 403, 408];
function errorHandler(err, req, res, next) {
if (err instanceof NotFound) {
respondWithError(req, res, 404, err.message, err.messages);
} else {
var code = err.code || 500;
proxyErrorCodes.map(function(e) {
if (err.message.indexOf(e) > - 1) {
code = e;
}
});
var message = 'Server error';
if (code === 401) {
message = 'Unauthorized';
}
else if (code === 403) {
message = 'Forbidden';
}
else if (code === 404) {
message = 'Not found';
}
else if (code === 408) {
message = 'Timeout';
}
else if (code === 410) {
message = 'Gone';
}
else if (code === 415 || code === 417) {
message = err.message || 'Unsupported Media Type';
}
respondWithError(req, res, code, message, err.messages);
}
}
process.on('uncaughtException', function(err) {
if (CONFIG.DEBUG) {
console.log(err.stack);
} else {
console.log(err.message);
}
});
if (process.env.NODE_ENV !== 'test') {
// This code not compatible with 'supertest' (in e2e.js)
// Disabled for tests.
app.use(CONFIG.relativeStaticUrl, express.static('static'));
}
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;