From b34d0c90650af3d80268a83002707631722e9d27 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 8 Apr 2010 00:46:21 +0800 Subject: [PATCH] Added DB username/password. --- lib/rack/gridfs.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/rack/gridfs.rb b/lib/rack/gridfs.rb index 40341cb..9330741 100644 --- a/lib/rack/gridfs.rb +++ b/lib/rack/gridfs.rb @@ -18,12 +18,12 @@ def initialize(app, options = {}) }.merge(options) @app = app - @hostname = options[:hostname] - @port = options[:port] - @database = options[:database] @prefix = options[:prefix] @db = nil + @hostname, @port, @database, @username, @password = + options.values_at(:hostname, :port, :database, :username, :password) + connect! end @@ -44,14 +44,14 @@ def gridfs_request(id) end private - - def connect! - Timeout::timeout(5) do - @db = Mongo::Connection.new(hostname).db(database) + def connect! + Timeout::timeout(5) do + @db = Mongo::Connection.new(hostname, port).db(database) + @db.authenticate(@username, @password) if @username + end + rescue Exception => e + raise Rack::GridFSConnectionError, "Unable to connect to the MongoDB server (#{e.to_s})" end - rescue Exception => e - raise Rack::GridFSConnectionError, "Unable to connect to the MongoDB server (#{e.to_s})" - end end