al-quran.reflectslight.io/packages/ruby/server/lib/server.rb

55 lines
1.1 KiB
Ruby
Raw Normal View History

# 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"
require_relative "server/dir"
def self.app(path)
Rack::Builder.app do
2023-07-05 18:58:31 +02:00
use Server::ETag
run Server::Dir.new(path)
end
end
def self.for_dir(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 = {})
@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)
@server.binder.parse(@options[:binds])
2023-06-30 21:14:21 +02:00
thr = @server.run
block ? thr.join : thr
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