From 579d1c8ff1372f83e7ce8f6ff4afdcf267633f82 Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Fri, 19 Jun 2015 17:35:02 -0300 Subject: [PATCH] Include simple rack-based HTTP server from Virginia For tests we currently remove the generated Gemfile so that the app boots with the version of Adhearsion under test. The generated Gemfile contains sinatra such that the default rack app will work. In order for the app to boot with the sample Rack config, we must include Sinatra at the project Gemfile level. --- CHANGELOG.md | 1 + Gemfile | 2 + adhearsion.gemspec | 2 + features/cli_create.feature | 6 +++ lib/adhearsion/configuration.rb | 8 ++++ .../generators/app/app_generator.rb | 1 + .../generators/app/templates/Gemfile.erb | 2 + .../generators/app/templates/config.ru | 7 ++++ lib/adhearsion/http_server.rb | 37 +++++++++++++++++++ lib/adhearsion/initializer.rb | 2 + 10 files changed, 68 insertions(+) create mode 100644 lib/adhearsion/generators/app/templates/config.ru create mode 100644 lib/adhearsion/http_server.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fcc5996e..e5a5c66e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * Change: No longer supporting FreeSWITCH via IES (Rayo only) or Asterisk < 11 * Change: Ruby 1.9 is no longer supported. Minimum supported versions are Ruby 2.2.0 and JRuby 9.0.0.0 * Feature: Add i18n support via `CallController#t` + * Feature: Integrate a Rack-based HTTP server from the Virginia plugin * Upgrade to Celluloid 0.16 # [2.6.1](https://github.com/adhearsion/adhearsion/compare/v2.6.0...v2.6.1) - [2015-06-15](https://rubygems.org/gems/adhearsion/versions/2.6.1) diff --git a/Gemfile b/Gemfile index fa75df156..61f377630 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source 'https://rubygems.org' gemspec + +gem 'sinatra', require: nil diff --git a/adhearsion.gemspec b/adhearsion.gemspec index 0ff821bf3..b03e6e49f 100644 --- a/adhearsion.gemspec +++ b/adhearsion.gemspec @@ -37,6 +37,8 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'nokogiri', ["~> 1.5", ">= 1.5.6"] s.add_runtime_dependency 'pry' s.add_runtime_dependency 'rake' + s.add_runtime_dependency 'reel', ["~> 0.5.0"] + s.add_runtime_dependency 'reel-rack', ["~> 0.2.0"] s.add_runtime_dependency 'ruby_ami', ["~> 2.2"] s.add_runtime_dependency 'ruby_jid', ["~> 1.0"] s.add_runtime_dependency 'ruby_speech', ["~> 2.3"] diff --git a/features/cli_create.feature b/features/cli_create.feature index 372c1f394..c62826a55 100644 --- a/features/cli_create.feature +++ b/features/cli_create.feature @@ -28,6 +28,7 @@ Feature: Adhearsion Ahn CLI (Create) | config/events.rb | | config/routes.rb | | config/locales/en.yml | + | config.ru | | Gemfile | | script/ahn | | spec/spec_helper.rb | @@ -50,6 +51,10 @@ Feature: Adhearsion Ahn CLI (Create) """ Adhearsion.router """ + And the file "config.ru" should contain each of these content parts: + """ + run Sinatra::Application + """ And the file "README.md" should contain each of these content parts: """ Start your new app with @@ -86,6 +91,7 @@ Feature: Adhearsion Ahn CLI (Create) | config/environment.rb | | config/events.rb | | config/routes.rb | + | config.ru | | Gemfile | | script/ahn | | spec/spec_helper.rb | diff --git a/lib/adhearsion/configuration.rb b/lib/adhearsion/configuration.rb index 71120d57a..bf44fd060 100644 --- a/lib/adhearsion/configuration.rb +++ b/lib/adhearsion/configuration.rb @@ -107,6 +107,14 @@ def initialize(&block) Whether to include text for translations that provide both text & audio. True or false. __ } + + desc "HTTP server" + http do + enable true, desc: "Enable or disable the HTTP server" + host "0.0.0.0", desc: "IP to bind the HTTP listener to" + port "8080", desc: "Port to bind the HTTP listener to" + rackup 'config.ru', desc: 'Path to Rack configuration file (relative to Adhearsion application root)' + end end Loquacious::Configuration.for :platform, &block if block_given? diff --git a/lib/adhearsion/generators/app/app_generator.rb b/lib/adhearsion/generators/app/app_generator.rb index f25bd7919..e63257bca 100644 --- a/lib/adhearsion/generators/app/app_generator.rb +++ b/lib/adhearsion/generators/app/app_generator.rb @@ -18,6 +18,7 @@ def setup_project template "adhearsion.erb", "config/adhearsion.rb" template "events.erb", "config/events.rb" template "routes.erb", "config/routes.rb" + copy_file "config.ru", "config.ru" copy_file "gitignore", ".gitignore" copy_file "rspec", ".rspec" copy_file "Procfile" diff --git a/lib/adhearsion/generators/app/templates/Gemfile.erb b/lib/adhearsion/generators/app/templates/Gemfile.erb index 1450fa397..0fbef73cd 100644 --- a/lib/adhearsion/generators/app/templates/Gemfile.erb +++ b/lib/adhearsion/generators/app/templates/Gemfile.erb @@ -7,6 +7,8 @@ gem 'adhearsion', '~> <%= Adhearsion::VERSION.split('.')[0,2].join('.') %>' # To use them, simply add them here and run `bundle install`. # +gem 'sinatra' + group :development, :test do gem 'rspec' end diff --git a/lib/adhearsion/generators/app/templates/config.ru b/lib/adhearsion/generators/app/templates/config.ru new file mode 100644 index 000000000..bc92b6cb0 --- /dev/null +++ b/lib/adhearsion/generators/app/templates/config.ru @@ -0,0 +1,7 @@ +require 'sinatra' + +get '/' do + 'Hello world!' +end + +run Sinatra::Application diff --git a/lib/adhearsion/http_server.rb b/lib/adhearsion/http_server.rb new file mode 100644 index 000000000..66bbad0f7 --- /dev/null +++ b/lib/adhearsion/http_server.rb @@ -0,0 +1,37 @@ +# encoding: utf-8 + +require 'reel' +require 'reel/rack' + +module Adhearsion + # @private + class HTTPServer + def self.start + config = Adhearsion.config.platform.http + + return unless config.enable + + rackup = File.join(Adhearsion.root, config.rackup) + unless File.exists?(rackup) + logger.error "Cannot start HTTP server because the Rack configuration does not exist at #{rackup}" + return + end + + app, options = ::Rack::Builder.parse_file rackup + options = { + Host: config.host, + Port: config.port, + }.merge(options) + + app = Rack::CommonLogger.new(app, logger) + + logger.info "Starting HTTP server listening on #{config.host}:#{config.port}" + + supervisor = ::Reel::Rack::Server.supervise_as(:ahn_http_server, app, options) + + Adhearsion::Events.register_callback :shutdown do + supervisor.terminate + end + end + end +end diff --git a/lib/adhearsion/initializer.rb b/lib/adhearsion/initializer.rb index a063dbd1e..3bd03129b 100644 --- a/lib/adhearsion/initializer.rb +++ b/lib/adhearsion/initializer.rb @@ -2,6 +2,7 @@ require 'adhearsion/linux_proc_name' require 'adhearsion/rayo/initializer' +require 'adhearsion/http_server' require 'rbconfig' module Adhearsion @@ -40,6 +41,7 @@ def start initialize_exception_logger setup_i18n_load_path Rayo::Initializer.init + HTTPServer.start init_plugins Rayo::Initializer.run