On Dec 10, 2:38 am, Aziz Light <
aziiz.li...@gmail.com> wrote:
> Hi everybody,
Hi Aziz!
I'm new to Sinatra as well so I hope I can help.
> Now as I've read in the documentation, there are two ways to run the
> app:
>
> - From
config.ru by requiring the app and using rack's run method to
> run it.
> - From a regular ruby file by using Sinatra's run! method.
These two method accomplish two different things. I use both. The
first one for production, where thin spawns multiple app processes
running on different ports. The second one for development, which
Sinatra actually instantiates a single instance of itself using thin.
I wonder if we could do away with the second one and not allow any of
these external type settings controllable by Sinatra. That may
simplify things a bit.
For development it is easy to fire up a single instance of the app by
running:
$ ruby main.rb
In fact, I don't utilze code reloading, but instead use the following:
$ while :; do ruby main.rb; done
Then before heading back to my browser after saving changes I do a
quick CTRL+C on that loop, which restarts the app in milliseconds. I
find this much more reliable and simpler than code reloaders. In fact,
I think the original code reloader was ripped from Sinatra because of
its complexity. BTW, restarting the app after changing views is not
required if page caching is not enabled.
> I am more comfortable with the second method, but I noticed that the
>
config.ru method was most widely used;
Again, that's because you are seeing production examples or examples
where guys just prefer to run thin directly for development and show-
and-tell.
> Now the thing that I don't understand is how to pass options (ie: the
> port number I want to app to run on) from
> Sinatra to the server.
Sinatra actually passes the port number, etc. to thin for
instantiating itself. However, thin is used to instantiate multiple
apps. To do this, use a thin config file. There are lots of options
for thin:
$ thin -h
For production, use:
$ thin -C thin-production.yml -R
config.ru start
Here is an example thin-production.yml file:
---
address: localhost
port: 3020
servers: 4
max_conns: 1024
max_persistent_conns: 512
timeout: 30
environment: production
pid: tmp/pids/thin-production.pid
log: log/thin-production.log
daemonize: true
You could use a different thin configuration file depending on your
needs or environment. For example, you probably want only one server
running in the foreground (non-daemonized) during development.
thin-production.yml above will instantiate four Sinatra apps running
at ports: 3020, 3021, 3022, 3023. Now your front-end webserver (e.g.
nginx) will proxy to these four ports. It looks like this:
nginx(80) -> thin(3020,3021,3022,3023) -> [rack -> sinatra]
> I tried every variation of the set method I knew
> of with no success (for more info on my
> failed attempts to achieve my goal, you can check out this
> stackoverflow question:
http://bit.ly/emkwhI
I'm heading over to Stackoverflow...