@@ -0,0 +1,25 @@ | |||
module.exports = function(req, res, templates, conf) { | |||
this.req = req; | |||
this.res = res; | |||
this.templates = templates; | |||
this.conf = conf; | |||
} | |||
module.exports.prototype = { | |||
end: function(str) { | |||
this.res.end(str); | |||
}, | |||
template: function(name, args) { | |||
var str = this.templates[name]; | |||
if (!str) | |||
throw new Error("No such template: "+name); | |||
for (var i in args) { | |||
str = str.split("{{"+i+"}}").join(args[i]); | |||
} | |||
return str; | |||
} | |||
} |
@@ -0,0 +1,4 @@ | |||
{ | |||
"webroot": "web", | |||
"port": 8081 | |||
} |
@@ -0,0 +1,54 @@ | |||
var http = require("http"); | |||
var fs = require("fs"); | |||
var Api = require("./api.js"); | |||
var conf = JSON.parse(fs.readFileSync("conf.json")); | |||
var files = { | |||
"/": "index.html", | |||
"/404": "404.html", | |||
"/viewer": "viewer.node.js" | |||
} | |||
//Prepare files | |||
var errs = false; | |||
Object.keys(files).forEach(function(i) { | |||
try { | |||
if (/\.node\.js$/.test(files[i])) | |||
files[i] = require("./"+conf.webroot+"/"+files[i]); | |||
else | |||
files[i] = fs.readFileSync(conf.webroot+"/"+files[i], "utf8"); | |||
} catch (err) { | |||
console.log(err.toString()); | |||
errs = true; | |||
} | |||
}); | |||
if (errs) | |||
process.exit(); | |||
//Prepare all templates | |||
var templates = {}; | |||
fs.readdirSync("templates").forEach(function(f) { | |||
templates[f.replace(/\.html$/, "")] = fs.readFileSync("templates/"+f, "utf8"); | |||
}); | |||
function onRequest(req, res) { | |||
console.log("Request for "+req.url); | |||
var file = files[req.url]; | |||
if (!file) { | |||
file = files["/404"]; | |||
res.writeHead(404); | |||
} | |||
if (typeof file == "function") | |||
file(new Api(req, res, templates, conf)); | |||
else | |||
res.end(file); | |||
} | |||
var server = http.createServer(onRequest); | |||
server.listen(conf.port); | |||
console.log("Listening on port "+conf.port+"."); |
@@ -0,0 +1,12 @@ | |||
{ | |||
"name": "mimg", | |||
"version": "0.0.0", | |||
"description": "A simple image host.", | |||
"main": "index.js", | |||
"scripts": { | |||
"test": "echo \"Error: no test specified\" && exit 1", | |||
"start": "node index.js" | |||
}, | |||
"author": "Martin Dørum Nygaard", | |||
"license": "GPLv2" | |||
} |
@@ -0,0 +1,10 @@ | |||
<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<meta charset="utf-8"> | |||
<title>{{title}}</title> | |||
</head> | |||
<body> | |||
</body> | |||
</html> |
@@ -0,0 +1,9 @@ | |||
<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<meta charset="utf-8"> | |||
</head> | |||
<body> | |||
404, file not found. | |||
</body> | |||
</html> |
@@ -0,0 +1,9 @@ | |||
<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<meta charset="utf-8"> | |||
</head> | |||
<body> | |||
Hi. | |||
</body> | |||
</html> |
@@ -0,0 +1,5 @@ | |||
module.exports = function(api) { | |||
api.end(api.template("viewer", { | |||
title: "lol hi" | |||
})); | |||
} |