Browse Source

modified the framework more

master
mort 8 years ago
parent
commit
198980ef0e

+ 28
- 4
index.js View File

@@ -7,9 +7,21 @@ var Db = require("./lib/db.js");
var conf = JSON.parse(fs.readFileSync("conf.json"));

var endpoints = {
"/": "index.html",

//General files
"/favicon.ico": "favicon.ico",
"/bootstrap.css": "bootstrap.css",
"/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
@@ -24,7 +36,7 @@ Object.keys(endpoints).forEach(function(i) {
//If it doesn't end with .node.js, it's a regular text file and will
//just be served as is
} 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
@@ -47,6 +59,12 @@ fs.readdirSync("templates").forEach(function(f) {
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 onRequest(req, res) {
console.log("Request for "+req.url);
@@ -61,7 +79,13 @@ function onRequest(req, res) {

//Execute if it's a .node.js, or just respond with the contents of the file
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 {
res.end(ep);
}

+ 25
- 10
lib/context.js View File

@@ -1,8 +1,20 @@
module.exports = function(req, res, templates, conf) {
this.req = req;
this.res = res;
this.templates = templates;
this.conf = conf;
function templatify(str, args) {
if (args == undefined)
return str;

for (var i in args) {
str = str.split("{{"+i+"}}").join(args[i]);
}

return str;
}

module.exports = function(options) {
this.req = options.req;
this.res = options.res;
this.templates = options.templates;
this.views = options.views;
this.conf = options.conf;
}

module.exports.prototype = {
@@ -12,14 +24,17 @@ module.exports.prototype = {

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 templatify(str, args);
},

return str;
view: function(name, args) {
var str = this.views[name];
if (!str)
throw new Error("No such view: "+name);

return templatify(str, args);
}
}

+ 9
- 0
templates/header.html View File

@@ -0,0 +1,9 @@
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<div class="pull-right">
Log In
</div>
</div>
</div>
</div>

web/index.html → views/index.html View File

@@ -2,8 +2,9 @@
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="/bootstrap.css">
</head>
<body>
Hi.
{{header}}
</body>
</html>

templates/viewer.html → views/viewer.html View File

@@ -5,6 +5,6 @@
<title>{{title}}</title>
</head>
<body>
{{header}}
</body>
</html>

+ 9
- 0
web/bootstrap.css
File diff suppressed because it is too large
View File


BIN
web/favicon.ico View File


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

@@ -0,0 +1,5 @@
module.exports = function(ctx) {
ctx.end(ctx.view("index", {
header: ctx.template("header")
}));
}

+ 0
- 0
web/index/script.js View File


+ 0
- 0
web/index/style.css View File


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

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

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

@@ -0,0 +1,5 @@
module.exports = function(ctx) {
ctx.end(ctx.view("viewer", {
header: ctx.template("header")
}));
}

+ 0
- 0
web/viewer/script.js View File


+ 0
- 0
web/viewer/style.css View File


Loading…
Cancel
Save