move config around

This commit is contained in:
Konstantin Haase 2012-09-11 13:50:02 +02:00
parent bff18a278c
commit f05f841a46
4 changed files with 22 additions and 12 deletions

View File

@ -6,7 +6,7 @@ GIT
GIT GIT
remote: git://github.com/rkh/gh.git remote: git://github.com/rkh/gh.git
revision: 907f35326579495a54c7ee2b030b6292d2e5d9ef revision: 5aa120dd493f1430fc1af9d97363daea5c4c3415
specs: specs:
gh (0.8.0) gh (0.8.0)
addressable addressable
@ -32,7 +32,7 @@ GIT
GIT GIT
remote: git://github.com/travis-ci/travis-core.git remote: git://github.com/travis-ci/travis-core.git
revision: c3459dc0610cf91632c9aa4e873ed45f4fe1b713 revision: b71c3be388451581f2ca60e6fd862c2bfc56bfb6
specs: specs:
travis-core (0.0.1) travis-core (0.0.1)
actionmailer (~> 3.2.3) actionmailer (~> 3.2.3)

View File

@ -36,25 +36,26 @@ class Travis::Api::App
# #
# This method is not threadsafe, but called when loading # This method is not threadsafe, but called when loading
# the environment, so no biggy. # the environment, so no biggy.
def self.setup def self.setup(options = {})
return if setup? setup! unless setup?
setup_travis Endpoint.set(options)
load_endpoints end
setup_endpoints
@setup = true def self.new(options = {})
setup(options) if options
super()
end end
attr_accessor :app attr_accessor :app
def initialize(options = {}) def initialize
@app = Rack::Builder.app do @app = Rack::Builder.app do
use Rack::Protection::PathTraversal use Rack::Protection::PathTraversal
use Rack::SSL if Endpoint.production? use Rack::SSL if Endpoint.production?
use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::ConnectionAdapters::ConnectionManagement
Middleware.subclasses.each { |m| use(m) } Middleware.subclasses.each { |m| use(m) }
endpoints = Endpoint.subclasses Endpoint.subclasses.each { |e| map(e.prefix) { run(e.new) } }
endpoints -= [Endpoint::Home] if options[:disable_root_endpoint]
endpoints.each { |e| map(e.prefix) { run(e.new) } }
end end
end end
@ -65,6 +66,13 @@ class Travis::Api::App
private private
def self.setup!
setup_travis
load_endpoints
setup_endpoints
@setup = true
end
def self.setup_travis def self.setup_travis
Travis::Database.connect Travis::Database.connect
end end

View File

@ -4,6 +4,7 @@ class Travis::Api::App
# Superclass for HTTP endpoints. Takes care of prefixing. # Superclass for HTTP endpoints. Takes care of prefixing.
class Endpoint < Responder class Endpoint < Responder
set(:prefix) { "/" << name[/[^:]+$/].underscore } set(:prefix) { "/" << name[/[^:]+$/].underscore }
set disable_root_endpoint: false
register :scoping register :scoping
before { content_type :json } before { content_type :json }

View File

@ -7,6 +7,7 @@ class Travis::Api::App
# Landing point. Redirects web browsers to [API documentation](#/docs/). # Landing point. Redirects web browsers to [API documentation](#/docs/).
get '/' do get '/' do
pass if settings.disable_root_endpoint?
redirect to('/docs/') if request.preferred_type('application/json', 'text/html') == 'text/html' redirect to('/docs/') if request.preferred_type('application/json', 'text/html') == 'text/html'
{ 'hello' => 'world' } { 'hello' => 'world' }
end end