require 'auth' require 'config' require 'git_fusion' require 'hws_p4_cleanup' require 'hws_helpers' require 'hws_settings' require 'helix_sync' require 'helix_web_services/version' require 'helix_versioning_engine' require 'projects' require 'rack/parser' require 'sinatra/base' require 'sinatra/namespace' require 'P4' module HelixWebServices module ErrorHandling def self.registered(app) app.set :show_exceptions => false app.set :dump_errors => true app.error do |err| err = env['sinatra.error'] if err.is_a?(P4Exception) # Can happen when we're not passing a block to # open_p4. Convert to a P4WebAPI error. This is not ideal # as it always uses the same code, but then we have no idea # what actually happened here. err = P4Error.default_error(err.to_s) # Fall through... end body = nil if err.is_a?(P4Error) if err.message_code == 7480 || err.message_code == 7189 halt 401, { MessageCode: 0, MessageSeverity: 4, MessageText: 'Resource not found' }.to_json else code = 500 code = 400 if err.user_error? halt code, { MessageCode: err.message_code, MessageSeverity: err.message_severity, MessageText: err.message_text }.to_json end end if body.nil? body = { MessageCode: 15_361, MessageSeverity: P4::E_FATAL, MessageText: 'Unknown server issue' }.to_json end halt 500, body end end end # A master Sinatra instance that basically sets up routing to each of the # sub-applications, and connects the authorization mechanism. # # In the future, we may have status and health check functionality available # here. # # We mount each application under a "versioned" subpath. This allows us to # keep an old and new version of a sub-application in the system at the # same time, ideally to allow for migration to breaking changes. class Master < Sinatra::Base register Sinatra::Namespace register HelixWebServices::ErrorHandling # Without this set, the return content type is always text/html before do content_type 'application/json' end # Inject Rack::Parser into the middleware stack so that it # automatically parses json bodies in post requests into the params # array. use Rack::Parser, content_types: { 'application/json' => proc { |body| ::MultiJson.decode body } } use HWSSettings use HWSP4Cleanup use Auth::Middleware, unauthenticated_paths: [ {method: 'POST', path: '/auth/v1/login'} ] not_found do status 404 '' end class << self def register_app(app) use app app.register HelixWebServices::ErrorHandling app.before do content_type 'application/json' headers['X-Helix-Web-Services-Version'] = HelixWebServices::PRODUCT_ID end app.helpers HWSHelpers app.not_found do status 404 '' end end end get '/status' do {status: 'OK'}.to_json end register_app(Auth::App) register_app(Config::App) register_app(HelixVersioningEngine::App) register_app(Projects::App) register_app(GitFusion::App) register_app(HelixSync::App) end end # We offer a special path in our settings where we'll load up any scripts # and require them. scripts_dir = ENV['CUSTOM_SCRIPTS'] || HWSSettings.system.CUSTOM_SCRIPTS if Dir.exist?(scripts_dir) $LOAD_PATH << scripts_dir Dir.glob("#{scripts_dir}/*.rb").each do |s| require s end end