tscheingeld
tscheingeld

Reputation: 887

How can I configure WEBrick to use a Unix socket?

I'm building a small application using WEBrick. It's working nicely. However, it uses up a port. I'd rather implement it as a unix socket, but I have no idea how.

I'm using WEBrick version 1.8.1 and ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux-gnu]

Upvotes: 1

Views: 56

Answers (1)

user513951
user513951

Reputation: 13715

WEBrick does not have any configuration option that allows you to select a non-TCP socket. However, you can trick it by telling it you want no socket at all, and then slipping a Unix socket into its back pocket, so to speak.

Amadan solved this as part of this answer (in response to your question from five years ago!):

require 'webrick'
require 'socket'

begin
  UNIXServer.open('/tmp/socket-simple') do |ssocket|
    server = WEBrick::HTTPServer.new(DoNotListen: true)
    server.listeners << ssocket

    server.mount_proc '/' do |req, res|
      res.set_content_type('text/plain')
      res.body = "Hello, world!"
    end

    server.start
  end
ensure
  File.unlink('/tmp/socket-simple')
end

Upvotes: 1

Related Questions