Permalink
Browse files

cut down the sludge in the cache, now it only stores the SHA1 instead…

… of the whole change log (can't belive I didnt catch that sooner -_- )

Changed caching to be stored now, so it's prefilled after a restart.

Signed-off-by: Shane Gadsby <schme16@gmail.com>
  • Loading branch information...
schme16 committed Jul 4, 2015
1 parent 38dc76a commit 85b4dea2846e279bcfd7cc565f560b35be2d6f49
Showing with 48 additions and 22 deletions.
  1. +2 −1 .gitignore
  2. +46 −21 server.js
View
@@ -25,4 +25,5 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
/ecosystem.json
store-cache
store-*
View
@@ -5,40 +5,60 @@ pmx = require('pmx');
pmx.init();
var express = require('express'),
fs = require('fs'),
app = express(),
request = require('request'),
cors = require('cors'),
http = require('https'),
mime = require('mime'),
function store (key, data) {
if (typeof key !== "undefined" && key !== false) {
if (typeof data !== "undefined" || data === false) {
fs.writeFile('store-' + key, JSON.stringify(data), function () {});
return data;
}
else {
var d = false;
try {
d = JSON.parse(fs.readFileSync('store-' + key));
}
catch(e) {}
cache = {},
return d
}
}
else {
return data || false
}
}
lastCall = function (meta, body, req, res, cacheing) {
if (body && !!body.sha && !cacheing) {
var newUrl = 'https://gitcdn' + (req.get('host').indexOf('min.gitcdn.xyz') !== -1 ? 'min' : '') + '-17ac.kxcdn.com/cdn/' + meta.user + '/' + meta.repo + '/' + body.sha + '/' + meta.filePath;
cache[meta.user + '/' + meta.repo] = body;
function lastCall (meta, sha, req, res, cacheing) {
if (sha && !cacheing) {
var newUrl = 'https://gitcdn' + (req.get('host').indexOf('min.gitcdn.xyz') !== -1 ? 'min' : '') + '-17ac.kxcdn.com/cdn/' + meta.user + '/' + meta.repo + '/' + sha + '/' + meta.filePath;
cache[meta.user + '/' + meta.repo] = sha;
res.redirect(301, newUrl)
}
else if (!!cacheing) {
cache[meta.user + '/' + meta.repo] = body;
cache[meta.user + '/' + meta.repo] = sha;
}
else {
if (!cacheing) res.sendStatus(500);
var err = new Error('Status 500: Body missing in lastCall() || ' + meta.user + '/' + meta.repo + '/' + body.sha + '/' + meta.filePath);
var err = new Error('Status 500: SHA1 hash is missing in lastCall() || ' + meta.user + '/' + meta.repo + '/' + sha + '/' + meta.filePath);
pmx.notify(err);
}
store('cache', cache);
};
var express = require('express'),
fs = require('fs'),
app = express(),
request = require('request'),
cors = require('cors'),
http = require('https'),
mime = require('mime'),
cache = store('cache', store('cache') || {});
app.use('/', express.static(process.cwd() + '/website'))
@@ -99,7 +119,12 @@ app.get('/repo/*', function (req, res) {
pmx.notify(err);
}
lastCall(meta, body, req, res, refreshCache);
if (body && body.sha) lastCall(meta, body.sha, req, res, refreshCache);
else{
if (!refreshCache) res.sendStatus(500)
var err = new Error('SHA1 hash is missing in /repo -> request: ' + meta.user + '/' + meta.repo + '/' + body.sha + '/' + meta.filePath);
pmx.notify(err);
}
}
else {
if (!refreshCache) res.sendStatus(500)

0 comments on commit 85b4dea

Please sign in to comment.