Installed ueberDB

pull/35/head
Peter 'Pita' Martischka 2011-05-14 18:57:07 +01:00
parent 0c6296c516
commit 68dfff5a21
6 changed files with 134 additions and 66 deletions

45
node/db.js Normal file
View File

@ -0,0 +1,45 @@
/**
* 2011 Peter 'Pita' Martischka
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var ueberDB = require("ueberDB");
var settings = require("./settings");
//set database settings
var db = new ueberDB.database(settings.dbType, settings.dbSettings);
//set the exported db to null, we will set it in intalize
exports.db = null;
exports.init = function(callback)
{
//initalize the database async
db.init(function(err)
{
//there was an error while initializing the database, output it and stop
if(err)
{
console.error("ERROR: Problem while initalizing the database");
console.error(err.stack ? err.stack : err);
process.exit(1);
}
//everything ok
else
{
exports.db = db;
callback(null);
}
});
}

View File

@ -17,50 +17,91 @@
var http = require('http')
, url = require('url')
, fs = require('fs')
, io = require('socket.io')
, socketio = require('socket.io')
, sys = require('sys')
, settings = require('./settings')
, server;
, db = require('./db')
, async = require('async');
server = http.createServer(function(req, res){
var path = url.parse(req.url).pathname;
if(path.substring(0,"/static".length) == "/static" || path.substring(0,"/p/".length) == "/p/")
async.waterfall([
function (callback)
{
if(path.substring(0,"/p/".length) == "/p/")
{
if(path.length < 7)
db.init(callback);
},
function (callback)
{
db.db.set("a","test");
db.db.get("a", function(err,value){
console.error(value);
})
var server = http.createServer(function(req, res){
var path = url.parse(req.url).pathname;
if(path.substring(0,"/static".length) == "/static" || path.substring(0,"/p/".length) == "/p/")
{
if(path.substring(0,"/p/".length) == "/p/")
{
if(path.length < 7)
send404(res, path);
path = "/static/padhtml";
}
sendFile(res, path, __dirname + "/.." + path);
}
else if(path == "/")
{
sendRedirect(res, path, "/p/test");
}
else if(path == "/newpad")
{
sendRedirect(res, path, "/p/" + randomPadName());
}
else if(path == "/ep/pad/reconnect")
{
if(req.headers.referer != null)
sendRedirect(res, path, req.headers.referer);
else
send404(res, path);
}
else
{
send404(res, path);
}
});
path = "/static/padhtml";
}
server.listen(settings.port);
console.log("Server is listening at port " + settings.port);
sendFile(res, path, __dirname + "/.." + path);
}
else if(path == "/")
{
sendRedirect(res, path, "/p/test");
}
else if(path == "/newpad")
{
sendRedirect(res, path, "/p/" + randomPadName());
}
else if(path == "/ep/pad/reconnect")
{
if(req.headers.referer != null)
sendRedirect(res, path, req.headers.referer);
else
send404(res, path);
}
else
{
send404(res, path);
}
});
server.listen(settings.port);
console.log("Server is listening at port 9001");
var io = socketio.listen(server);
var messageHandler = require("./MessageHandler");
messageHandler.setSocketIO(io);
function randomPadName() {
io.on('connection', function(client){
try{
messageHandler.handleConnect(client);
}catch(e){errorlog(e);}
client.on('message', function(message){
try{
messageHandler.handleMessage(client, message);
}catch(e){errorlog(e);}
});
client.on('disconnect', function(){
try{
messageHandler.handleDisconnect(client);
}catch(e){errorlog(e);}
});
});
callback(null);
}
]);
function randomPadName()
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 10;
var randomstring = '';
@ -117,28 +158,6 @@ function requestLog(code, path, desc)
console.log(code +", " + path + ", " + desc);
}
var io = io.listen(server);
var messageHandler = require("./MessageHandler");
messageHandler.setSocketIO(io);
io.on('connection', function(client){
try{
messageHandler.handleConnect(client);
}catch(e){errorlog(e);}
client.on('message', function(message){
try{
messageHandler.handleMessage(client, message);
}catch(e){errorlog(e);}
});
client.on('disconnect', function(){
try{
messageHandler.handleDisconnect(client);
}catch(e){errorlog(e);}
});
});
function errorlog(e)
{
var timeStr = new Date().toUTCString() + ": ";
@ -156,7 +175,3 @@ function errorlog(e)
console.error(timeStr + JSON.stringify(e));
}
}

View File

@ -18,6 +18,8 @@ var fs = require("fs");
//default settings
exports.port = 9001;
exports.dbType = "sqlite";
exports.dbSettings = { "filename" : "../var/sqlite.db" };
//read the settings sync
var settingsStr = fs.readFileSync("../settings.json");

View File

@ -6,7 +6,8 @@
"author" : "Peter 'Pita' Martischka <petermartischka@googlemail.com>",
"dependencies" : {
"socket.io" : ">=0.6.17",
"ueberDB" : ">=0.0.1"
"ueberDB" : ">=0.0.1",
"async" : ">=0.1.9"
},
"version" : "0.0.1",
"bin" : {

View File

@ -1,3 +1,7 @@
{
"port":9001
"port" : 9001,
"dbType" : "sqlite",
"dbSettings" : {
"filename" : "../var/sqlite.db"
}
}

1
var/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
sqlite.db