Skip to content

Commit

Permalink
Include simple rack-based HTTP server from Virginia
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benlangfeld committed Jun 20, 2015
1 parent 4b9846a commit 579d1c8
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
@@ -1,3 +1,5 @@
source 'https://rubygems.org'

gemspec

gem 'sinatra', require: nil
2 changes: 2 additions & 0 deletions adhearsion.gemspec
Expand Up @@ -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"]
Expand Down
6 changes: 6 additions & 0 deletions features/cli_create.feature
Expand Up @@ -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 |
Expand All @@ -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
Expand Down Expand Up @@ -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 |
Expand Down
8 changes: 8 additions & 0 deletions lib/adhearsion/configuration.rb
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions lib/adhearsion/generators/app/app_generator.rb
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions lib/adhearsion/generators/app/templates/Gemfile.erb
Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/adhearsion/generators/app/templates/config.ru
@@ -0,0 +1,7 @@
require 'sinatra'

get '/' do
'Hello world!'
end

run Sinatra::Application
37 changes: 37 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions lib/adhearsion/initializer.rb
Expand Up @@ -2,6 +2,7 @@

require 'adhearsion/linux_proc_name'
require 'adhearsion/rayo/initializer'
require 'adhearsion/http_server'
require 'rbconfig'

module Adhearsion
Expand Down Expand Up @@ -40,6 +41,7 @@ def start
initialize_exception_logger
setup_i18n_load_path
Rayo::Initializer.init
HTTPServer.start
init_plugins

Rayo::Initializer.run
Expand Down

0 comments on commit 579d1c8

Please sign in to comment.