Sinatra: Request: Support unix sockets

Created on 23 Nov 2015  路  5Comments  路  Source: sinatra/sinatra

It would be nice if you could use a unix socket file rather than listening on a port. Right now to do this I have to put sinatra behind something else.

Most helpful comment

require 'sinatra/base'

SOCKET_PATH = File.expand_path('sinatra.sock')

class App < Sinatra::Base
  set :server, "thin"
  set :bind, SOCKET_PATH

  get '/' do
    "Hello, World"
  end
end

App.run!

This way, choose thin as a backend and set bind server to socket path.

All 5 comments

Sinatra sits on top of Rack, which allows web applications to talk to an underlying http server and vice versa). It's down to the underlying server to support listening on http, or unix sockets, etc.

By default, sinatra will start webbrick, the built-in ruby web server (which is slow). You can very easily tell it to use another server, such as Thin or Puma or Unicorn or Phusion or any one of a host of other servers (and indeed this is recommended for production). Thin for example supports listening on unix sockets just fine.

There is actually a pretty good recipe written already on how to configure Sinatra using nginx and unicorn
=> recipes.sinatrarb.com/p/deployment/nginx_proxied_to_unicorn

Yes, seeing a nginx-unicorn-sinatra setup was what made me want to switch to unix sockets, I'm aware it's an option.

I'll look into webbrick's options, I hadn't realized that was built into ruby rather than sinatra.

@za3k: Maybe this can shed a light on what you're looking for => http://docs.ruby-lang.org/en/2.1.0/WEBrick.html#module-WEBrick-label-Dropping+Permissions

require 'sinatra/base'

SOCKET_PATH = File.expand_path('sinatra.sock')

class App < Sinatra::Base
  set :server, "thin"
  set :bind, SOCKET_PATH

  get '/' do
    "Hello, World"
  end
end

App.run!

This way, choose thin as a backend and set bind server to socket path.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

namusyaka picture namusyaka  路  3Comments

singram picture singram  路  4Comments

daveroberts picture daveroberts  路  5Comments

Harishkris picture Harishkris  路  3Comments

matsbror picture matsbror  路  4Comments