Replace WEBrick with Puma

This commit is contained in:
Alex Kotov 2023-05-03 15:33:50 +04:00
parent 2af3e40510
commit 21fb7b7094
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 38 additions and 31 deletions

View file

@ -41,9 +41,9 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename f }
spec.add_runtime_dependency 'puma', '~> 6.2', '>= 6.2.2'
spec.add_runtime_dependency 'rack', '~> 2.2', '>= 2.2.7'
spec.add_runtime_dependency 'sinatra', '~> 3.0', '>= 3.0.6'
spec.add_runtime_dependency 'webrick', '~> 1.8', '>= 1.8.1'
spec.add_development_dependency 'pry', '~> 0.14'
spec.add_development_dependency 'rake', '~> 13.0'

View file

@ -10,9 +10,10 @@ require 'logger'
# Gems #
########
require 'puma'
require 'rack'
require 'rack/handler/puma'
require 'sinatra/base'
require 'webrick'
################
# Own requires #

View file

@ -25,13 +25,13 @@ module Diversipub
p stdout
p stderr
webrick_thread.join
puma_thread.join
true
end
def finish
puts 'finishing'
webrick.shutdown
puma.stop
nil
end
@ -53,26 +53,44 @@ module Diversipub
end.freeze
end
def webrick_thread
@webrick_thread ||= Thread.start { webrick.start }
def puma_thread
@puma_thread ||= Thread.start { puma.run }
end
def webrick
@webrick ||= WEBrick::HTTPServer.new(webrick_options).tap do |webrick|
webrick.mount '/', Rack::Handler::WEBrick, rack_app
def puma
@puma ||= Puma::Launcher.new(
puma_config,
log_writer: puma_log_writer,
)
end
def puma_config
@puma_config ||= Puma::Configuration.new(
{
app: rack_app,
binds: ['tcp://127.0.0.1:9292'].freeze,
environment: 'production',
log_requests: true,
logger: puma_logger,
rackup: nil,
tag: 'diversipub',
tcp_host: '127.0.0.1',
tcp_port: 9292,
workers: 0,
},
)
end
def puma_log_writer
@puma_log_writer ||= Puma::LogWriter.new stderr, stderr
end
def puma_logger
@puma_logger ||= Logger.new(stderr).tap do |puma_logger|
puma_logger.level = Logger::DEBUG
end
end
def webrick_options
@webrick_options ||= {
AccessLog: webrick_access_log,
BindAddress: 'localhost',
DocumentRoot: nil,
Logger: web_logger,
Port: 8080,
}.freeze
end
def rack_app
@rack_app ||= RackApp.new sinatra_app
end
@ -80,17 +98,5 @@ module Diversipub
def sinatra_app
@sinatra_app ||= SinatraApp.new self
end
def webrick_access_log
@webrick_access_log ||= [
[$stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT],
].freeze
end
def web_logger
@web_logger ||= Logger.new(stderr).tap do |web_logger|
web_logger.level = Logger::INFO
end
end
end
end