diff --git a/package.json b/package.json index 9eb6785..954a121 100644 --- a/package.json +++ b/package.json @@ -6,10 +6,11 @@ "dependencies": { "express": "3.x", "mongodb": "1.1.8", - "socket.io": "0.9.10" + "socket.io": "0.9.10", + "mongoose" : "3.8.12" }, "engines": { - "node": "0.8.4", - "npm": "1.1.49" + "node": "0.10.32", + "npm": "1.4.9" } } diff --git a/readme.md b/readme.md index 8732ef1..5979a2b 100644 --- a/readme.md +++ b/readme.md @@ -9,29 +9,10 @@ This application is further documented [here](http://coenraets.org/blog). The application is also hosted online. You can test it [here](http://nodecellar.coenraets.org). -## To run the application on your own Heroku account:## - -1. Install the [Heroku Toolbelt](http://toolbelt.heroku.com) - -2. [Sign up](http://heroku.com/signup) for a Heroku account - -3. Login to Heroku from the `heroku` CLI: - - $ heroku login - -4. Create a new app on Heroku: - - $ heroku create - -5. Add the [MongoLab Heroku Add-on](http://addons.heroku.com/mongolab) - - $ heroku addons:add mongolab - -6. Upload the app to Heroku: - - $ git push heroku master - -7. Open the app in your browser: - - $ heroku open - +## To run the application on your own Azure account:## +1. Setup mongo db to Azure VM - see setup folder for hints +2. In Azure websites create App settings: + a. MongoDbServer with DNS for the MongoDb which listens on port 27017 + a. MongoDbUserName with username to wine database - default user is wineUser + b. MongoDbPassword - password for the user with readWrite access + c. TODO create variable for the server \ No newline at end of file diff --git a/routes/wines.js b/routes/wines.js index 1626db8..88afc28 100644 --- a/routes/wines.js +++ b/routes/wines.js @@ -1,11 +1,49 @@ var mongo = require('mongodb'); + var Server = mongo.Server, Db = mongo.Db, BSON = mongo.BSONPure; + +var serverName = process.env.MongoDbServer || 'galini-mongodb.cloudapp.net', + usename = process.env.MongoDbUserName || 'wineUser', + password = process.env.MongoDbPassword; + +console.log('Connecting to wine database on ' + serverName + ' with user ' + usename + '... '); + +var server = new mongo.Server(serverName, 27017, { + auto_reconnect: true + }), + db = new mongo.Db('wine', server); -var server = new Server('localhost', 27017, {auto_reconnect: true}); -db = new Db('winedb', server, {safe: true}); +// callback: (err, db) +function openDatabase(callback) { + db.open(function(err, db) { + if (err) + return callback(err); + + console.log('Database connected'); + + return callback(null, db); + }); +} + +// callback: (err, collection) +function authenticate(db1, username, password, callback) { + db1.authenticate(username, password, function(err, result) { + if (err) { + return callback (err); + } + if (result) { + var collection = new mongo.Collection(db1, 'wines'); + + // always, ALWAYS return the error object as the first argument of a callback + return callback(null, collection); + } else { + return callback (new Error('authentication failed')); + } + }); +} db.open(function(err, db) { if(!err) { @@ -16,6 +54,8 @@ db.open(function(err, db) { populateDB(); } }); + } else { + console.log("Error while connecting to database " + JSON.stringify(err)); } }); diff --git a/setup/SetupDB.js b/setup/SetupDB.js new file mode 100644 index 0000000..0866af0 --- /dev/null +++ b/setup/SetupDB.js @@ -0,0 +1,14 @@ + +Run .\bin\mongo --port 27017 + + +use wine +db.createUser( + { + user: "wineUser", + pwd: "1XXXXXXX", + roles: [ + { role: "readWrite", db: "wine" }, + ] + } +) \ No newline at end of file diff --git a/setup/mongod.cfg b/setup/mongod.cfg new file mode 100644 index 0000000..a386118 --- /dev/null +++ b/setup/mongod.cfg @@ -0,0 +1,9 @@ +systemLog: + destination: file + path: "/mongodb/mongodb.log" + logAppend: true +storage: + journal: + enabled: true +net: + port: 27017 \ No newline at end of file diff --git a/setup/readme.md b/setup/readme.md new file mode 100644 index 0000000..fe5f8b2 --- /dev/null +++ b/setup/readme.md @@ -0,0 +1,25 @@ +Useful links + +md C:\mongodb\data +md c:\data\db + +Install windows service +cd "C:\Program Files\MongoDB 2.6 Standard" + +.\bin\mongod.exe --config "C:\mongodb\mongod.cfg" --install + +Useful links +http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/ +http://docs.mongodb.org/manual/reference/configuration-options/ +http://docs.mongodb.org/manual/tutorial/change-user-privileges/ +http://stackoverflow.com/questions/4870328/how-to-read-environment-variable-in-node-js +http://stackoverflow.com/questions/10108170/node-js-reuse-mongodb-reference + +To setup db user see SetupDb.js + +For Azure VM setup endpoint on port 27017 + +Open the firewall - http://itblog.gr/213/configuring-windows-firewall-from-the-command-line/ + +netsh advfirewall firewall add rule name="MongoDb" protocol=TCP localport=27017 action=allow dir=IN +