Skip to content

Commit

Permalink
Cache releaseUrl for life of running app
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardhorsford committed Mar 2, 2016
1 parent 8fe7065 commit 20abd2c
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var basicAuth = require('basic-auth'),
prompt = require('prompt'),
portScanner = require('portscanner'),
releaseUrl = null,
request = require('sync-request');

/**
Expand Down Expand Up @@ -55,22 +56,34 @@ exports.matchRoutes = function(req, res) {

};

// Synchronously get the url for the latest release on github
// Synchronously get the url for the latest release on github and store
exports.getLatestRelease = function() {

var url = "";
var options = {
headers: {'user-agent': 'node.js'}
};
var gitHubUrl = 'https://api.github.com/repos/alphagov/govuk_prototype_kit/releases/latest';
try {
res = request('GET', gitHubUrl, options);
var data = JSON.parse(res.getBody('utf8'));
url = data.zipball_url;

// Release url already exists
if (releaseUrl !== null){
console.log("Release url cached:", releaseUrl);
return releaseUrl;
}
catch(err) {
url = "https://github.com/alphagov/govuk_prototype_kit/releases/latest";
// Release url doesn't exist
else {
var options = {
headers: {'user-agent': 'node.js'}
};
var gitHubUrl = 'https://api.github.com/repos/alphagov/govuk_prototype_kit/releases/latest';
try {
res = request('GET', gitHubUrl, options);
var data = JSON.parse(res.getBody('utf8'));
url = data.zipball_url;
releaseUrl = url;
console.log("Release url is:", releaseUrl);
}
catch(err) {
url = "https://github.com/alphagov/govuk_prototype_kit/releases/latest";
console.log("Couldn't retrieve release url");
}
}
console.log("Release url is", url);
return url;
};

Expand Down

0 comments on commit 20abd2c

Please sign in to comment.