Skip to content

Commit

Permalink
Merge pull request #62 from alpaca-tc/puma
Browse files Browse the repository at this point in the history
User can use servers other than webrick
  • Loading branch information
alpaca-tc committed Jun 19, 2024
2 parents 3c42109 + 3397d9d commit f49135b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require 'diver_down'
require 'diver_down/web'
require 'rack/reloader'
require 'rack/contrib'
require 'puma'

definition_dir = ENV.fetch('DIVER_DOWN_DIR')
metadata = DiverDown::Web::Metadata.new(ENV.fetch('DIVER_DOWN_METADATA'))
Expand Down
5 changes: 2 additions & 3 deletions exe/diver_down_web
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

require 'bundler/setup'
require 'rack/contrib'
require 'webrick'
require 'diver_down'
require 'diver_down-web'
require 'optparse'
Expand Down Expand Up @@ -47,9 +46,9 @@ begin
# Rack 2.0
require 'rack'
require 'rack/server'
Rack::Server.new(app:, server: :webrick).start
Rack::Server.new(app:).start
rescue LoadError
# Rack 3.0
require 'rackup'
Rackup::Server.new(app:, server: :webrick).start
Rackup::Server.new(app:).start
end
14 changes: 9 additions & 5 deletions lib/diver_down/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ class Web
# For development
autoload :DevServerMiddleware, 'diver_down/web/dev_server_middleware'

# @return [DiverDown::Web::DefinitionStore]
def self.store
@store ||= DiverDown::Web::DefinitionStore.new
end

# @param definition_dir [String]
# @param metadata [DiverDown::Web::Metadata]
# @param store [DiverDown::Web::DefinitionStore]
def initialize(definition_dir:, metadata:, store: DiverDown::Web::DefinitionStore.new)
@store = store
def initialize(definition_dir:, metadata:)
@metadata = metadata
@files_server = Rack::Files.new(File.join(WEB_DIR))

Expand All @@ -39,7 +43,7 @@ def initialize(definition_dir:, metadata:, store: DiverDown::Web::DefinitionStor
# @return [Array[Integer, Hash, Array]]
def call(env)
request = Rack::Request.new(env)
action = DiverDown::Web::Action.new(store: @store, metadata: @metadata, request:)
action = DiverDown::Web::Action.new(store: self.class.store, metadata: @metadata, request:)

case [request.request_method, request.path]
in ['GET', %r{\A/api/definitions\.json\z}]
Expand Down Expand Up @@ -114,11 +118,11 @@ def load_definition_files_on_thread(definition_files)
definition = definition_loader.load_file(definition_file)

# No needed to synchronize because this is executed on a single thread.
@store.set(definition)
self.class.store.set(definition)
end

# Cache combined_definition
@store.combined_definition
self.class.store.combined_definition
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/diver_down/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
include Rack::Test::Methods

def app
@app ||= described_class.new(definition_dir:, metadata:, store:)
@app ||= described_class.new(definition_dir:, metadata:)
end

let(:definition_dir) do
Expand All @@ -20,6 +20,10 @@ def app
DiverDown::Web::Metadata.new(metadata_path)
end

before do
allow(DiverDown::Web).to receive(:store).and_return(store)
end

describe 'GET /' do
around do |example|
index_path = File.join(DiverDown::Web::WEB_DIR, 'index.html')
Expand Down
8 changes: 8 additions & 0 deletions spec/support/diver_down_web.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

RSpec.configure do |config|
config.after do
# Remove class variable cache
DiverDown::Web.remove_instance_variable(:@store) if DiverDown::Web.instance_variable_defined?(:@store)
end
end

0 comments on commit f49135b

Please sign in to comment.