forked from 3drepo/3drepo.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
80 lines (70 loc) · 2.65 KB
/
api.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
/**
* Copyright (C) 2014 3D Repo Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var express = require('express');
var app = express();
var routes = require('./routes.js')();
var config = require('./js/core/config.js');
var hostname = config.api_server.hostname;
// Attach the encoders to the router
var x3dom_encoder = require('./js/core/encoders/x3dom_encoder.js').route(routes);
var json_encoder = require('./js/core/encoders/json_encoder.js').route(routes);
//var html_encoder = require('./js/core/encoders/html_encoder.js').route(routes);
var src_encoder = require('./js/core/encoders/src_encoder.js').route(routes);
var img_encoder = require('./js/core/encoders/img_encoder.js').route(routes);
var bin_encoder = require('./js/core/encoders/bin_encoder.js').route(routes);
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var compress = require('compression');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');
if (!config.crossOrigin)
{
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
//res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.header('Access-Control-Allow-Credentials', true);
//res.header('Cache-Control', 'public, max-age=3600');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.sendStatus(200);
} else {
next();
}
}
}
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(compress());
if (!config.crossOrigin)
app.use(allowCrossDomain);
app.use(cookieParser(config.cookie_parser_secret));
app.use(expressSession({
secret: config.cookie_secret,
resave: false,
saveUninitialized: true,
cookie: {
domain: hostname,
path: "/" + config.api_server.host_dir,
httpOnly: false
}
}));
app.use('/', routes.router);
exports.app = app