Posts Tagged ‘mongodb’

Rails Metal MongoDB GridFS access

Friday, November 20th, 2009

I’m kind of new to Metal (and all things Rack) but this works. Just for reference.

# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
class ImageShow
 
  def self.call(env) 
    request = Rack::Request.new(env)
    if request.path_info =~ /^\/show_image\/(.+)$/
      if GridFS::GridStore.exist?(Media.database, $1)
        GridFS::GridStore.open(Media.database, $1, 'r') do |file|
          [200, {'Content-Type' => file.content_type}, [file.read]]
        end
      else
        [404, {'Content-Type' => 'text/plain'}, ['File not found.']]
      end
    else
      [404, {"Content-Type" => "text/html"}, ["Not Found"]]
    end
 end
 
end

That will need to be saved as image_show.rb. Slow .. but not as slow as doing it through a Rails controller.

Installing nginx-gridfs on RHEL5

Thursday, October 8th, 2009

Now, installing on RHEL5. This took a while to work out!

cd ~/src
mkdir nginx_gridfs && cd nginx_gridfs
 
yum install scons boost boost-devel pcre pcre-devel
 
git clone git://github.com/mdirolf/nginx-gridfs.git  
wget http://sysoev.ru/nginx/nginx-0.7.62.tar.gz
tar -zxvf nginx-0.7.62.tar.gz
 
#note my mongo install path; different from on OSX
export MONGO_INCLUDE_PATH=/root/src/mongodb-linux-x86_64-1.0.0/include/mongo/
export LIBMONGOCLIENT=/root/src/mongodb-linux-x86_64-1.0.0/lib64/libmongoclient.a
export BOOST_INCLUDE_PATH=/usr/lib64/
export LIBBOOST_THREAD=/usr/lib64/libboost_thread.so
export LIBBOOST_FILESYSTEM=/usr/lib64/libboost_filesystem.so
 
cd nginx-0.7.62/
 
./configure --add-module=/root/src/nginx_gridfs/nginx-gridfs
make
make install

I’d like to point out that this install was by pure trial and error; a lot of trials, and a lot of errors, since I have no clue whatsoever about C++. The above Works For Me™ but is probably suboptimal and your mileage may vary.

update: it should be noted that this is very early version software; there are bugs and it’s probably not a production option yet. Great potential though, so check it out.

Installing Nginx-GridFS on OSX

Wednesday, October 7th, 2009

Follow these steps to install ’s on OSX. Linux coming soon.

cd ~/src # your sources directory
mkdir nginx_gridfs && cd nginx_gridfs
git clone git://github.com/mdirolf/nginx-gridfs.git  
wget http://sysoev.ru/nginx/nginx-0.7.62.tar.gz
tar -zxvf nginx-0.7.62.tar.gz
 
sudo port install boost pcre++ spidermonkey scons # macports!
 
cd nginx-0.7.62/
 
# i put mongodb at /mongodb - alter to fit your installation
export MONGO_INCLUDE_PATH=/mongodb/include/mongo/ 
export LIBMONGOCLIENT=/mongodb/lib/libmongoclient.a
export BOOST_INCLUDE_PATH=/opt/local/include/
export LIBBOOST_THREAD=/opt/local/lib/libboost_thread-mt.a
export LIBBOOST_FILESYSTEM=/opt/local/lib/libboost_filesystem-mt.a 
export LIBBOOST_SYSTEM=/opt/local/lib/libboost_system-mt.a 
 
./configure --add-module=/Users/sho/src/nginx_gridfs/nginx-gridfs
make
sudo make install

In nginx.conf:

server {
    listen       80;
    server_name  localhost;
 
    location / {
        root   html;
        index  index.html index.htm;
    }
 
    location /gridfs/ {
        gridfs;
        gridfs_db database-name;
 
        # these are the default values:
        mongod_host 127.0.0.1:27017;
        gridfs_root_collection fs;
    }
 
    # blah
 
}

You should now be able to start nginx using /usr/local/nginx/sbin/nginx and navigate to 127.0.0.1/gridfs/#{file_name}.

UPDATE: for snow leopard, python is in /usr/bin, so you may need to symlink it like this for scons to find it:

$ sudo ln -s /usr/bin/python2.5 /opt/local/bin/python2.5

Also, nginx is updated to 0.8.30 now; these instructions work for the latest version too.

UPDATE 2: Currently the best version of this to use is , which simply pulls together a number of different patches to make a working version. Well, works for me, anyway!