Add ability to bind to any host, port

For example:
rake server[0.0.0.0,8888]
This commit is contained in:
0x1eef 2024-03-10 08:53:33 -03:00
parent 60379b2de5
commit b8642cb3f0
2 changed files with 10 additions and 6 deletions

View file

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

View file

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