Skip to content

Instantly share code, notes, and snippets.

@TheNicholasNick
Created July 10, 2009 09:00
Show Gist options
  • Save TheNicholasNick/144360 to your computer and use it in GitHub Desktop.
Save TheNicholasNick/144360 to your computer and use it in GitHub Desktop.
load multiple sinatra apps
**After the fact**
namelessjon pointed me in the right direction.
map /asdasdasd do
run app
end
request.env['SCRIPT_NAME'] = "/asdasdasd"
also this is all in gem form: http://github.com/emk/sinatra-url-for/tree/master
I needed a way to have my apps be under scm and all config stuff be somewhere else.
Then I can do this:
* Checkout apps into own directories
* Configure config.ru
* thin start -R config.ru
Considerations:
* I'll always run the stack via a config.ru (either standalone app or bunch of standalone apps in a stack)
* I want to have a stack as a combo git submodules therefore apps need to be standalone and play nice together
* This an idea I came up with quickly - I have limited ruby knowledge, I'm maxing out my ruby fu here ;)
* I also use the merb (thor) bundler - for quickness none of that is shown...
* As long as the app has a unique name it won't clash with other apps. obviously "blog" would if there were two apps called "blog". My apps aren't named with generic adjectives, I use proper nouns.
Directory Structure
|_app_dir/
|_lib/
|_ blog/
|_blog/
|_views/
|_posts/
|_index.haml
|_new.haml
|_static/
|_logo.png
|_blog.rb
|_config.ru
# run with thin start -R config.ru
require "haml"
require "sinatra/base"
##
$:.unshift "#{pwd}/lib/blog" # <-pretty sure i've read something that discourages this - however I haven't found the "proper" way to do it. Suffice to say this "works"....
require "blog"
map "/blog" do
run Blog::Main
end
module Blog
class Main < Sinatra::Base
set :app_file, __FILE__
set :static, true
set :public, Proc.new { File.join(root, "blog", "static") }
set :views, Proc.new { File.join(root, "blog", "views") }
helpers do
# http://github.com/namelessjon has shown me the light ;)
def link_to(uri = "/")
"#{request.env['SCRIPT_NAME']}#{uri}"
end
end
get "/posts" do
haml :"posts/index"
end
get "/posts/new" do
haml :"posts/new"
end
end
end
%img{ :src => link_to("/logo.png"), :alt => "logo"}
%h1 Posts - Index
%a{ :href => link_to("/posts/new"), :title => "Create a New Post" } New
%img{ :src => link_to("/logo.png"), :alt => "logo"}
%h1 Post - New
%a{ :href => link_to("/posts"), :title => "Cancel and go back to index" } Cancel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment