Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
silentrob committed Jan 14, 2014
1 parent 55af453 commit b90cef3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 45 deletions.
74 changes: 30 additions & 44 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,80 +42,66 @@ 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();
}

}
}

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")
}

})

});
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/templates/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>harp init myproject</code> and then <code>harp server myproject</code>.
else
h2 Your apps
h2 Your apps:
ul.projects
for project in projects
li
Expand Down

0 comments on commit b90cef3

Please sign in to comment.