6.6. Handlers as GemPlugins
Handlers can also be GemPlugins, and they are the preferred way to distribute your handlers. If we take our UserAgentRemover handler and turn it into a GemPlugin, the code would be:
class UserAgentRemover < GemPlugin::Plugin "/handlers" include Mongrel::HttpHandlerPlugin def process(req, resp) if req.params.delete "HTTP_USER_AGENT" STDERR.puts "Removed a user agent" end end end uri "/dumped", :handler => plugin("/handlers/useragentremover")
The only change made to the code was to do the usual GemPlugin::Plugin voodoo, include the HttpHandlerPlugin module, and then use the Configurator#plugin function to set the :handler => plugin() setting.
6.6.1. More Examples
The Mongrel RubyForge project Files section has several other examples of using the GemPlugin system to create distributions of your handlers and commands. You should take a look at mongrel_upload_progress as a full example of implementing a useful set of handlers as GemPlugins.