Getting camping working with fcgi on a shared host (DreamHost), part 1

Posted by Tim Connor Fri, 06 Jul 2007 16:04:00 GMT

Since I’ve been splitting up an app into an rails admin section and a separate camping front-end, I need to get camping working on the host to deploy. And that host, for this app, happens to be DreamHost, which means it’s gotta be fcgi. I’m not all the way done with that, but after a late night last night, I managed to get it running on fcgi locally. I’ll skip over the basics (for one thing, if you haven’t gotten this far yet, you are going to need to struggle through it to be familiar enough with what is happening to have a chance of finishing the process) until I have it all worked out smoothly, but I have to post a couple highlights, to spare some people with the same pain.

First off, no luck running off of edge. The only way I got it working consistently with mongrel, cgi (crucial for debugging and figuring out why things aren’t working), and fcgi, was to use the gem release (1.5.180), and patch that (keep reading). Once I had that running smoothly with the built-in server and then cgi, I got it working with fcgi. If you have the same problem I did, you’ll get an “`camp_do’: undefined local variable or method `exc’” error. THis is because there is another problem, which is throwing an error, but there is a bug in the exception handling code. Manually fix that (change e on to exc in the exception catching, so that it is defined), and then you can move on to the next issue, since your exceptions will now actually get through to the log, or the screen, depending on how far you’ve made it, and you can use them for more debugging.

If you have my problem, you can track down the next error to the path variable not being populated right in the fastcgi.rb


def camp_do(req)
root, path, dir, app = “/”
if ENV[‘FORCE_ROOT’] and ENV[‘FORCE_ROOT’].to_i == 1
path = req.env[‘SCRIPT_NAME’]
else
root = req.env[‘SCRIPT_NAME’]
path = req.env[‘PATH_INFO’]
end

Wether or not I set ENV[‘FORCE_ROOT’] the wrong server variables are being set for camping to know the right path. This shows up as gsub on nil errors, or “/dispatch.fcgi not found”, or some others, depending on exactly which way you are running things. This all comes back to not being able to use ScriptAlias, as in the wiki, due to only having .htaccess level access. And using RedirectRule and other mod_rewrite stuff that workins in .htaccess (I actually just stole that right from my rails .htaccess) populates things a little bit different.

My fix was to set “ENV[”FORCE_ROOT"]=1.to_s" in my dispatch.fcgi, and then change the corresponding “path =” line to match what my set-up was populating -req.env[‘REQUEST_URI’]. I tracked this down by getting things running well enough to get the default error dump to the browser, and then checking over that.

Now I need to duplicate this without using global gems (since I can’t upgrade them on the shared host), and the do more debugging on the actual host.

A separate admin app, aka sharing a database between rails and camping 1

Posted by Tim Connor Thu, 28 Jun 2007 19:03:00 GMT

I realized that pretty much all of my controllers for a certain rails app were the basic rest actions + public_show and public_index. I thought about it and realized it would be easier all around if those couple actions were just spun out into a different app. Then the admin app could be pure rest, with no extra actions and the various settings that differ between the two (ssl or not, sessions on or not, login required or not, read-write or read-only database access) could be set at the app level, instead of per action. All at once my app would be much simplified, and as a bonus, more secure. And there are obvious scaling/performance benefits to splitting them up.

After getting most of the way through this, I have to say I really like camping, and doing things this way has definitely simplified both codebases. I didn’t realize how little was actually shared between the two aspects. At first I was going to try and share the model code between the two apps, and deal with the hassle of merging back and forth, or having a symlink to shared folder (I have been playing around with svk again, for my disconnected/laptop development, and that means no svn:externals – I’ll write later how I’ve decided that is mostly a useful constraint), but then I realized even my models files could be partitioned better. Some models weren’t even needed on the front-end, on the ones that were most of the relationships/associations weren’t, and on the rest most of the methods were either/or public/admin.

So all of a sudden all models (not to mention the controllers) on the front-end became, much, much slimmer, to the point of just usually just being “class ModelName < Base; end” or maybe one association or a finder or two. Obviously this is easier to test, secure, and maintain. The only hiccup was that camping assumes the possibility of multiple apps per site, and thus prefixes the app name to the table. While you could make database views named app_model, for camping to access, it was simpler to just overload and cancel out the prefixing:

module MyApp::Models
  def Base.table_name_prefix; end

As, I’ve discovered, one of the benefits of going camping is it encourages you to simplify the code – to the point of making things short one-liners. I also think that it’s helping my ruby skills to do more development outside of rails, even if it is still in a similar venue. For example, since I couldn’t figure out how to do the rails style content_for/capture and multi-yield, I had to be more creative with blocks in my partials (which, just being methods like any other view, can be called with a block – I’ll write about that later, too). Also for smaller apps, or parts of them, I really like the simplified routing/controller/link-generation trifecta. Oh, R(), how I love thee. I think Rails could actually learn a thing or two about the possible simplifications, but then again you can just use camping when rails is overkill.