diff --git a/app.js b/app.js index d5a9bec..00f1494 100644 --- a/app.js +++ b/app.js @@ -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); +} \ No newline at end of file diff --git a/package.json b/package.json index 1dc46d5..39a30b4 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,9 @@ "author": "Jan Philipp Timme ", "dependencies": { "express": "*", - "cradle": "*" + "cradle": "*", + "log4js": "*", + "async": "*" }, "devDependencies": {}, "engines": { diff --git a/settings.json.template b/settings.json.template new file mode 100644 index 0000000..93adbfa --- /dev/null +++ b/settings.json.template @@ -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": {} + } +} diff --git a/src/Settings.js b/src/Settings.js new file mode 100644 index 0000000..e6e7816 --- /dev/null +++ b/src/Settings.js @@ -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; \ No newline at end of file