In Production! ;)

4 views
Skip to first unread message

Emanuele Tozzato

unread,
Nov 18, 2009, 10:22:48 AM11/18/09
to sina...@googlegroups.com
We finally have our first proof-of-concept Sinatra app in production
at http://www.headcounters.com !

It was so easy with Heroku! I'm not asking you to use it, but it would
be great to see some real life stress-test!

;)

--
Emanuele Tozzato
http://mekdigital.com

Paul Walker

unread,
Nov 19, 2009, 10:19:20 AM11/19/09
to sina...@googlegroups.com
I am looking for some feedback on best practices when defining an app
as subclass of Sinatra::Base. Up until now, I have always just
defined the routes at the top-level. While I have not experienced any
drawbacks to this, I would like to adhere to best practices.

When defining an app at the top-level, I have kept routes organized
into separate files and have simply referenced them from the app's
main file, keeping things neat and tidy. How do I mix in-routes to
the subclass from another file without doing something crazy hooking/
class_eval or defining a large inheritance chain? Am I missing a
basic ruby construct here?

Also, for those that sublass Sinatra::Base, do you usually do local
development by running a config.ru file? Or simply use a if __FILE__
== $0 condition at the bottom of your main file?

Finally, I'm having trouble finding open source sample Sinatra apps.
It would be great if the site could list a few, much as the webpy site
does, but I'd be happy if someone could post a link or two :-).

Much appreciated,
~Paul

Paul

unread,
Nov 19, 2009, 10:30:48 AM11/19/09
to sinatrarb
I'm sorry, I did not mean to hijack the original thread :-(

Damian Janowski

unread,
Nov 19, 2009, 10:34:28 AM11/19/09
to sina...@googlegroups.com
On Thu, Nov 19, 2009 at 12:19 PM, Paul Walker <pjwa...@gmail.com> wrote:
> I am looking for some feedback on best practices when defining an app
> as subclass of Sinatra::Base.  Up until now, I have always just
> defined the routes at the top-level.  While I have not experienced any
> drawbacks to this, I would like to adhere to best practices.
>
> When defining an app at the top-level, I have kept routes organized
> into separate files and have simply referenced them from the app's
> main file, keeping things neat and tidy.  How do I mix in-routes to
> the subclass from another file without doing something crazy hooking/
> class_eval or defining a large inheritance chain?  Am I missing a
> basic ruby construct here?

Here's one strategy: http://github.com/monkrb/skeleton/blob/master/init.rb

Then you start with `ruby init.rb`.

I usually keep config.ru for production.

> Also, for those that sublass Sinatra::Base, do you usually do local
> development by running a config.ru file?  Or simply use a if __FILE__
> == $0 condition at the bottom of your main file?
>
> Finally, I'm having trouble finding open source sample Sinatra apps.
> It would be great if the site could list a few, much as the webpy site
> does, but I'd be happy if someone could post a link or two :-).

There's a super simple Reddit-like app running at
http://news.monkrb.com and you can find the source code at
http://github.com/monkrb/reddit-clone.

Hope that helps.
D.

Jesús Gabriel y Galán

unread,
Nov 19, 2009, 10:53:08 AM11/19/09
to sina...@googlegroups.com
On Thu, Nov 19, 2009 at 4:19 PM, Paul Walker <pjwa...@gmail.com> wrote:
> I am looking for some feedback on best practices when defining an app
> as subclass of Sinatra::Base.  Up until now, I have always just
> defined the routes at the top-level.  While I have not experienced any
> drawbacks to this, I would like to adhere to best practices.
>
> When defining an app at the top-level, I have kept routes organized
> into separate files and have simply referenced them from the app's
> main file, keeping things neat and tidy.  How do I mix in-routes to
> the subclass from another file without doing something crazy hooking/
> class_eval or defining a large inheritance chain?  Am I missing a
> basic ruby construct here?
>
> Also, for those that sublass Sinatra::Base, do you usually do local
> development by running a config.ru file?  Or simply use a if __FILE__
> == $0 condition at the bottom of your main file?

Well, I don't do much Sinatra development, but I've done a couple of
little applications and what I did was to subclass Sinatra::Base like:

require 'sinatra/base'

class MyClass < Sinatra::Base
configure do
...
end
before do
...
end

get '/' do
...
end
end

MyClass.run!

In order to separate the routes in different files, you can do this:

require 'sinatra/base'
require 'myroutes'

class MyClass < Sinatra::Base
configure do
...
end
before do
...
end
end

MyClass.run!

----------------
and in myroutes.rb reopen the class:

class MyClass < Sinatra::Base
get '/' do
...
end
...
end

Maybe there's a better way.


Jesus.

Paul Walker

unread,
Nov 19, 2009, 11:24:17 AM11/19/09
to sina...@googlegroups.com
Thank you, I tend to forget Ruby's ability to re-open a class. That's
the construct I'm looking for ;-).
> --
>
> You received this message because you are subscribed to the Google
> Groups "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=
> .
>
>

Paul Walker

unread,
Nov 19, 2009, 11:25:28 AM11/19/09
to sina...@googlegroups.com
Thanks, Monk looks interesting (although perhaps a bit heavy handed
for me right now)!

~Paul

Kyle Drake

unread,
Nov 20, 2009, 1:58:21 PM11/20/09
to sina...@googlegroups.com
This is a wonderfully simple strategy for separating out routes, I may
start using this. There's some stuff in that monk strategy that I
liked too. Might put some of this into my sammy_davis_jr reference
implementation. Thanks guys!

-Kyle

2009/11/19 Jesús Gabriel y Galán <jgabrie...@gmail.com>:

Paul Walker

unread,
Nov 20, 2009, 2:09:49 PM11/20/09
to sina...@googlegroups.com
Yeah, Monk has some good stuff including the general patterns and
loving the reloader and dependencies, although I've had to do some
hackery to implement it without having to take haml and sass along for
the ride. Fyi, your referenced files that add routes to classes do
not have to also inherit via Sinatra::Base via < Sinatra::Base as this
is already done in your main file. Here is the current state of my
init.rb:

RACK_ENV = ENV["RACK_ENV"] ||= "development" unless defined? RACK_ENV
ROOT_DIR = File.expand_path(File.dirname(__FILE__)) unless defined?
ROOT_DIR

def root_path(*args)
File.join(ROOT_DIR, *args)
end

begin
require "vendor/dependencies/lib/dependencies"
rescue LoadError
require "dependencies"
end

require 'sinatra/base'
require 'erubis'

module Monk; module Glue; end; end

class Main < Sinatra::Base
set :dump_errors, true
set :logging, true
set :raise_errors, false
set :root, root_path
set :app_file, __FILE__
set :run, Proc.new { $0 == app_file }
set :show_exceptions, true
set :static, true
set :views, root_path("app", "views")

get '/' do
response_helper
# eruby :index
end

configure :development do
require "monk/glue/reloader"
use Monk::Glue::Reloader
end

end

#Load all application files.
Dir[root_path("app/**/*.rb")].each { |file| require file }

Main.run! if Main.run?
> To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages