|
|
|
|
|
|
|
|
var conf = JSON.parse(fs.readFileSync("conf.json")); |
|
|
var conf = JSON.parse(fs.readFileSync("conf.json")); |
|
|
|
|
|
|
|
|
var endpoints = { |
|
|
var endpoints = { |
|
|
"/": "index.html", |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//General files |
|
|
|
|
|
"/favicon.ico": "favicon.ico", |
|
|
|
|
|
"/bootstrap.css": "bootstrap.css", |
|
|
"/404": "404.html", |
|
|
"/404": "404.html", |
|
|
"/viewer": "viewer.node.js" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Index files |
|
|
|
|
|
"/": "index/index.node.js", |
|
|
|
|
|
"/index/script.js": "index/script.js", |
|
|
|
|
|
"/index/style.css": "index/style.css", |
|
|
|
|
|
|
|
|
|
|
|
//Viewer files |
|
|
|
|
|
"/viewer": "viewer/index.node.js", |
|
|
|
|
|
"/viewer/script.js": "viewer/script.js", |
|
|
|
|
|
"/viewer/style.css": "viewer/style.css" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Prepare endpoints |
|
|
//Prepare endpoints |
|
|
|
|
|
|
|
|
//If it doesn't end with .node.js, it's a regular text file and will |
|
|
//If it doesn't end with .node.js, it's a regular text file and will |
|
|
//just be served as is |
|
|
//just be served as is |
|
|
} else { |
|
|
} else { |
|
|
endpoints[i] = fs.readFileSync(conf.webroot+"/"+endpoints[i], "utf8"); |
|
|
|
|
|
|
|
|
endpoints[i] = fs.readFileSync(conf.webroot+"/"+endpoints[i]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Errors will usually be because an endpoint doesn't exist |
|
|
//Errors will usually be because an endpoint doesn't exist |
|
|
|
|
|
|
|
|
templates[f.replace(/\.html$/, "")] = fs.readFileSync("templates/"+f, "utf8"); |
|
|
templates[f.replace(/\.html$/, "")] = fs.readFileSync("templates/"+f, "utf8"); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
//Prepare all views |
|
|
|
|
|
var views = {}; |
|
|
|
|
|
fs.readdirSync("views").forEach(function(f) { |
|
|
|
|
|
views[f.replace(/\.html$/, "")] = fs.readFileSync("views/"+f, "utf8"); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
//Function to run on each request |
|
|
//Function to run on each request |
|
|
function onRequest(req, res) { |
|
|
function onRequest(req, res) { |
|
|
console.log("Request for "+req.url); |
|
|
console.log("Request for "+req.url); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Execute if it's a .node.js, or just respond with the contents of the file |
|
|
//Execute if it's a .node.js, or just respond with the contents of the file |
|
|
if (typeof ep == "function") { |
|
|
if (typeof ep == "function") { |
|
|
ep(new Context(req, res, templates, conf)); |
|
|
|
|
|
|
|
|
ep(new Context({ |
|
|
|
|
|
req: req, |
|
|
|
|
|
res: res, |
|
|
|
|
|
templates: templates, |
|
|
|
|
|
views: views, |
|
|
|
|
|
conf: conf |
|
|
|
|
|
})); |
|
|
} else { |
|
|
} else { |
|
|
res.end(ep); |
|
|
res.end(ep); |
|
|
} |
|
|
} |