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
|
|
|
|
2013-09-15 18:30:51 +02:00
|
|
|
//load own tools (tiny functions that help a little ;-)
|
|
|
|
var tools = require("./src/Tools.js");
|
|
|
|
|
2013-09-13 23:11:00 +02:00
|
|
|
//load settings
|
|
|
|
var settings = require("./src/Settings.js");
|
|
|
|
|
2013-09-16 18:58:09 +02:00
|
|
|
//load api handler
|
|
|
|
var sessionAPIHandler = require("./src/Session.api.js");
|
2013-09-16 19:01:03 +02:00
|
|
|
var userAPIHandler = require("./src/User.api.js");
|
2013-09-16 18:58:09 +02:00
|
|
|
|
2013-09-13 23:11:00 +02:00
|
|
|
//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) {
|
2013-09-14 23:31:17 +02:00
|
|
|
console.log("An error occured - could not talk to couchdb:");
|
2013-09-13 23:11:00 +02:00
|
|
|
console.log(err);
|
|
|
|
process.exit(1);
|
|
|
|
} else if(exists == true) {
|
|
|
|
console.log("Database exists :-)");
|
|
|
|
} else {
|
2013-09-14 23:31:17 +02:00
|
|
|
console.log("Database does not exist :-(");
|
2013-09-13 23:11:00 +02:00
|
|
|
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();
|
2013-09-14 23:29:45 +02:00
|
|
|
app.use(express.cookieParser());
|
|
|
|
app.use(express.session({
|
|
|
|
"secret": settings.general.sessionsecret
|
|
|
|
}));
|
2013-09-15 18:30:51 +02:00
|
|
|
app.use(express.bodyParser());
|
2013-09-14 23:29:45 +02:00
|
|
|
|
|
|
|
//some logging for debugging
|
2013-09-13 18:49:44 +02:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
console.log("%s %s", req.method, req.url);
|
2013-09-14 23:29:45 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
//initialize fresh session
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
if(req.session.initialized != true) {
|
|
|
|
req.session.initialized = true;
|
2013-09-15 18:30:51 +02:00
|
|
|
req.session.data = {
|
|
|
|
"user": null,
|
|
|
|
"profile": {},
|
|
|
|
"login": false,
|
2013-09-16 19:16:12 +02:00
|
|
|
"lastActivity": new Date().toString()
|
2013-09-15 18:30:51 +02:00
|
|
|
};
|
2013-09-14 23:29:45 +02:00
|
|
|
}
|
2013-09-15 18:30:51 +02:00
|
|
|
console.log(["Session Data", req.session.data]);
|
2013-09-13 18:49:44 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
//deliver static files by default
|
2013-09-14 23:32:59 +02:00
|
|
|
app.use("/", express.static(__dirname + '/static'));
|
2013-09-13 18:49:44 +02:00
|
|
|
|
2013-09-14 23:29:45 +02:00
|
|
|
//API: /session
|
2013-09-16 18:58:09 +02:00
|
|
|
app.use("/session", new sessionAPIHandler(db));
|
2013-09-13 18:49:44 +02:00
|
|
|
|
2013-09-15 18:30:51 +02:00
|
|
|
//API: /user
|
2013-09-16 19:01:03 +02:00
|
|
|
app.use("/user", new userAPIHandler(db));
|
2013-09-15 18:30:51 +02:00
|
|
|
|
2013-09-16 21:56:12 +02:00
|
|
|
//'automatic' error handling and/or responding to non-implemented http calls
|
|
|
|
//i know this is ugly as hell, but it might stay for a while.
|
2013-09-14 23:31:17 +02:00
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
if(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.send(500, "Oops, an error occured.");
|
|
|
|
} else {
|
2013-09-16 21:56:12 +02:00
|
|
|
res.send(501, "Method not implemented");
|
2013-09-14 23:31:17 +02:00
|
|
|
}
|
|
|
|
res.end();
|
2013-09-13 18:49:44 +02:00
|
|
|
});
|
|
|
|
|
2013-09-16 19:27:03 +02:00
|
|
|
//define listening callback function
|
|
|
|
var onListenCallback = function() {
|
|
|
|
var url = "";
|
|
|
|
url += (settings.general.https) ? "https://" : "http://";
|
|
|
|
url += settings.general.listen.host;
|
|
|
|
if((settings.general.https && settings.general.listen.port != 443) || (!settings.general.https && settings.general.listen.port != 80)) {
|
|
|
|
url += ":";
|
|
|
|
url += settings.general.listen.port;
|
|
|
|
}
|
|
|
|
url += "/";
|
|
|
|
console.log("Dashboard now listening on: " + url);
|
|
|
|
console.log("Enjoy! ;-)");
|
|
|
|
}
|
|
|
|
|
2013-09-14 23:32:59 +02:00
|
|
|
//fire it up as https (or http - NOT recommended(!)) server
|
2013-09-13 23:11:00 +02:00
|
|
|
if(settings.general.https == true) {
|
|
|
|
var httpsOptions = {
|
|
|
|
"cert": fs.readFileSync(settings.https.cert),
|
|
|
|
"key": fs.readFileSync(settings.https.key)
|
|
|
|
};
|
2013-09-16 19:27:03 +02:00
|
|
|
https.createServer(httpsOptions, app).listen(settings.general.listen.port, settings.general.listen.host, onListenCallback);
|
2013-09-13 23:11:00 +02:00
|
|
|
} else {
|
2013-09-16 19:27:03 +02:00
|
|
|
http.createServer(app).listen(settings.general.listen.port, settings.general.listen.host, onListenCallback);
|
|
|
|
}
|