X-Git-Url: http://git.pjr.cc/?p=nodejs-repoproxy.git;a=blobdiff_plain;f=lib%2Frouter.js;h=5767d268dc06af7085f8285e0fa25eb4d1f8bb6a;hp=8a7dca251eb82398749e9e10a27e69b27afc5cb2;hb=3933776341028e83d89e0f888fff3e6319230abf;hpb=68fbf923faf99f86cc36dc5776451f89925df8bd diff --git a/lib/router.js b/lib/router.js index 8a7dca2..5767d26 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2,34 +2,41 @@ var url = require("url"); var fs = require("fs"); var cache = require("./cache.js"); var path = require("path"); +var log = require("./log.js"); exports.routeRequest = function(req, res) { // first, unify the request + log.debug("request: ", req.url); var thisQuery = unifyRequest(req, res, function(unified) { + log.debug("unified request is ", unified); if(unified.requestFor == "/favicon.ico") { unified.b.writeHead(404, {"Content-Type": "text/plain"}); unified.b.write("404 Not Found\n"); } else if(unified.exists) { - if(unified.isFile) { + if(typeof global.repoproxy.downloads[unified.fullFilePath] != "undefined" && global.repoproxy.downloads[unified.fullFilePath] == 1) { + cache.upstreamRequest(unified); + } else if(unified.isFile) { cache.serviceFile(unified); } else if(unified.isDirectory) { cache.serviceDirectory(unified); } else { - console.log("ERROR: something went majorly wrong with something, ", unified); + log.debug("ERROR: something went majorly wrong with something, ", unified); } } else { - // it doesnt exist yet, so we send it to the cache service - console.log("file doesnt exist, upstream we go: ", unified); - cache.upstreamRequest(unified, function(err) { - if(err == null) { - cache.watchAndService(unfied); - } // if upstream sends anything other then a 200, cache.upstreamRequest will handle it (i.e. 302, 404, etc) - }); + // it doesnt exist yet, so we send it to the cache service if it matches an upstream service + if(typeof global.repoproxy.repo[unified.topPath] != "undefined") { + log.debug("file doesnt exist, upstream we go: ", unified); + cache.upstreamRequest(unified); + } else { + unified.b.writeHead(404, {"Content-Type": "text/plain"}); + unified.b.write("404 Not Found\n"); + unified.b.end(); + } } }); } -function unifyRequest(req, res, callback, testing) { +function unifyRequest(req, res, callback) { var unified = new Object(); var originalurl = url.parse(req.url); @@ -78,24 +85,24 @@ function unifyRequest(req, res, callback, testing) { //send a 302 and call it a day res.writeHead("302", { 'Location': unified.originalReq+"/" }); res.end(); - - // TODO: remove this after testing - if(testing) callback(null); - return 302; } if(stats.isDirectory()) { unified.isDirectory = true; unified.isFile = false; + unified.fileSize = null; } else if(stats.isFile()) { unified.isDirectory = false; - unified.isFile = true; + unified.isFile = true; + unified.fileSize = stats["size"]; } else { unified.isDirectory = false; unified.isFile = false; + unified.fileSize = null; } } else { unified.exists = false; + unified.fileSize = null; } callback(unified);