Browse Source

various changes

master
mort 8 years ago
parent
commit
a7b29e6919
8 changed files with 84 additions and 25 deletions
  1. 2
    0
      .gitignore
  2. 0
    4
      conf.json
  3. 15
    0
      conf.json.example
  4. 41
    19
      index.js
  5. 0
    0
      lib/context.js
  6. 21
    0
      lib/db.js
  7. 4
    1
      package.json
  8. 1
    1
      web/viewer.node.js

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
conf.json
node_modules

+ 0
- 4
conf.json View File

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

+ 15
- 0
conf.json.example View File

@@ -0,0 +1,15 @@
{
"webroot": "web",
"port": 8081,
"db": {
"host": "localhost",
"user": "dbuser",
"pass": "dbpass",
"database": "mimg"
},
"use_https": false,
"https": {
"key": "",
"cert": ""
}
}

+ 41
- 19
index.js View File

@@ -1,10 +1,11 @@
var http = require("http");
var https = require("https");
var fs = require("fs");
var Api = require("./api.js");
var Context = require("./lib/context.js");

var conf = JSON.parse(fs.readFileSync("conf.json"));

var files = {
var endpoints = {
"/": "index.html",
"/404": "404.html",
"/viewer": "viewer.node.js"
@@ -12,19 +13,32 @@ var files = {

//Prepare files
var errs = false;
Object.keys(files).forEach(function(i) {
Object.keys(endpoints).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");

//The endpoint is a function if the file ends with .node.js
if (/\.node\.js$/.test(endpoints[i])) {
endpoints[i] = require("./"+conf.webroot+"/"+endpoints[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");
}

//Errors will usually be because an endpoint doesn't exist
} catch (err) {
console.log(err.toString());
errs = true;
if (err.code == "ENOENT") {
console.log(err.toString());
errs = true;
} else {
throw err;
}
}
});
if (errs)
process.exit();

//No need to proceed if some endpoints don't exist
if (errs) process.exit();

//Prepare all templates
var templates = {};
@@ -35,20 +49,28 @@ fs.readdirSync("templates").forEach(function(f) {
function onRequest(req, res) {
console.log("Request for "+req.url);

var file = files[req.url];
var ep = endpoints[req.url];

if (!file) {
file = files["/404"];
//If the file doesn't exist, we 404.
if (!ep) {
ep = endpoints["/404"];
res.writeHead(404);
}

if (typeof file == "function")
file(new Api(req, res, templates, conf));
else
res.end(file);
//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));
} else {
res.end(ep);
}
}

var server = http.createServer(onRequest);
var server;
if (conf.use_https) {
server = https.createServer(conf.https, onRequest);
} else {
server = http.createServer(onRequest);
}
server.listen(conf.port);

console.log("Listening on port "+conf.port+".");

api.js → lib/context.js View File


+ 21
- 0
lib/db.js View File

@@ -0,0 +1,21 @@
var pg = require("pg");

module.expors = function(conf, cb) {
var conStr =
"postgres://"+
conf.user+":"+
conf.pass+"@"+
conf.host+"/"+
conf.database;

pg.connect(conStr, function(err, client) {
if (err) return cb(err);

this.client = client;
cb();
}.bind(this));
}

module.exports.prototype = {
query: function
}

+ 4
- 1
package.json View File

@@ -8,5 +8,8 @@
"start": "node index.js"
},
"author": "Martin Dørum Nygaard",
"license": "GPLv2"
"license": "GPLv2",
"dependencies": {
"pg": "^4.4.0"
}
}

+ 1
- 1
web/viewer.node.js View File

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

Loading…
Cancel
Save