From b8642cb3f01a696b9e3ddc83e2b088f8813e6ef6 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Sun, 10 Mar 2024 08:53:33 -0300 Subject: [PATCH] Add ability to bind to any host, port For example: rake server[0.0.0.0,8888] --- Rakefile.rb | 5 +++-- packages/ruby/server/lib/server.rb | 11 +++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Rakefile.rb b/Rakefile.rb index 66b369c..eed1e07 100644 --- a/Rakefile.rb +++ b/Rakefile.rb @@ -11,10 +11,11 @@ load "tasks/linter.rake" load "tasks/nanoc.rake" desc "Serve the website on localhost" -task :server do +task :server, [:host, :port] do |_t, args| require "server" build_dir = Ryo.from(YAML.load_file("./nanoc.yaml")).output_dir - s = Server.for_dir(build_dir) + h = args.to_h + s = Server.for_dir(build_dir, h.slice(:host,:port)) s.start(block: true) rescue Interrupt s.stop diff --git a/packages/ruby/server/lib/server.rb b/packages/ruby/server/lib/server.rb index 7fc34a7..f13581f 100644 --- a/packages/ruby/server/lib/server.rb +++ b/packages/ruby/server/lib/server.rb @@ -15,7 +15,13 @@ class Server end def self.for_dir(path, options = {}) - new(app(path), options) + host = options.delete(:host) || "127.0.0.1" + port = options.delete(:port) || 7777 + new app(path), options.merge!( + binds: ["tcp://#{host}:#{port}"], + tcp_host: host, + tcp_port: port + ) end def initialize(app, options = {}) @@ -39,9 +45,6 @@ class Server def default_options { - tcp_host: "127.0.0.1", - tcp_port: 7777, - binds: ["tcp://127.0.0.1:7777"], supported_http_methods: %w[GET HEAD], min_threads: 1, max_threads: 5,