diff --git a/lib/middleware.js b/lib/middleware.js index 870f875d..cad02af9 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -42,43 +42,37 @@ exports.notMultihostURL = function(req, rsp, next){ exports.index = function(dirPath){ return function(req, rsp, next){ - var host = req.headers.host - var hostname = host.split(':')[0] - var arr = hostname.split(".") - var port = host.split(':')[1] ? ':' + host.split(':')[1] : '' + var host = req.headers.host; + var hostname = host.split(':')[0]; + var arr = hostname.split("."); + var port = host.split(':')[1] ? ':' + host.split(':')[1] : ''; + var poly = polymer.root(__dirname + "/templates"); if(arr.length == 2){ fs.readdir(dirPath, function(err, files){ - var projects = [] + var projects = []; files.forEach(function(file){ - if(file.split(".").length == 3){ - - var portal = file.split('.') - portal.shift() - - var local = file.split('.') - local.pop() - local.pop() - local.push(host) + var local = file.split('.'); + var appPart = local.shift(); + // DOT files are ignored. + if (file[0] !== ".") { projects.push({ "name" : file, - "localUrl" : 'http://' + local.join('.'), - "remoteUrl" : 'http://' + file, - "portalUrl" : 'http://' + portal.join('.') + '/apps/' + file, + "localUrl" : 'http://' + appPart + "." + host, "localPath" : path.resolve(dirPath, file) - }) + }); } - }) - var poly = polymer.root(__dirname + "/templates") + }); + poly.render("index.jade", { pkg: pkg, projects: projects, layout: "_layout.jade" }, function(error, body){ rsp.end(body) - }) + }); }) - }else{ - next() + } else { + next(); } } @@ -86,36 +80,28 @@ exports.index = function(dirPath){ exports.hostProjectFinder = function(dirPath){ return function(req, rsp, next){ - var host = req.headers.host - var hostname = host.split(':')[0] - var matches = [] + var host = req.headers.host; + var hostname = host.split(':')[0]; + var matches = []; fs.readdir(dirPath, function(err, files){ + var appPart = hostname.split(".")[0]; - [".io", ".me"].forEach(function(ext){ - var val = hostname.replace(/\.\w+$/, ext) - if(files.indexOf(val) !== -1){ - matches.push(val) + files.forEach(function(file){ + var fp = file.split('.'); + if (appPart == fp[0]) { + matches.push(file); } - }) - - ;[".harpapp.io"].forEach(function(ext){ - var val = hostname.replace(/\.\w+\.\w+$/, ext) - if(files.indexOf(val) !== -1){ - matches.push(val) - } - }) + }); if(matches.length > 0){ - req.projectPath = path.resolve(dirPath, matches[0]) - next() - }else{ - // TODO: add better error message here + req.projectPath = path.resolve(dirPath, matches[0]); + next(); + } else { rsp.end("Cannot find project") } - }) - + }); } } diff --git a/lib/templates/index.jade b/lib/templates/index.jade index 15e8c188..2e3eb6d2 100644 --- a/lib/templates/index.jade +++ b/lib/templates/index.jade @@ -5,7 +5,7 @@ if projects == "" p You have no apps yet, but you are ready to create one! p Inside your apps directory, run harp init myproject and then harp server myproject. else - h2 Your apps + h2 Your apps: ul.projects for project in projects li