
Home::Endpoint by default redirects GET / request to /docs. This may be desireable when Travis::Api::App is the only app running, but if it's running in front of other app, you may want to keep root for other things.
68 lines
1.8 KiB
Ruby
68 lines
1.8 KiB
Ruby
# Make sure we set that before everything
|
|
ENV['RACK_ENV'] ||= ENV['RAILS_ENV'] || ENV['ENV']
|
|
ENV['RAILS_ENV'] = ENV['RACK_ENV']
|
|
|
|
require 'travis'
|
|
require 'backports'
|
|
require 'rack'
|
|
require 'rack/protection'
|
|
require 'active_record'
|
|
|
|
# Rack class implementing the HTTP API.
|
|
# Instances respond to #call.
|
|
#
|
|
# run Travis::Api::App.new
|
|
#
|
|
# Requires TLS in production.
|
|
class Travis::Api::App
|
|
autoload :AccessToken, 'travis/api/api/access_token'
|
|
autoload :Responder, 'travis/api/app/responder'
|
|
autoload :Endpoint, 'travis/api/app/endpoint'
|
|
autoload :Extensions, 'travis/api/app/extensions'
|
|
autoload :Helpers, 'travis/api/app/helpers'
|
|
autoload :Middleware, 'travis/api/app/middleware'
|
|
|
|
Rack.autoload :SSL, 'rack/ssl'
|
|
|
|
# Used to track if setup already ran.
|
|
def self.setup?
|
|
@setup ||= false
|
|
end
|
|
|
|
# Loads all endpoints and middleware and hooks them up properly.
|
|
# Calls #setup on any middleware and endpoint.
|
|
#
|
|
# This method is not threadsafe, but called when loading
|
|
# the environment, so no biggy.
|
|
def self.setup(options = {})
|
|
return if setup?
|
|
Travis::Database.connect
|
|
|
|
Responder.set(options) if options
|
|
Backports.require_relative_dir 'app/middleware'
|
|
Backports.require_relative_dir 'app/endpoint'
|
|
Responder.subclasses.each(&:setup)
|
|
|
|
@setup = true
|
|
end
|
|
|
|
attr_accessor :app
|
|
|
|
def initialize(options = {})
|
|
Travis::Api::App.setup
|
|
@app = Rack::Builder.app do
|
|
use Rack::Protection::PathTraversal
|
|
use Rack::SSL if Endpoint.production?
|
|
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) } }
|
|
end
|
|
end
|
|
|
|
# Rack protocol
|
|
def call(env)
|
|
app.call(env)
|
|
end
|
|
end
|