diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index 03538804..43e0de1b 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -1,43 +1,44 @@ require 'rack' require 'rack/protection/path_traversal' -module Travis::Web - class App - ASSET_DIRS = %r(/(styles|scripts)/) +class Travis::Web::App + ASSET_DIRS = %r(/(styles|scripts)/) - autoload :Api, 'travis/web/app/api' - autoload :Assets, 'travis/web/app/assets' - autoload :Config, 'travis/web/app/config' - autoload :Files, 'travis/web/app/files' - autoload :Helpers, 'travis/web/app/helpers' - autoload :Filter, 'travis/web/app/filter' - autoload :Terminal, 'travis/web/app/terminal' + DEFAULT_ENDPOINT = 'https://api.travis-ci.org' + DEFAULT_PUSHER_KEY = '23ed642e81512118260e' - Rack.autoload :SSL, 'rack/ssl' - Rack.autoload :Deflater, 'rack/deflater' + autoload :Api, 'travis/web/app/api' + autoload :Assets, 'travis/web/app/assets' + autoload :Config, 'travis/web/app/config' + autoload :Files, 'travis/web/app/files' + autoload :Helpers, 'travis/web/app/helpers' + autoload :Filter, 'travis/web/app/filter' + autoload :Terminal, 'travis/web/app/terminal' - include Terminal + Rack.autoload :SSL, 'rack/ssl' + Rack.autoload :Deflater, 'rack/deflater' - attr_accessor :app + include Terminal - def initialize - config = Config.new - announce(config) + attr_accessor :app - @app = Rack::Builder.app do - use Rack::SSL if config.production? - use Rack::Protection::PathTraversal + def initialize + config = Config.new + announce(config) - use Travis::Web::App::Api, config if config.run_api? - use Rack::Deflater if config.deflate? - use Travis::Web::App::Assets, config - use Travis::Web::App::Filter, config - run Travis::Web::App::Files.new - end - end + @app = Rack::Builder.app do + use Rack::SSL if config.production? + use Rack::Protection::PathTraversal - def call(env) - app.call(env) + use Travis::Web::App::Api, config if config.run_api? + use Rack::Deflater if config.deflate? + use Travis::Web::App::Assets, config + use Travis::Web::App::Filter, config + run Travis::Web::App::Files.new end end + + def call(env) + app.call(env) + end end diff --git a/lib/travis/web/app/config.rb b/lib/travis/web/app/config.rb index 25b3b903..ea3eff19 100644 --- a/lib/travis/web/app/config.rb +++ b/lib/travis/web/app/config.rb @@ -1,6 +1,10 @@ class Travis::Web::App class Config - OPTIONS = %w(ENV API_ENDPOINT CLIENT_ENDPOINT RUN_API WATCH DEFLATE) + OPTIONS = %w(ENV API_ENDPOINT CLIENT_ENDPOINT PUSHER_KEY RUN_API WATCH DEFLATE) + + def [](key) + send(key) + end def keys @keys ||= OPTIONS.map(&:downcase) @@ -25,13 +29,17 @@ class Travis::Web::App end def api_endpoint - config.fetch(:api_endpoint, run_api? ? '/api' : "https://api.travis-ci.org").gsub(/:\d+/, '') + config.fetch(:api_endpoint, run_api? ? '/api' : DEFAULT_API_ENDPOINT).gsub(/:\d+/, '') end def client_endpoint config.fetch(:client_endpoint, '/') end + def pusher_key + config.fetch(:pusher_key, DEFAULT_PUSHER_KEY) + end + def deflate? !!config.fetch(:deflate, production?) end diff --git a/lib/travis/web/app/filter.rb b/lib/travis/web/app/filter.rb index 18eb2751..9df6f000 100644 --- a/lib/travis/web/app/filter.rb +++ b/lib/travis/web/app/filter.rb @@ -1,7 +1,7 @@ class Travis::Web::App class Filter - autoload :Endpoint, 'travis/web/app/filter/endpoint' - autoload :Assets, 'travis/web/app/filter/assets' + autoload :Config, 'travis/web/app/filter/config' + autoload :Assets, 'travis/web/app/filter/assets' attr_reader :app, :config @@ -21,11 +21,17 @@ class Travis::Web::App def filter(headers, body) headers.delete 'Content-Length' # why don't we just set this to the new length? filtered = [] - body.each { |s| filtered << filters.inject(s) { |s, filter| filter.apply(s) } } + body.each { |chunk| filtered << filter_chunk(chunk) } body.close if body.respond_to?(:close) [headers, filtered] end + def filter_chunk(string) + filters.inject(string) do |string, filter| + filter.apply? ? filter.apply(string) : string + end + end + def filters @filters ||= Filter.constants.map { |name| Filter.const_get(name).new(config) } end diff --git a/lib/travis/web/app/filter/assets.rb b/lib/travis/web/app/filter/assets.rb index 06df9539..91063fba 100644 --- a/lib/travis/web/app/filter/assets.rb +++ b/lib/travis/web/app/filter/assets.rb @@ -1,29 +1,35 @@ -class Travis::Web::App::Filter - class Assets - attr_reader :config +class Travis::Web::App + class Filter + class Assets + attr_reader :config - def initialize(config) - @config = config - end - - def apply(string) - string = version(string) - string = minify(string) # if config.production? - string - end - - private - - def version(string) - string.gsub(Travis::Web::App::ASSET_DIRS) do - "/#{config.version}/#{$1}/" - end + def initialize(config) + @config = config end - def minify(string) - string.gsub(%r((/javascripts/.*).js)) do - "#{$1}.min.js/" - end + def apply? + true end + + def apply(string) + string = version(string) + string = minify(string) if config.production? + string + end + + private + + def version(string) + string.gsub(Travis::Web::App::ASSET_DIRS) do + "/#{config.version}/#{$1}/" + end + end + + def minify(string) + string.gsub(%r((/javascripts/.*).js)) do + "#{$1}.min.js/" + end + end + end end end diff --git a/lib/travis/web/app/filter/config.rb b/lib/travis/web/app/filter/config.rb new file mode 100644 index 00000000..47eb5674 --- /dev/null +++ b/lib/travis/web/app/filter/config.rb @@ -0,0 +1,24 @@ +class Travis::Web::App + class Filter + class Config + CONFIG_TAG = %r(]*>) + + attr_reader :config + + def initialize(config) + @config = config + end + + def apply? + config.api_endpoint != DEFAULT_ENDPOINT || + config.pusher_key != DEFAULT_PUSHER_KEY + end + + def apply(string) + string.gsub(CONFIG_TAG) do + %() + end + end + end + end +end diff --git a/lib/travis/web/app/filter/endpoint.rb b/lib/travis/web/app/filter/endpoint.rb deleted file mode 100644 index 72e23bcc..00000000 --- a/lib/travis/web/app/filter/endpoint.rb +++ /dev/null @@ -1,19 +0,0 @@ -class Travis::Web::App::Filter - class Endpoint - DEFAULT_ENDPOINT = 'https://api.travis-ci.org' - - attr_reader :config - - def initialize(config) - @config = config - end - - def apply(string) - apply? ? string.gsub(DEFAULT_ENDPOINT, config.api_endpoint) : string - end - - def apply? - config.api_endpoint != DEFAULT_ENDPOINT - end - end -end diff --git a/public/index.html b/public/index.html index 3fb08040..4a22cdb1 100644 --- a/public/index.html +++ b/public/index.html @@ -3,6 +3,7 @@
+