Skip to content

Commit

Permalink
Split app.rb and server.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
mediafinger committed Apr 7, 2020
1 parent ff0d7b2 commit b985afb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 48 deletions.
2 changes: 1 addition & 1 deletion demo_app_with_hutch/Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: bundle exec puma -t 0:6 -p ${PORT:-4500} -e ${RACK_ENV:-development}
web: bundle exec ruby app.rb -p ${PORT:-4500}
web: bundle exec ruby server.rb -p ${PORT:-4500}
2 changes: 1 addition & 1 deletion demo_app_with_hutch/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ _Alias:_ `rake publisher`

rake server

Starts puma and loads the sinatra app **(a publisher)**. Open http://0.0.0.0:4500/
Starts puma and loads the sinatra server **(a publisher)**. Open http://0.0.0.0:4500/
_Alias:_ `rake sinatra`

### routes
Expand Down
6 changes: 3 additions & 3 deletions demo_app_with_hutch/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ end
desc "Alias for hutch"
task consumer: :hutch

desc "Prints the routes of the app in a human readable form"
desc "Prints the routes of the sinatra server in a human readable form"
task routes: :environment do
actions = %w(GET POST PUT PATCH DELETE)
routes = actions.each_with_object({}) do |action, hash|
hash[action] = App.routes[action]&.each_with_object([]) { |route, arr| arr << route[0].to_s }
hash[action] = Server.routes[action]&.each_with_object([]) { |route, arr| arr << route[0].to_s }
end
ap routes.compact
end
Expand All @@ -64,7 +64,7 @@ task :test do
Rake::Task["rspec"].invoke
end

desc "Run the web app (the publisher), start sinatra server"
desc "Run the server (the publisher), start sinatra server"
task server: :environment do
sh " puma -t 0:8 -p 4500 --tag CONSUMER -C puma.rb"
end
Expand Down
40 changes: 0 additions & 40 deletions demo_app_with_hutch/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

require_relative "settings.rb" unless defined? Settings

require "sinatra"
require "haml"

# Hutch.connect - create queues and start consuming
load "./hutch_publisher_config.rb" unless ENV["RACK_ENV"] == "test"

Expand All @@ -15,40 +12,3 @@ def self.publish(routing_keys:, message:, properties: {}, options: {})
Hutch.publish(routing_keys, message, properties, options)
end
end

class App < Sinatra::Application
configure do
set :root, Settings.root
set :server, :puma
end

get "/" do
haml :index
end

get "/publish" do
haml :publish
end

post "/publish" do
routing_keys = params.fetch("routing_keys", "barcelona.ruby.event")
message = { message: params["message"] }

# Publish message:
result = Client.publish(routing_keys: routing_keys, message: message)

# For demonstration only:
queue_name = "barcelona_ruby_event"
queue = result.channel.queues.find { |name, _q| name == queue_name }&.last
messages_count = queue&.message_count || 0

# TODO: check for success
haml :publish, locals: { success: "Queue #{queue_name} has now #{messages_count} messages." }

# TODO: rescue other possible errors
rescue ArgumentError => e
haml :publish, locals: {
failure: "Error: the following message has not been delivered: '#{params[:message]}' to #{routing_keys}. Error: #{e}",
}
end
end
4 changes: 2 additions & 2 deletions demo_app_with_hutch/config.ru
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "settings.rb" unless defined? Settings
require File.join(Settings.root, "app")
require File.join(Settings.root, "server")

run App
run Server
43 changes: 43 additions & 0 deletions demo_app_with_hutch/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require_relative "app.rb"

require "sinatra"
require "haml"

class Server < Sinatra::Application
configure do
set :root, Settings.root
set :server, :puma
end

get "/" do
haml :index
end

get "/publish" do
haml :publish
end

post "/publish" do
routing_keys = params.fetch("routing_keys", "barcelona.ruby.event")
message = { message: params["message"] }

# Publish message:
result = Client.publish(routing_keys: routing_keys, message: message)

# For demonstration only:
queue_name = "barcelona_ruby_event"
queue = result.channel.queues.find { |name, _q| name == queue_name }&.last
messages_count = queue&.message_count || 0

# TODO: check for success
haml :publish, locals: { success: "Queue #{queue_name} has now #{messages_count} messages." }

# TODO: rescue other possible errors
rescue ArgumentError => e
haml :publish, locals: {
failure: "Error: the following message has not been delivered: '#{params[:message]}' to #{routing_keys}. Error: #{e}",
}
end
end
2 changes: 1 addition & 1 deletion demo_app_with_hutch/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

require_relative "../settings.rb" unless defined? Settings

require File.expand_path("#{Settings.root}/app.rb", __FILE__)
require File.expand_path("#{Settings.root}/app.rb", __FILE__) # load "server.rb" when you need the rack endpoints

0 comments on commit b985afb

Please sign in to comment.