diff options
-rw-r--r-- | node/db.js | 45 | ||||
-rw-r--r-- | node/server.js | 139 | ||||
-rw-r--r-- | node/settings.js | 2 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | settings.json | 6 | ||||
-rw-r--r-- | var/.gitignore | 1 |
6 files changed, 132 insertions, 64 deletions
diff --git a/node/db.js b/node/db.js new file mode 100644 index 00000000..b91fd7a7 --- /dev/null +++ b/node/db.js @@ -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); + } + }); +} diff --git a/node/server.js b/node/server.js index 731f6b63..1787cd64 100644 --- a/node/server.js +++ b/node/server.js @@ -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) + { + db.init(callback); + }, + function (callback) { - if(path.substring(0,"/p/".length) == "/p/") - { - if(path.length < 7) + 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); + } + }); + + server.listen(settings.port); + console.log("Server is listening at port " + settings.port); + + var io = socketio.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);} + }); + }); - 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); + callback(null); } -}); -server.listen(settings.port); -console.log("Server is listening at port 9001"); +]); -function randomPadName() { +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)); } } - - - - diff --git a/node/settings.js b/node/settings.js index 6638941f..7cee8b6f 100644 --- a/node/settings.js +++ b/node/settings.js @@ -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"); diff --git a/package.json b/package.json index 84d22f64..846ad399 100644 --- a/package.json +++ b/package.json @@ -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" : { diff --git a/settings.json b/settings.json index 3d7f6e2e..9ba17de6 100644 --- a/settings.json +++ b/settings.json @@ -1,3 +1,7 @@ { - "port":9001 + "port" : 9001, + "dbType" : "sqlite", + "dbSettings" : { + "filename" : "../var/sqlite.db" + } } diff --git a/var/.gitignore b/var/.gitignore new file mode 100644 index 00000000..7ce04197 --- /dev/null +++ b/var/.gitignore @@ -0,0 +1 @@ +sqlite.db
\ No newline at end of file |