2023-06-26 02:03:07 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Server
|
|
|
|
require "rack"
|
|
|
|
require_relative "server/puma"
|
2023-06-29 17:07:24 +02:00
|
|
|
require_relative "server/gzip"
|
2023-07-05 18:58:31 +02:00
|
|
|
require_relative "server/etag"
|
2023-06-26 02:03:07 +02:00
|
|
|
require_relative "server/dir"
|
|
|
|
|
|
|
|
def self.app(path)
|
|
|
|
Rack::Builder.app do
|
2023-07-05 18:58:31 +02:00
|
|
|
use Server::ETag
|
2023-06-26 02:03:07 +02:00
|
|
|
run Server::Dir.new(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.for_dir(path, options = {})
|
2024-03-10 12:53:33 +01:00
|
|
|
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
|
|
|
|
)
|
2023-06-26 02:03:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(app, options = {})
|
|
|
|
@app = app
|
|
|
|
@options = default_options.merge!(options)
|
|
|
|
@events = Puma::Events.new
|
|
|
|
@server = Puma::Server.new(@app, @events, @options)
|
|
|
|
end
|
|
|
|
|
2023-06-30 21:14:21 +02:00
|
|
|
def start(block: false)
|
2023-06-26 02:03:07 +02:00
|
|
|
@server.binder.parse(@options[:binds])
|
2023-06-30 21:14:21 +02:00
|
|
|
thr = @server.run
|
|
|
|
block ? thr.join : thr
|
2023-06-26 02:03:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
@server.stop
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def default_options
|
|
|
|
{
|
|
|
|
supported_http_methods: %w[GET HEAD],
|
|
|
|
min_threads: 1,
|
|
|
|
max_threads: 5,
|
|
|
|
workers: 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|