Nuo Yan

Problem Solved

Recent Posts

Tags

News


  • Follow me on twitter: @nuoyan
    Make a donation to this Blog by PayPal. Thanks!






    Nuo is currently a Software Development Engineer in a Seattle-based software company.




    Locations of visitors to this page

    The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my school or employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.


Community

Email Notifications

Archives

July 2009 - Posts

Don't forget the port number when replicating CouchDB

In the afternoon I needed to replicate a database  from my co-worker's CoudhDB using CouchDBX. In the replicator, I entered my co-worker's database's path like the following:

http://10.0.0.3/TestDatabase

And I selected to replicate to one of my existing databases.  Everything seemed to be right (I could ping and telnet my co-worker's IP and CouchDB port 5984 without problem), but CouchDBX returned error 500, could not connect to remote database.

After a series of diagnostics, we finally realized, we forgot to put the port number in the path of remote database. When we changed the database path to http://10.0.0.3:5984/TestDatabase,  everything just worked very well.

I'm sharing this since I bet many people may encounter the same problem. Hope this is helpful.

Creating a Merb Rack middleware for running in Merb applications

There are a lot of resources on the Internet showing how to write and run a standalone rack application or middleware, but few showing how to write one to run in other Merb applications. This is actually very easy, so I'm going to share how I did it. Hopefully to be helpful to people in such need.

The goal is to have a rack middleware in a merb application, which can be installed as gem, and then can be ued in other merb applications.

First, create the merb application:

$ merb-gen app  application_name 

where application_name is your proposed application name.

Second, create your code folder and ruby file. I created a "lib" folder to be the container of my middlewares.

Third, open the ruby file and write your middleware. (In this example, let's name  it samplemiddleware.rb within the "lib/test" folder)

The class could inherit Merb::Rack::Middleware if you want. Below is an example.

module Test
  class SampleMiddleware < Merb::Rack::Middleware
   
    def initialize(app)
      @app = app
    end
   
    def deferred?(env)
      @app.deferred?(env) if @app.respond_to?(:deferred?)
    end
   
    def call(env)
      puts "do something here"
      @app.call(env)
    end
   
  end
end

Fourth, write your rake file, so the rack middleware merb application can be installed as a gem.

Fifth, install the gem.

So far we finished the middleware, now we go to the other merb application which we want the middleware to run within. Assuming the merb application has a rack configuration file (rack.rb) under the "config" folder. Open this file.

We need to require our middleware, in this case:

require 'test/samplemiddleware.rb''

And then use the middleware in the same file:

use Test::SampleMiddleware

Finally remember to have a line to run the Merb::Rack application (should already have it):

run Merb::Rack::Application.new

Now when the merb application runs, the rack middleware you wrote and installed will run as well. It's handy to use the middleware to process requests in your Merb applications before Merb handles the requests.

Posted: Mon, Jul 13 2009 20:54 by Nuo Yan | with no comments |
Filed under: