Skip to content

Commit

Permalink
Fix bugs from rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse committed Feb 10, 2016
1 parent a0fa9aa commit a17bcdf
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
18 changes: 13 additions & 5 deletions bin/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var apiAuth = {
};

var analytics = undefined;
var downloadEvent = process.env.ANALYTICS_EVENT_DOWNLOAD || 'download';
if (process.env.ANALYTICS_TOKEN) {
analytics = new Analytics(process.env.ANALYTICS_TOKEN);
}
Expand All @@ -33,7 +34,7 @@ var myNuts = nuts.Nuts({
var userId = req.query.user;

analytics.track({
event: process.env.ANALYTICS_EVENT_DOWNLOAD || 'download',
event: downloadEvent,
anonymousId: userId? null : uuid.v4(),
userId: userId,
properties: {
Expand Down Expand Up @@ -99,10 +100,17 @@ app.use(function(err, req, res, next) {
});
});

myNuts.init()

// Start the HTTP server
var server = app.listen(process.env.PORT || 5000, function () {
var host = server.address().address;
var port = server.address().port;
.then(function() {
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);
console.log('Listening at http://%s:%s', host, port);
});
}, function(err) {
console.log(err.stack || err);
process.exit(1);
});
9 changes: 6 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ module.exports = {
},

'versions': function (req) {
return {
return this.versions.filter({
platform: req.query.platform,
channel: req.query.channel || '*'
};
});
},

'channels': function () {
return this.versions.channels();
},

'version/:tag': function (req) {
return this.versions.get(req.params.tag);
return this.versions.resolve({
tag: req.params.tag,
channel: '*'
});
},

'resolve': function(req) {
Expand Down
25 changes: 11 additions & 14 deletions lib/backends/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ function GitHubBackend() {
var that = this;
Backend.apply(this, arguments);

if (this.opts.token) {
this.client = new GitHub(this.opts.token);
} else {
this.client = new GitHub({
username: this.opts.username,
password: this.opts.password
});
}
this.client = new GitHub({
token: this.opts.token,
username: this.opts.username,
password: this.opts.password
});

this.ghrepo = this.client.repo(this.opts.repository);
this.releases = this.memoize(this._releases);
Expand Down Expand Up @@ -56,18 +53,18 @@ GitHubBackend.prototype.getAssetStream = function(asset) {
};
var httpAuth;

if (opts.token) {
headers['Authorization'] = 'token '+opts.token;
} else if (opts.username) {
if (this.opts.token) {
headers['Authorization'] = 'token '+this.opts.token;
} else if (this.opts.username) {
httpAuth = {
user: opts.username,
pass: opts.password,
user: this.opts.username,
pass: this.opts.password,
sendImmediately: true
};
}

return Q(request({
uri: asset.download_url,
uri: asset.raw.url,
method: 'get',
headers: headers,
auth: httpAuth
Expand Down
13 changes: 11 additions & 2 deletions lib/nuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ var platforms = require('./utils/platforms');
var winReleases = require('./utils/win-releases');
var API_METHODS = require('./api');

function getFullUrl(req) {
return req.protocol + '://' + req.get('host') + req.originalUrl;
}

function Nuts(opts) {
if (!(this instanceof Nuts)) return new Nuts(opts);
var that = this;

_.bindAll(this);

this.opts = _.defaults(opts || {}, {
// Backend to use
Expand Down Expand Up @@ -57,6 +64,8 @@ function Nuts(opts) {
this.versions = new Versions(this.backend);

// Bind routes
this.router.use(useragent.express());

this.router.get('/', this.onDownload);
this.router.get('/download/channel/:channel/:platform?', this.onDownload);
this.router.get('/download/version/:tag/:platform?', this.onDownload);
Expand All @@ -73,7 +82,7 @@ function Nuts(opts) {
this.router.get('/api/' + route, function(req, res, next) {
return Q()
.then(function() {
return method(req);
return method.call(that, req);
})
.then(function(result) {
res.send(result);
Expand Down Expand Up @@ -107,7 +116,7 @@ Nuts.prototype.serveAsset = function(req, res, version, asset) {
}

// Call middleware
return Q.nfcall(opts.onDownload, {
return Q.nfcall(this.opts.onDownload, {
version: version,
platform: asset
}, req, res)
Expand Down
13 changes: 6 additions & 7 deletions lib/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ function normalizeVersion(release) {

downloadCount = downloadCount + asset.download_count;
return {
id: String(asset.id),
type: platform,
filename: asset.name,
size: asset.size,
content_type: asset.content_type,
download_url: asset.url,
download_count: asset.download_count
raw: asset
};
})
.compact()
Expand All @@ -47,8 +47,7 @@ function normalizeVersion(release) {
channel: extractChannel(release.tag_name),
notes: release.body || "",
published_at: new Date(release.published_at),
platforms: releasePlatforms,
download_count: downloadCount
platforms: releasePlatforms
};
}

Expand Down Expand Up @@ -82,13 +81,13 @@ Versions.prototype.list = function() {

// Get a specific version by its tag
Versions.prototype.get = function(tag) {
return this.resolveVersion({
return this.resolve({
tag: tag
});
};

// Filter versions with criterias
Versions.prototype.filter = function() {
Versions.prototype.filter = function(opts) {
opts = _.defaults(opts || {}, {
tag: 'latest',
platform: null,
Expand Down Expand Up @@ -126,7 +125,7 @@ Versions.prototype.resolve = function(opts) {

// List all channels from releases
Versions.prototype.channels = function(opts) {
return this.versions()
return this.list()
.then(function(versions) {
var channels = {};

Expand Down

0 comments on commit a17bcdf

Please sign in to comment.