added a logger, started working on the maintenance timer routines
[nodejs-repoproxy.git] / lib / router.js
index 71fddb1..63622f7 100644 (file)
@@ -2,12 +2,20 @@ 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
+       console.log("request: ", req.url);
        var thisQuery = unifyRequest(req, res, function(unified) {
-               if(unified.exists) {
-                       if(unified.isFile) {
+               console.log("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(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);
@@ -15,18 +23,20 @@ exports.routeRequest = function(req, res) {
                                console.log("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") {
+                               console.log("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);
        
@@ -42,18 +52,10 @@ function unifyRequest(req, res, callback, testing) {
        // create the full file path by spanning the cachedir
        unified.fullFilePath = (global.repoproxy.cacheDir + "/" + originalurl.pathname.replace(/^\/pub/, "")).replace(/\/+/g, "/");
        
-       // determine if the request is for a directory
-       if(unified.requestFor.match(/\/$/) != null) {
-               unified.isDirectoryRequest = true;
-               unified.fullPathDirName = unified.fullFilePath;
-       } else {
-               unified.isDirectoryRequest = false;
-               unified.fullPathDirName = path.dirname(unified.fullFilePath);
-       }
-       
        // determine the topPath, subpath etc.
        var spl = unified.requestFor.split("/");
        unified.topPath = spl[1];
+       unified.topFullPath = (global.repoproxy.cacheDir + "/" + spl[1]).replace(/\/+/g, "/");
        unified.subPath = "";
        if(spl.length > 2) {
                for(var i=2; i < spl.length; i++) {
@@ -64,6 +66,17 @@ function unifyRequest(req, res, callback, testing) {
                unified.subPath = null;
        }
        
+       // determine if the request is for a directory
+       if(unified.requestFor.match(/\/$/) != null) {
+               unified.isDirectoryRequest = true;
+               unified.fullPathDirName = unified.fullFilePath;
+               unified.subPathDirName = unified.subPath;
+       } else {
+               unified.isDirectoryRequest = false;
+               unified.fullPathDirName = path.dirname(unified.fullFilePath);
+               unified.subPathDirName = path.dirname(unified.subPath);
+       }
+       
        
        fs.stat(unified.fullFilePath, function(err, stats) {
                if(err == null) {
@@ -72,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);