[TASK] Add a settings file template and begin couchdb support.

This commit is contained in:
Jan Philipp Timme 2013-09-13 23:11:00 +02:00
parent d6117dcbaa
commit 1f8e98c4a0
4 changed files with 86 additions and 11 deletions

47
app.js
View File

@ -1,10 +1,33 @@
var async = require("async");
var cradle = require("cradle");
var express = require("express");
var fs = require("fs");
var http = require("http");
var https = require("https");
var fs = require("fs");
var log4js = require("log4js");
//load settings
var settings = require("./src/Settings.js");
//initialize couch connector
cradle.setup(settings.couchdb);
var couch = new(cradle.Connection);
var db = couch.database(settings.couchdb.database);
db.exists(function(err, exists) {
if(err) {
console.log(err);
process.exit(1);
} else if(exists == true) {
console.log("Database exists :-)");
} else {
console.log("Database does not exist!");
process.exit(1);
}
});
//begin setting up the dashboard app
var app = express();
app.use(function(req, res, next) {
console.log("%s %s", req.method, req.url);
next();
@ -13,19 +36,23 @@ app.use(function(req, res, next) {
//deliver static files by default
app.use(express.static(__dirname + '/static'));
//serve random fun stuff on /ohai ;-)
app.use("/ohai", function(req, res) {
res.send("ohai!");
});
//define 404 for everything else (ugly but i think it's useful)
app.use(function(req, res) {
res.status(404).send("Sorry, nothing here.");
});
//http.createServer(app).listen(3000);
var httpsOptions = {
"key": fs.readFileSync("snakeoil/privkey.pem"),
"cert": fs.readFileSync("snakeoil/cert.pem")
};
https.createServer(httpsOptions, app).listen(3000);
//fire it up as https (or http) server
if(settings.general.https == true) {
var httpsOptions = {
"cert": fs.readFileSync(settings.https.cert),
"key": fs.readFileSync(settings.https.key)
};
https.createServer(httpsOptions, app).listen(settings.general.listen);
} else {
http.createServer(app).listen(settings.general.listen);
}

View File

@ -10,7 +10,9 @@
"author": "Jan Philipp Timme <jan.philipp@timme.it>",
"dependencies": {
"express": "*",
"cradle": "*"
"cradle": "*",
"log4js": "*",
"async": "*"
},
"devDependencies": {},
"engines": {

27
settings.json.template Normal file
View File

@ -0,0 +1,27 @@
{
"general": {
//serve https or http?
//generate certs in ./snakeoil before activating this
"https": false,
"listen": 3000
},
"https": {
"key": "snakeoil/privkey.pem",
"cert": "snakeoil/cert.pem",
},
"couchdb": {
"host": "127.0.0.1",
"port": 5984,
"database": "dashboard",
"auth": { //replace with null in case of admin party
"username": "admin",
"password": "admin"
},
"cache": true,
"raw": false,
"secure": false, //access couch via https?
"retries": 0,
"retryTimeout": 10e3,
"headers": {}
}
}

19
src/Settings.js Normal file
View File

@ -0,0 +1,19 @@
var fs = require("fs");
var vm = require("vm");
var settingsFilename = "settings.json";
settingsString = fs.readFileSync(settingsFilename).toString();
var settings;
try {
if(settingsString) {
settings = vm.runInContext("exports = " + settingsString, vm.createContext(), settingsFilename);
settings = JSON.parse(JSON.stringify(settings)); //fix objects having constructors of other vm.context
}
} catch(e) {
console.error("There was an error processing the settings.json file: " + e.message);
process.exit(1);
}
module.exports = settings;