Posts Tagged ‘short’

Getting Started with Pancake

November 15, 2009

It’s been a really busy week at work this week 🙂 Now I’ve got a chance to write a quick “getting started” post I thought I’d show everyone how to get going with Pancake.

Pancake is designed to be flexible. You can have a single application contained in a config.ru, a two file config.ru + application file, or something with a bit more structure like a “micro” or “short” stacks.

I’ll just focus on the micro and short generated stacks today. They’re both Pancake “Short” stacks, but the generators are suited to different purposes. To get started you can use pancakes generator “pancake-gen”

Micro

$ pancake-gen micro my_stack

This generator will output enough code to generate your Short Stack app, make Passenger happy, and be able to mount it inside another stack.

The main application file here is “my_stack.rb” and there’s a Rakefile, config.ru, and public and temp directories for Passenger. You’ll also see a pancake_init.rb file. This file is not used unless you’re mounting this inside another stack (we’ll get to that later)

If you take a look in the my_stack.rb file, you’ll see that there’s a simple action in there. With any kind of Short Stack, just return your response at the end of the action and your gold. Pancake and Rack will look after the rest.

Views are stored in the “views” directory in the <template_name>.<format>.<render_engine> format.

Short

$ pancake-gen short my_stack

When you generate a stack with the “short” argument, you get a bit more structure. The biggest difference is that a “short” generated stack has everything you need to make the stack into an independent gem.

$ rake -T

Shows you all the Jeweler goodness for gemming up your stack.
The real working directory in this kind of stack is at lib/my_stack There’s a few more directories generated in there with this one. Mostly it’s just a bit more structure for a larger application.

Mounted Stacks

Ok so after that extraordinarily brief rundown of where each of the generated stack types fit, lets generate and mount these bad boys.


$ pancake-gen micro app_container
$ cd app_container
$ mkdir mounts
$ cd mounts
$ pancake-gen short mounted


Great, now the "mounted" stack will get loaded when you start up the app_container stack. The AppContainer stack will load all the mounts by loading mounts/*/pancake_init.rb You can use that to load anything you want. Another Rack application like Sinatra, a custom bit of kit. Even ActiveRecord plugins if you wanted.
As it stands right now, there's no way to access the mounted app in the browser though so you need to mount it in url space.
Open up the app_container.rb file and get it looking like this:


class AppContainer < Pancake::Stacks::Short
  add_root(__FILE__)
  initialize_stack

  router do |r|
    r.mount(Mounted, "/mount")
  end
 
  get "/", :_name => :home do
    "You're in the App!"
  end
end

Here can see we're also initializing the stack. When we do that, all the mounted apps and models are loaded which we need to do prior to mounting the apps in the router.

You may want to edit the "mounted" app to show something interesting.


  # mounts/mounted/lib/mounted/mounted.rb
class Mounted
  get "(/)" do
    "You're in Mounted!"
  end
end

I tend to use shotgun when developing. This is a code reloading rack adapter and suits very well 🙂 I also tend to tell it to use thin and start on port 5000. Go back to the "app_container" directory, so you're in the directory with the config.ru file.

$ shotgun -s thin -p 5000

Now you can visit http://localhost:5000/ and http://localhost:5000/mount to see your nice shiny new mounted application 🙂
When you're ready to give it a non-reloaded squirt, I use unicorn to start.

$ unicorn -p 5000

it's much faster.

Although we haven't gone into the ins and outs of how to write pancake apps. At least we can now generate them and get started.