Skip to content

Commit

Permalink
Merge pull request #2 from skewart/config-host-base-url
Browse files Browse the repository at this point in the history
Config host base url
  • Loading branch information
skewart committed Dec 21, 2015
2 parents 239286f + 4e75f58 commit b62934d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ $ npm install
This service requires to be configured using environment variables:

```
# Set the port for the service
# Set the port for the service (default is 5000)
$ export PORT=6000
# Set the host for the service (default is 0.0.0.0)
$ export HOST=127.0.0.1
# Set a base URL for the service (default is '/')
$ export BASE_URL=/release
# Access token for the GitHub API (requires permissions to access the repository)
# If the repository is public you do not need to provide an access token
# you can also use GITHUB_USERNAME and GITHUB_PASSWORD
Expand Down
14 changes: 8 additions & 6 deletions bin/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ var basicAuth = require('basic-auth');
var Analytics = require('analytics-node');
var nuts = require('../');

const
BASE_URL = process.env.BASE_URL || '/',
PORT = process.env.PORT || 5000,
HOST = process.env.HOST || '0.0.0.0';

var app = express();

var apiAuth = {
Expand Down Expand Up @@ -70,7 +75,7 @@ var myNuts = nuts({
}
});

app.use(myNuts.router);
app.use(BASE_URL, myNuts.router);

// Error handling
app.use(function(req, res, next) {
Expand Down Expand Up @@ -99,9 +104,6 @@ app.use(function(err, req, res, next) {
});
});

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);
var server = app.listen(PORT, HOST, function () {
console.log('Listening at http://%s:%s%s', HOST, PORT, BASE_URL);
});
43 changes: 26 additions & 17 deletions lib/github.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var _ = require('lodash');
var Q = require('q');
var destroy = require('destroy');
var github = require('octonode');
var request = require('request');
var Buffer = require('buffer').Buffer;
Expand All @@ -18,7 +19,7 @@ module.exports = function(opts) {
}

// List releases
var listReleases = function(page) {
function listReleases(page) {
page = page || 1;

var uri = "/repos/"+opts.repository+"/releases";
Expand All @@ -38,18 +39,18 @@ module.exports = function(opts) {
return releases.concat(r);
});
});
};
}

var cacheListReleases =_.memoize(listReleases, function() {
return cacheInstance+Math.ceil(Date.now()/opts.timeout)
});

var clearCache = function() {
function clearCache() {
cacheInstance = cacheInstance + 1;
};
}

// Stream a download to res
var streamAsset = function (uri) {
function streamAsset(uri) {
var headers = {
'User-Agent': "releaser-server",
'Accept': "application/octet-stream"
Expand All @@ -72,23 +73,31 @@ module.exports = function(opts) {
headers: headers,
auth: httpAuth
});
};
}

// Read a asset
var readAsset = function(uri) {
function readAsset(uri) {
var d = Q.defer();
var output = Buffer([]);
var res = streamAsset(uri);

streamAsset(uri)
.on('data', function(buf) {
output = Buffer.concat([output, buf]);
})
.once('error', function(err) {
d.reject(err);
})
.once('end', function() {
d.resolve(output);
});
var cleanup = function() {
destroy(res);
res.removeAllListeners();
};

res
.on('data', function(buf) {
output = Buffer.concat([output, buf]);
})
.on('error', function(err) {
cleanup();
d.reject(err);
})
.on('end', function() {
cleanup();
d.resolve(output);
});

return d.promise
}
Expand Down
10 changes: 3 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ module.exports = function nuts(opts) {
.then(function(versions) {
// Update needed?
var latest = _.first(versions);
if (!latest) throw new Error("Version not found");

// File exists
var asset = _.find(latest.platforms, {
Expand All @@ -224,14 +225,9 @@ module.exports = function nuts(opts) {

releases = _.chain(releases)

// Exclude deltas and other versions
.filter(function(entry) {
return (!entry.isDelta && entry.version == winReleases.normVersion(latest.tag));
})

// Change filename to use downlaodp roxy
// Change filename to use download proxy
.map(function(entry) {
entry.filename = url.resolve(fullUrl, '/download/'+latest.tag+'/'+entry.filename);
entry.filename = url.resolve(fullUrl, '/download/'+entry.version+'/'+entry.filename);

return entry;
})
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuts-serve",
"version": "2.6.1",
"version": "2.6.2",
"description": "Server to make GitHub releases (private) available to download with Squirrel support",
"main": "./lib/index.js",
"homepage": "https://github.com/GitbookIO/nuts",
Expand All @@ -20,7 +20,8 @@
"analytics-node": "1.2.2",
"uuid": "2.0.1",
"github-webhook-handler": "0.5.0",
"strip-bom": "2.0.0"
"strip-bom": "2.0.0",
"destroy": "1.0.3"
},
"devDependencies": {
"mocha": "1.18.2",
Expand Down

0 comments on commit b62934d

Please sign in to comment.