forked from ckihneman/DestinyTrialsReport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
123 lines (112 loc) · 3.42 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
121
122
123
'use strict';
var compression = require('compression');
var express = require('express');
var subdomain = require('express-subdomain');
var request = require('request');
var throng = require('throng');
var siteCreators = JSON.parse(process.env.SITE_CREATORS);
var siteDonators = JSON.parse(process.env.SITE_DONATORS);
throng(start, {
workers: process.env.WEB_CONCURRENCY || 1,
lifetime: Infinity
});
function start() {
var app = express();
app.use(compression());
app.use(express.static(__dirname));
// Check devs and donators
app.use('/supporterStatus/:membershipId', function(req, res) {
var nonHazard = [];
var membershipId = req.params.membershipId;
if (membershipId) {
if (siteCreators.indexOf(membershipId) > -1) {
nonHazard.push('Site Developer');
}
if (siteDonators.indexOf(membershipId) > -1) {
nonHazard.push('Site Donator');
}
}
res.send(nonHazard);
});
app.use('/api/*?', function(req, res) {
if (req.headers[process.env.AUTH]) {
var options = {
url: 'http://' + process.env.API_URL + '/' + req.originalUrl.replace('/api/', ''),
headers: {'X-API-Key': process.env.BUNGIE_API_KEY, 'X-AUTH-Key': process.env.AUTH_KEY}
};
try {
request(options, function (error, response, body) {
if (!error && response.statusCode !== 503) {
var json = JSON.stringify(body);
res.send(JSON.parse(json));
} else {
res.send(error);
}
});
} catch (e) {}
} else {
res.send('Nope');
}
});
// Guardian.net API Proxy
app.use('/ggg/*?', function(req, res) {
var options = {
url: 'https://api.guardian.gg/' + req.originalUrl.replace('/ggg/', ''),
headers: {'X-API-Key': process.env.BUNGIE_API_KEY}
};
request(options, function (error, response, body) {
if (!error) {
res.send(JSON.parse(body));
} else {
res.send(error);
}
});
});
// DestinyTrialsReport
app.get('/:platform/:playerName', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/:platform/:playerOne/:playerTwo/:playerThree', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
// MyTrialsReport
var router = express.Router();
router.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
router.get('/:platform/:playerName', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.use(subdomain('my', router));
app.use(subdomain('opponents', router));
// Bungie API Proxy
app.use('/Platform/*?', function(req, res) {
if (req.headers[process.env.AUTH]) {
var options = {
url: 'https://www.bungie.net' + req.originalUrl,
headers: {'X-API-Key': process.env.BUNGIES_API_KEY}
};
try {
request(options, function (error, response, body) {
if (!error && response.statusCode !== 503) {
var json = JSON.stringify(body);
res.send(JSON.parse(json));
} else {
res.send(error);
}
});
} catch (e) {}
} else {
res.send('Nope');
}
});
// CORS
app.all('*', function (req, res) {
res.header('Access-Control-Allow-Origin', 'http://www.destinytrialsreport.com');
});
// Start server
var port = process.env.PORT || 9000;
app.listen(port, function () {
console.log('Listening on port ' + port);
});
}