-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
90 lines (68 loc) · 2.63 KB
/
Copy pathserver.js
File metadata and controls
90 lines (68 loc) · 2.63 KB
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
/*
Aaron Rest Server (aaron-rest-server)
Copyright 2015 Emil Hemdal <emilAThemdal.se>
Copyright 2015 Landstinget Dalarna Bibliotek och informationscentral
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/>.
*/
'use strict';
var restify = require('restify');
var cp = require('restify-cookies');
var mysql = require('mysql');
var qs = require('querystring');
var os = require('os');
var util = require('util');
var url = require('url');
var serverLog = util.debuglog('server');
var config = require('./config');
var pool = mysql.createPool(config.mysql);
serverLog('Loading routes!');
var user = require('./routes/user');
var auth = require('./routes/authenticate');
var test = require('./routes/test');
var perm = require('./routes/permissions');
var orga = require('./routes/organization');
serverLog('Starting restify server!');
var server = restify.createServer();
server.pre(restify.pre.userAgentConnection()); // Used to make curl return right away with HEAD methods.
server.use(restify.queryParser());
server.use(cp.parse);
server.use(restify.bodyParser({
maxBodySize: 0,
mapParams: true,
mapFiles: false,
overrideParams: false,
uploadDir: os.tmpdir(),
multiples: true,
hash: 'sha1'
}));
server.use(function(req, res, next) {
// TODO check GET arguments
//console.log(req.body);
//serverLog('req.body was', req.body);
var parsedURL;
if(req.headers.origin) {
parsedURL = url.parse(req.headers.origin);
if(config.http.domains.indexOf(parsedURL.host) !== -1) {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
}
}
next();
});
serverLog('Activating routes!');
auth.activateRoute(server, pool); // I know checkAuth already! :)
user.activateRoute(server, pool, auth.checkAuth);
test.activateRoute(server, pool, auth.checkAuth);
perm.activateRoute(server, pool, auth.checkAuth);
orga.activateRoute(server, pool, auth.checkAuth);
server.listen(22766, function() {
console.log('%s listening at %s', server.name, server.url);
});