diff --git a/Gemfile.lock b/Gemfile.lock index 3a971014..43304774 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,7 +6,7 @@ GIT GIT remote: git://github.com/rkh/gh.git - revision: 907f35326579495a54c7ee2b030b6292d2e5d9ef + revision: 5aa120dd493f1430fc1af9d97363daea5c4c3415 specs: gh (0.8.0) addressable @@ -32,7 +32,7 @@ GIT GIT remote: git://github.com/travis-ci/travis-core.git - revision: c3459dc0610cf91632c9aa4e873ed45f4fe1b713 + revision: b71c3be388451581f2ca60e6fd862c2bfc56bfb6 specs: travis-core (0.0.1) actionmailer (~> 3.2.3) diff --git a/lib/travis/api/app.rb b/lib/travis/api/app.rb index e3ebf422..36d1f1d8 100644 --- a/lib/travis/api/app.rb +++ b/lib/travis/api/app.rb @@ -36,25 +36,26 @@ class Travis::Api::App # # This method is not threadsafe, but called when loading # the environment, so no biggy. - def self.setup - return if setup? - setup_travis - load_endpoints - setup_endpoints - @setup = true + def self.setup(options = {}) + setup! unless setup? + Endpoint.set(options) + end + + def self.new(options = {}) + setup(options) if options + super() end attr_accessor :app - def initialize(options = {}) + def initialize @app = Rack::Builder.app do use Rack::Protection::PathTraversal use Rack::SSL if Endpoint.production? use ActiveRecord::ConnectionAdapters::ConnectionManagement + Middleware.subclasses.each { |m| use(m) } - endpoints = Endpoint.subclasses - endpoints -= [Endpoint::Home] if options[:disable_root_endpoint] - endpoints.each { |e| map(e.prefix) { run(e.new) } } + Endpoint.subclasses.each { |e| map(e.prefix) { run(e.new) } } end end @@ -65,6 +66,13 @@ class Travis::Api::App private + def self.setup! + setup_travis + load_endpoints + setup_endpoints + @setup = true + end + def self.setup_travis Travis::Database.connect end diff --git a/lib/travis/api/app/endpoint.rb b/lib/travis/api/app/endpoint.rb index 44011042..9d96435f 100644 --- a/lib/travis/api/app/endpoint.rb +++ b/lib/travis/api/app/endpoint.rb @@ -4,6 +4,7 @@ class Travis::Api::App # Superclass for HTTP endpoints. Takes care of prefixing. class Endpoint < Responder set(:prefix) { "/" << name[/[^:]+$/].underscore } + set disable_root_endpoint: false register :scoping before { content_type :json } diff --git a/lib/travis/api/app/endpoint/home.rb b/lib/travis/api/app/endpoint/home.rb index fab79ec6..96bbcb7d 100644 --- a/lib/travis/api/app/endpoint/home.rb +++ b/lib/travis/api/app/endpoint/home.rb @@ -7,6 +7,7 @@ class Travis::Api::App # Landing point. Redirects web browsers to [API documentation](#/docs/). get '/' do + pass if settings.disable_root_endpoint? redirect to('/docs/') if request.preferred_type('application/json', 'text/html') == 'text/html' { 'hello' => 'world' } end