Add ability to bind to a unix socket.

This commit is contained in:
0x1eef 2024-03-22 10:36:23 -03:00
parent ad5ab6c617
commit d71267970f
3 changed files with 17 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -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