Browse Source

basic framework made

master
mort 8 years ago
commit
1e4c8d4eac
8 changed files with 128 additions and 0 deletions
  1. 25
    0
      api.js
  2. 4
    0
      conf.json
  3. 54
    0
      index.js
  4. 12
    0
      package.json
  5. 10
    0
      templates/viewer.html
  6. 9
    0
      web/404.html
  7. 9
    0
      web/index.html
  8. 5
    0
      web/viewer.node.js

+ 25
- 0
api.js View File

@@ -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;
}
}

+ 4
- 0
conf.json View File

@@ -0,0 +1,4 @@
{
"webroot": "web",
"port": 8081
}

+ 54
- 0
index.js View File

@@ -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+".");

+ 12
- 0
package.json View File

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

+ 10
- 0
templates/viewer.html View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{title}}</title>
</head>
<body>

</body>
</html>

+ 9
- 0
web/404.html View File

@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
404, file not found.
</body>
</html>

+ 9
- 0
web/index.html View File

@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
Hi.
</body>
</html>

+ 5
- 0
web/viewer.node.js View File

@@ -0,0 +1,5 @@
module.exports = function(api) {
api.end(api.template("viewer", {
title: "lol hi"
}));
}

Loading…
Cancel
Save