4.1. Configuring Mongrel
Mongrel lets you configure and extend it through Yaml configuration files. You can write these by hand or pass configuration options into Mongrel, and it will generate one for you that you can later pass as a parameter.
In most cases, during development you won't need to change the defaults. A common practice is to daemonize the Mongrel process, and when it's necessary to run multiple Rails applications at once, specify the port, like so:
$ mongrel_rails -d -p 3001
This can become tedious if you have to type it frequently, so in keeping with the Don't Repeat Yourself principle, Mongrel will take in a config file (see the -C parameter) in YAML format that sets these parameters for you. It even comes with a Generator parameter (see the -G parameter) that will generate this config file for you. You can tell Mongrel to put the file wherever you want, and include all the parameters you want to set. In this example we put it in Rails' config directory.
$ mongrel_rails start -d -p 3001 -G config/bam.yml ** Writing config to config/bam.yml ** Finished. Re-run without -G and WITH -C using your new config file.
The content of your config file should look like this:
--- :config_script: :debug: false :pid_file: log/mongrel.pid :num_processors: 1024 :docroot: public :user: :host: 0.0.0.0 :mime_map: :prefix: :group: :port: '3001' :daemon: true :cwd: /Users/matt/examples/bam :config_file: :includes: - mongrel :environment: development :timeout: 0 :log_file: log/mongrel.log
We can now run Mongrel without having to type the config in every time:
$ mongrel_rails start -C config/bam.yml
4.1.1. Mongrel Scripts
If you want to mount a handler yourself, you can tell mongrel_rails to run a script file. It's just a Ruby file, so you can require or include whatever code you need. However, don't name the file with an .rb extension, because Rails will automatically load it if it is in the load path. We usually use .conf, though it doesn't really matter.
Mongrel has a DirHandler that will list the contents of a directory, so we'll mount the /drawing prefix to point to a directory on our system that we want to let users browse. You could mount a directory from anywhere in your file system. Let's create a file drawings.conf with the following content:
uri '/your_face', :handler => DirHandler.new('/Users/matt/examples/your_face')
If you are using a config file (like we just used above), you'll need to regenerate it with all the correct options, or you can just edit the existing config manually. Mongrel will give precedence to the parameter values in the config file over those in the command line. Since the -G parameter writes out all the parameters (even the empty ones, like :config_script in the above example), if you tried to run the following command, Mongrel would NOT load the script file.
$ mongrel_rails start -C config/bam.yml -S config/drawings.conf
Instead you should change the bam.yml file's existing :config_script parameter to config/drawings.yml, and it will work like a charm.