-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
128 lines (111 loc) · 3.37 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
var endpointChanged = false;
module.exports = function(r, ip2asn, endpoint) {
var Promise = require('bluebird'),
express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
jsonParser = bodyParser.json({limit: '55mb'}),
passport = require('passport'),
config = require('./config'),
cors = require('cors'),
jwt = require('jwt-simple'),
Queue = require('rethinkdb-job-queue'),
app = express(),
discover = require('./lib/discover'),
rx = require('./api/rx'),
elasticsearch = require('elasticsearch'),
authentication = require('./api/authentication'),
read = require('./api/read'),
write = require('./api/write'),
relay = require('./api/relay'),
upload = require('./api/upload'),
safe = require('./api/safe');
app.use(passport.initialize());
if (!!config.behindProxy) {
app.enable('trust proxy');
app.set('trust proxy', 'loopback, linklocal, uniquelocal');
}
if (!!config.graylog) {
app.use(require('express-bunyan-logger')({
name: 'API',
streams: [{
type: 'raw',
stream: require('gelf-stream').forBunyan(config.graylog.host, config.graylog.port)
}]
}));
}else{
app.use(require('express-bunyan-logger')({
name: 'API'
}));
}
require('./lib/auth')(config, passport, r);
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors({
maxAge: 86400
}));
var messageQ = new Queue(config.rethinkdb, {
name: 'jobQueue',
// This is not a master queue
masterInterval: false,
changeFeed: false
});
app.use(function(req, res, next){
req.r = r;
req.Q = messageQ;
req.config = config;
req.ip2asn = ip2asn;
if (!config.elasticsearch) {
req.elasticsearch = null
}else{
req.elasticsearch = new elasticsearch.Client({
host: config.elasticsearch + ':9200',
requestTimeout: 1000 * 60 * 5
});
}
next();
});
app.use('/healthz', function(req, res, next) {
var r = req.r;
if (endpointChanged) {
return next(new Error('Endpoint changed.'))
}
Promise.all([
discover(),
r.table('domains', {
readMode: 'majority'
}).run(r.conn)
]).spread(function(ip, domains) {
if (endpoint !== false && ip !== endpoint) {
endpointChanged = true;
throw new Error('Endpoint changed.')
}
return res.status(200).send('ok')
}).catch(function(e) {
next(e)
})
});
var version = '/v' + config.apiVersion;
app.use(version + '/login', jsonParser, authentication);
app.use(version + '/rx', jsonParser, rx);
app.use(version + '/read', jsonParser, read);
app.use(version + '/write', jsonParser, write);
app.use(version + '/relay', jsonParser, relay);
app.use(version + '/upload', upload);
app.use(version + '/safe', safe);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.status(200).send({ok: true, message: 'Dermail API v2 (v' + require(__dirname + '/version.json') + ')'});
});
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
req.log.error(err);
res.status(err.status || 500);
res.send({
ok: false,
errName: err.name,
message: err.message
});
});
return app;
};