2013-09-13 23:11:00 +02:00
|
|
|
var async = require("async");
|
|
|
|
var cradle = require("cradle");
|
2013-09-13 18:49:44 +02:00
|
|
|
var express = require("express");
|
2013-09-13 23:11:00 +02:00
|
|
|
var fs = require("fs");
|
2013-09-13 18:49:44 +02:00
|
|
|
var http = require("http");
|
|
|
|
var https = require("https");
|
2013-09-13 23:11:00 +02:00
|
|
|
var log4js = require("log4js");
|
2013-09-14 01:18:43 +02:00
|
|
|
var scrypt = require("scrypt");
|
2013-09-13 23:11:00 +02:00
|
|
|
|
|
|
|
//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);
|
|
|
|
}
|
|
|
|
});
|
2013-09-13 18:49:44 +02:00
|
|
|
|
2013-09-13 23:11:00 +02:00
|
|
|
//begin setting up the dashboard app
|
2013-09-13 18:49:44 +02:00
|
|
|
var app = express();
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
console.log("%s %s", req.method, req.url);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
//deliver static files by default
|
|
|
|
app.use(express.static(__dirname + '/static'));
|
|
|
|
|
2013-09-13 23:11:00 +02:00
|
|
|
//serve random fun stuff on /ohai ;-)
|
2013-09-13 18:49:44 +02:00
|
|
|
app.use("/ohai", function(req, res) {
|
2013-09-14 01:18:43 +02:00
|
|
|
res.status(200).send("ohai!");
|
2013-09-13 18:49:44 +02:00
|
|
|
});
|
|
|
|
|
2013-09-13 23:11:00 +02:00
|
|
|
//define 404 for everything else (ugly but i think it's useful)
|
2013-09-13 18:49:44 +02:00
|
|
|
app.use(function(req, res) {
|
|
|
|
res.status(404).send("Sorry, nothing here.");
|
|
|
|
});
|
|
|
|
|
2013-09-13 23:11:00 +02:00
|
|
|
//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);
|
|
|
|
}
|