-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathweb.js
176 lines (149 loc) · 4.64 KB
/
web.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
170
171
172
173
174
175
176
var express = require('express');
var uuid = require('uuid');
var basicAuth = require('basic-auth');
var Analytics = require('analytics-node');
var nuts = require('../');
var fs = require('fs');
var https = require('https');
var app = express();
var apiAuth = {
username: process.env.API_USERNAME,
password: process.env.API_PASSWORD
};
var analytics = undefined;
var downloadEvent = process.env.ANALYTICS_EVENT_DOWNLOAD || 'download';
if (process.env.ANALYTICS_TOKEN) {
analytics = new Analytics(process.env.ANALYTICS_TOKEN);
}
// Set up for https termination
var key = "", cert = ""
if (process.env.HTTPS_KEYFILE !== 'undefined') {
try {
key = fs.readFileSync(process.env.HTTPS_KEYFILE);
} catch (e) {
if (e.code === 'ENOENT') {
console.log('Key file not found!');
} else {
throw e;
}
}
}
if (process.env.HTTPS_CERTFILE !== 'undefined') {
try {
cert = fs.readFileSync(process.env.HTTPS_CERTFILE);
} catch (e) {
if (e.code === 'ENOENT') {
console.log('Certificate file not found!');
} else {
throw e;
}
}
}
var https_options = {
key: key,
cert: cert
};
var myNuts = nuts.Nuts({
repository: process.env.GITHUB_REPO,
token: process.env.GITHUB_TOKEN,
username: process.env.GITHUB_USERNAME,
password: process.env.GITHUB_PASSWORD,
timeout: process.env.VERSIONS_TIMEOUT,
cache: process.env.VERSIONS_CACHE,
refreshSecret: process.env.GITHUB_SECRET,
proxyAssets: !Boolean(process.env.DONT_PROXY_ASSETS)
});
// Control access to API
myNuts.before('api', function(access, next) {
if (!apiAuth.username) return next();
function unauthorized() {
next(new Error('Invalid username/password for API'));
};
var user = basicAuth(access.req);
if (!user || !user.name || !user.pass) {
return unauthorized();
};
if (user.name === apiAuth.username && user.pass === apiAuth.password) {
return next();
} else {
return unauthorized();
};
});
// Log download
myNuts.before('download', function(download, next) {
console.log('download', download.platform.filename, "for version", download.version.tag, "on channel", download.version.channel, "for", download.platform.type);
next();
});
myNuts.after('download', function(download, next) {
console.log('downloaded', download.platform.filename, "for version", download.version.tag, "on channel", download.version.channel, "for", download.platform.type);
// Track on segment if enabled
if (analytics) {
var userId = download.req.query.user;
analytics.track({
event: downloadEvent,
anonymousId: userId? null : uuid.v4(),
userId: userId,
properties: {
version: download.version.tag,
channel: download.version.channel,
platform: download.platform.type,
os: nuts.platforms.toType(download.platform.type)
}
});
}
next();
});
if (process.env.TRUST_PROXY) {
try {
var trustProxyObject = JSON.parse(process.env.TRUST_PROXY);
app.set('trust proxy', trustProxyObject);
}
catch (e) {
app.set('trust proxy', process.env.TRUST_PROXY);
}
}
app.use(myNuts.router);
// Error handling
app.use(function(req, res, next) {
res.status(404).send("Page not found");
});
app.use(function(err, req, res, next) {
var msg = err.message || err;
var code = 500;
console.error(err.stack || err);
// Return error
res.format({
'text/plain': function(){
res.status(code).send(msg);
},
'text/html': function () {
res.status(code).send(msg);
},
'application/json': function (){
res.status(code).send({
'error': msg,
'code': code
});
}
});
});
myNuts.init()
// Start the HTTP and/or HTTPS server
.then(function() {
// Enable https endpoint if key and cert are set
if(key != "" && cert != "") {
var https_server = https.createServer(https_options, app).listen(process.env.HTTPSPORT || 5001, function () {
var hosts = https_server.address().address;
var ports = https_server.address().port;
console.log('Listening at https://%s:%s', hosts, ports);
});
}
var server = app.listen(process.env.PORT || 5000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Listening at http://%s:%s', host, port);
});
}, function(err) {
console.log(err.stack || err);
process.exit(1);
});