From d71267970f4483f1b265c675c92d07c8181ca512 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 22 Mar 2024 10:36:23 -0300 Subject: [PATCH] Add ability to bind to a unix socket. --- Rakefile.rb | 11 ++++++++--- nanoc.yaml | 7 +++++++ packages/ruby/server/lib/server.rb | 6 ++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Rakefile.rb b/Rakefile.rb index d9588df31..35224bbee 100644 --- a/Rakefile.rb +++ b/Rakefile.rb @@ -12,11 +12,16 @@ load "rake/tasks/nanoc.rake" load "rake/tasks/submodules.rake" desc "Serve the website on localhost" -task :server, [:host, :port] do |_t, args| +task :server, [:protocol] do |_t, args| require "server" - build_dir = Ryo.from(YAML.load_file("./nanoc.yaml")).output_dir + nanoc = Ryo.from(YAML.load_file("./nanoc.yaml")) h = args.to_h - s = Server.for_dir(build_dir, h.slice(:host,:port)) + o = if h[:protocol] == 'unix' + {unix: nanoc.server.unix.path} + else + {host: nanoc.server.tcp.host, port: nanoc.server.tcp.port} + end + s = Server.for_dir(nanoc.output_dir, o) s.start(block: true) rescue Interrupt s.stop diff --git a/nanoc.yaml b/nanoc.yaml index db3875988..e0eb9cd1c 100644 --- a/nanoc.yaml +++ b/nanoc.yaml @@ -19,3 +19,10 @@ data_sources: encoding: UTF-8 content_dir: src/ layouts_dir: src/layouts + +server: + unix: + path: /tmp/al-quran.reflectslight.io + tcp: + host: 127.0.0.1 + port: 7777 diff --git a/packages/ruby/server/lib/server.rb b/packages/ruby/server/lib/server.rb index f13581f96..d09035437 100644 --- a/packages/ruby/server/lib/server.rb +++ b/packages/ruby/server/lib/server.rb @@ -17,10 +17,9 @@ class Server def self.for_dir(path, options = {}) host = options.delete(:host) || "127.0.0.1" port = options.delete(:port) || 7777 + unix = options.delete(:unix) || false new app(path), options.merge!( - binds: ["tcp://#{host}:#{port}"], - tcp_host: host, - tcp_port: port + binds: [unix ? "unix://#{unix}" : "tcp://#{host}:#{port}"] ) end @@ -45,7 +44,6 @@ class Server def default_options { - supported_http_methods: %w[GET HEAD], min_threads: 1, max_threads: 5, workers: 1