From 88671b04d896d273d3a5cfcfe1205eceecef7dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Wn=C4=99trzak?= Date: Thu, 14 Jul 2011 10:52:09 +0200 Subject: [PATCH] configuration file for node --- .gitignore | 1 + README.rdoc | 7 ++- app-node/config.json | 1 - app-node/config.json.example | 6 +++ app-node/server.js | 50 +++++++++++----------- app/controllers/node/base_controller.rb | 3 +- app/controllers/node/members_controller.rb | 1 - config/deploy.rb | 1 + config/initializers/node_config.rb | 2 + 9 files changed, 40 insertions(+), 32 deletions(-) delete mode 100644 app-node/config.json create mode 100644 app-node/config.json.example create mode 100644 config/initializers/node_config.rb diff --git a/.gitignore b/.gitignore index a5db633..f0f12bb 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ db/*.rdb config/redis.yml config/database.yml +app-node/config.json diff --git a/README.rdoc b/README.rdoc index d89c3de..d825ede 100644 --- a/README.rdoc +++ b/README.rdoc @@ -22,14 +22,17 @@ If you are using rvm, create new gemset to not mess with your default gems cp config/redis.yml.example config/redis.yml cp config/database.yml.example config/database.yml + cp app-node/config.json.example app-node/config.json 3. Setup Redis Run redis (download from http://redis.io/download) - tested on 2.0 and 2.2 stable releases -4. Run node server (download from http://nodejs.org/#download) - tested on (v0.4.3, v0.4.7-v.0.4.9) +4. Run node server (download from http://nodejs.org/#download) - tested on (v0.4.3, v0.4.7-v.0.5.0) - node app-node/server.js +Go to app-node directory directly (this is necessary to load configuraion file using relative path see https://github.com/joyent/node/issues/1326) + + cd app-node && node server.js Tip: You can also use nvm https://github.com/creationix/nvm diff --git a/app-node/config.json b/app-node/config.json deleted file mode 100644 index b660ffa..0000000 --- a/app-node/config.json +++ /dev/null @@ -1 +0,0 @@ -{"username": "node", "pasword": "secret"} diff --git a/app-node/config.json.example b/app-node/config.json.example new file mode 100644 index 0000000..0a7acf1 --- /dev/null +++ b/app-node/config.json.example @@ -0,0 +1,6 @@ +{ + "username": "node", + "pasword": "secret", + "host": "localhost", + "port": 3000 +} diff --git a/app-node/server.js b/app-node/server.js index 6998c3f..38c332b 100644 --- a/app-node/server.js +++ b/app-node/server.js @@ -142,7 +142,7 @@ var initializeClientConnections = function() { setTimeout(function() { redisClient.hkeys(supervisionSessionsKey, function(err, resp) { if (!_und.include(resp, userId)) { - PGS.request(PGS.supervision_members_path(supervisionId, userId), "DELETE"); + PGS.request(PGS.node_supervision_member_path(supervisionId, userId), "DELETE"); } }); }, 20000); @@ -161,42 +161,43 @@ var initializeClientConnections = function() { } var PGS = { - //username: "node", - - //password: "secret", + initialize: function() { + var config; + fs.readFile('./config.json', 'utf8', function (err, data) { + if (err) throw err; + config = JSON.parse(data); + PGS.username = config.username; + PGS.password = config.password; + PGS.host = config.host; + PGS.port = config.port; + }); + }, auth_header: function() { return "Basic " + new Buffer(PGS.username + ":" + PGS.password).toString("base64"); }, - supervision_members_path: function(supervisionId, memberId) { + node_supervision_member_path: function(supervisionId, memberId) { return "/node/supervisions/" + supervisionId + "/members/" + memberId; }, request: function(path, method) { - //var body = JSON.stringify({ "supervision_membership": {"firstname": "Joe", "lastname": "Bloggs"} }) var options = { - host: 'localhost', - port: 3000, - path: path, - method: method, - headers: {"Content-Type": "application/json", "Accept": "application/json", "Authorization": PGS.auth_header()}//, "Content-Length": Buffer.byteLength(body)} + host: PGS.host, + port: PGS.port, + path: path, + method: method, + headers: {"Content-Type": "application/json", "Accept": "application/json", "Authorization": PGS.auth_header()} }; var req = http.request(options, function(res) { - console.log('STATUS: ' + res.statusCode); - res.setEncoding('utf8'); - res.on('data', function (chunk) { - console.log('BODY: ' + chunk); - }); + res.setEncoding('utf8'); }); req.on('error', function(e) { - console.log('problem with request: ' + e.message); + console.log('problem with request: ' + e.message); }); - // write data to request body - //req.write(body); req.end(); } } @@ -247,10 +248,7 @@ var pingRedisClient = function(){ }; setInterval(pingRedisClient, 10000); -fs.readFile('/home/wojtek/Ruby/peergroup/app-node/config.json', 'utf8', function (err, data) { - if (err) throw err; - var config = JSON.parse(data); - PGS.username = config.username; - PGS.password = config.password; - console.log(JSON.parse(data)); -}); +/* + * Setup configuration to connect to rails application + */ +PGS.initialize(); diff --git a/app/controllers/node/base_controller.rb b/app/controllers/node/base_controller.rb index 8da52d9..e01b188 100644 --- a/app/controllers/node/base_controller.rb +++ b/app/controllers/node/base_controller.rb @@ -1,10 +1,9 @@ class Node::BaseController < ApplicationController - # protect_from_forgery :only => [] before_filter :authenticate_node def authenticate_node authenticate_or_request_with_http_basic do |username, password| - username == "node" && password == "secret" + username == NODE_CONFIG[:username] && password == NODE_CONFIG[:password] end end end diff --git a/app/controllers/node/members_controller.rb b/app/controllers/node/members_controller.rb index 9363b33..d659149 100644 --- a/app/controllers/node/members_controller.rb +++ b/app/controllers/node/members_controller.rb @@ -4,7 +4,6 @@ class Node::MembersController < Node::BaseController def destroy @membership = supervision.memberships.find_by_user_id!(params[:id]) @membership.destroy - Rails.logger.info(@membership.inspect) respond_with @membership end diff --git a/config/deploy.rb b/config/deploy.rb index baad021..024c516 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -39,6 +39,7 @@ task :link do run "ln -nfs #{shared_path}/config/database.yml #{latest_release}/config/database.yml" run "ln -nfs #{shared_path}/config/redis.yml #{latest_release}/config/redis.yml" + run "ln -nfs #{shared_path}/app-node/config.json #{latest_release}/app-node/config.json" end namespace :apache do diff --git a/config/initializers/node_config.rb b/config/initializers/node_config.rb new file mode 100644 index 0000000..bce8d08 --- /dev/null +++ b/config/initializers/node_config.rb @@ -0,0 +1,2 @@ +# Create seperate config file when node will be moved from rails app repository +NODE_CONFIG = JSON.parse(File.read(Rails.root.join("app-node", "config.json"))).symbolize_keys