How do you choose your HTTP server in Sinatra? - sinatra

I am running a Sinatra application locally.
Ever since I installed Thin, my Sinatra app always uses it as the web server. However, I have been having performance issues with Thin serving the large amount of static files required by my application, and I would like to switch to either Mongrel or WEBrick.
I cannot seem to figure out how to switch my application over to Mongrel or WEBrick. Even when I put require 'mongrel' or require 'webrick' at the top of my app.rb, upon a ruby app.rb, Sinatra still starts with Thin.
So, my question is: how do you specify the web server for Sinatra to use in development mode? Also, how could I do so in production mode?

You can set the server Sinatra uses with the :server configuration setting:
set :server, 'webrick' # or thin, mongrel
In production, it is much better to use a more sophisticated server like Phusion Passenger or Unicorn, since they have better performance than Thin, Mongrel, or WEBrick. If you choose Passenger or Unicorn, you would not configure the server within your Sinatra application file itself, but instead typically configure it separately using a Rackup config.ru file.
"Ruby on Rails Server options" is aimed at Rails applications, but is still very relevant for Sinatra apps.

Related

Why run Quart app with Hypercorn / isn't it automatic?

If you inspect the Quart library, app.run() just establishes some config and then uses asyncio.run(serve(self, config)), where serve comes from from hypercorn.asyncio import serve.
So even if you run a Quart app via python myapp.py, isn't it already using a Hypercorn server?
In particular, what's the difference between this and running via hypercorn myapp:app?
https://pgjones.gitlab.io/quart/deployment.html
It is not recommended to run Quart directly (via run()) in production. Instead it is recommended that Quart be run using Hypercorn or an alternative ASGI server. Hypercorn is installed with Quart and is used to serve requests by default (e.g. with run()).
So it sounds like, even though Hypercorn is used to serve requests by default with run(), it is not recommended to use run()? Is anyone else confused?
So even if you run a Quart app via python myapp.py, isn't it already using a Hypercorn server?
Yep.
In particular, what's the difference between this and running via hypercorn myapp:app?
I want to reserve the run method for development so that it can by default make decisions that are beneficial for development, but bad for production. An example at the moment is that the run method uses the reloader (reloads the app whenever the code changes) by default, which is great when developing but a performance issue in production. Another example is that the run method will not use multiple workers, which again leads to worse performance in production.
(I am the Quart author)

A good development workflow for static sites with one or two PHP files?

so I'm still learning a lot about best practices and local development workflows when working on my web projects. At the moment, when I develop a static site locally I tend to use Grunt + Bower + some static site generator to start up a local server really quickly. On the other hand when I'm working on a more PHP focused project I will set up a Vagrant box, depending on the focus. My question though is what is the best way to locally develop static sites that have one or two PHP files within them, such as a handler for a contact form? I can't run the PHP on my local server under grunt (at least as my current workflow stands — I use the browsersync plugin — is there a way to do this?), but using Vagrant seems like overkill, especially sometimes when I've settled into using a static site generator, and later realise I need to write a server side script.
You can use nginx to reverse proxy requests to your localhost (your machine). This would route *.php to other IP (a vagrant machine just for those two PHP that happened later) and all other static request to you local grunt server.
With this setup your development site would ever be in http://localhost/ or http://localhost:xxxx where xxx is some port. But, depending on the target requested (php, jpg, html, etc) the request will be routed to right place.

How to configure the base url of a Catalyst application?

I have developed a Catalyst application, which runs via the catalyst development server at, say, localhost:3000. Next I configured Apache to proxy requests to http://myhost/myapp/ to localhost:3000 using mod_proxy.
I would now like Catalyst to know that it has been publicly relocated to the base url myapp/ and take it into account when forming URLs via uri_for.
How can I do it?
KT, the easiest thing to do is to install Catalyst::TraitFor::Request::ProxyBase, as it is specifically designed to replace request base with the value passed by HTTP proxy.
The module is completely transparent and requires no modification of the existing app.

Web framework with user-friendly desktop deployment?

I'm building a web app with Backbone.js (I'm not tied to Backbone yet though). I need a back-end framework only for persistence to a database via a RESTful API. However, I also need to able to deploy it as a 'desktop' app for off-line use, i.e. running a local server and launching a browser window, but I don't want users to have to start a server from the command line to run the application.
I can use SQLite as a database since it's only a single user application, it's just the framework that I'm stuck on. I have looked at the following:
Rails and Django: Default web servers are too flimsy, requires Ruby/Python and runs from the command line. I'm aware of the Bitnami stacks but at 99mb it's too big of a dependency and not exactly hidden from the user.
Sproutcore: Run from command line, also too bulky.
Pyjamas Desktop - Depends on MSHTML which I suspect limits my ability to use HTML5 features.
I'm leaning towards creating a Java app that starts a Scala/Lift server instance and opens a web browser, then sits in the system tray (kind of like WAMP). Is anyone familiar with a tool or framework built for user-friendly deployment as a standalone desktop app?
I do not know if PHP is an option for you? Then I would recommend phpdock.
web2py has a standalone deploy-to-desktop feature with no dependency on Python: http://web2py.com/books/default/chapter/29/14#How-to-distribute-your-applications-as-binaries
As Eydun said, phpdock is an option but it's commercially licensed .
I settled on using Java/Spring/H2/Hibernate/Jetty. I find that Jetty serves requests VERY quickly so the application looks real-time when launched in a browser. There is a tutorial on embedding the Jetty server here. I imagine it's quite trivial to build a GUI that launches the server and a browser.
Another Java option is to use the Play Framework, which may be more at home to those coming from a Django/Rails background. However, the documentation for "creating a standalone version of your application" for Play 2.0+ indicates that they have ditched using Java EE containers (Tomcat/Jetty) and WAR files in favor of running the JARs with the bundled copy of JBoss Netty, so it may take a bit of work to get it running the way you want it.
I would recommend the Play Framework approach if you're OK with using/learning Scala.

Deploying Sinatra app .rb

I know this is probably a stupid question. But I created a .rb file with Sinatra framework and I am not sure how to "deploy" it. When I posted the file on the server I just get a simple text file that reads back my code. It works great when I run it with Sinatra.
Thanks in advance!
It looks like your web server (Apache?) is just serving you with your Ruby script (that is a text, ASCII file) instead of running it (that is: instead of passing it through the Ruby interpreter).
Hence:
Is the Ruby interpreter installed on your server?
Is your web server configured to run Ruby scripts (files terminating with ".rb")
through the Ruby interpreter?
And, is Sinatra itself installed on your web server?
Anyway, Ruby applications (Rails, Sinatra, Padrino) are usually deployed to a server using GIT. Have a look at Heroku and Engine Yard.
Also, there are tools specifically designed to help the developer in deploying Ruby applications. Have a look at Capistrano or Vlad and/or Google for "how to deploy a sinatra application".
For Sinatra/Rails/etc, most people use a Webserver (ie Apache, Nginx, etc) AND an Application Server (Thin, Passenger, Unicorn). For a simple app these can live on the same host. The webserver manages the incoming traffic and the application server executes the ruby code and passes results to the webserver.
When you execute ruby app.rb to run your Sinatra application, you're actually loading up WEBrick which is an application server. Since your development machine is local, you can access it directly at localhost:3000 or some similar address.
Look into tutorial on setting up your webserver with Thin, it's one of the easier ones to work with. When/if you outgrow it, then look into Unicorn.