Class: Divergence::Application
- Inherits:
-
Rack::Proxy
- Object
- Rack::Proxy
- Divergence::Application
- Defined in:
- lib/divergence/application.rb,
lib/divergence.rb,
lib/divergence/respond.rb,
lib/divergence/webhook.rb
Overview
Responsible for managing the cache folders and swapping the codebases around.
Constant Summary collapse
- @@config =
Configuration.new
- @@log =
Logger.new('./log/app.log')
Class Method Summary collapse
Instance Method Summary collapse
-
#call(env) ⇒ Object
The main entry point for the application.
- #config ⇒ Object
- #handle_webhook ⇒ Object
- #ignore ⇒ Object
-
#initialize ⇒ Application
constructor
A new instance of Application.
-
#link!(path) ⇒ Object
Links the application directory to the given path, which is always a cache directory in our case.
- #ok ⇒ Object
-
#prepare(branch, opts = {}) ⇒ Object
Prepares the filesystem for loading up a branch.
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
34 35 36 37 38 39 40 |
# File 'lib/divergence.rb', line 34 def initialize config.ok? @git = GitManager.new(config.git_path) @cache = CacheManager.new(config.cache_path, config.cache_num) @active_branch = "" end |
Class Method Details
.config ⇒ Object
30 31 32 |
# File 'lib/divergence.rb', line 30 def self.config @@config end |
.configure(&block) ⇒ Object
22 23 24 |
# File 'lib/divergence.rb', line 22 def self.configure(&block) block.call(@@config) end |
.log ⇒ Object
26 27 28 |
# File 'lib/divergence.rb', line 26 def self.log @@log end |
Instance Method Details
#call(env) ⇒ Object
The main entry point for the application. This is called by Rack.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/divergence/respond.rb', line 5 def call(env) @req = RequestParser.new(env) # First, lets find out what subdomain/git branch # we're dealing with (if any). unless @req.has_subdomain? # No subdomain, simply proxy the request. return proxy(env) end # Handle webhooks from Github for updating the current # branch if necessary. if @req.is_webhook? return handle_webhook end # Lets get down to business. begin # Get the proper branch name using a touch of magic branch = @git.discover(@req.subdomain) # Prepare the branch and cache if needed path = prepare(branch) # If we're requesting a different branch than the # one currently loaded, we'll need to link it to # the application directory. link!(path) unless path.nil? @active_branch = branch rescue Exception => e Application.log.error e. return error!(branch) end # We're finished, pass the request through. proxy(env) end |
#config ⇒ Object
42 43 44 |
# File 'lib/divergence.rb', line 42 def config @@config end |
#handle_webhook ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/divergence/webhook.rb', line 3 def handle_webhook hook = JSON.parse(@req['payload']) branch = hook["ref"].split("/").last.strip Application.log.info "Webhook: received for #{branch} branch" # If the webhook is for the currently active branch, # then we perform a pull and a swap. if @cache.is_cached?(branch) Application.log.info "Webhook: updating #{branch}" git_path = @git.switch branch, :force => true config.callback :before_webhook, git_path, :branch => branch @cache.sync branch, git_path config.callback :after_webhook, @cache.path(branch), :branch => branch ok else Application.log.info "Webhook: ignoring #{branch}" ignore end end |
#ignore ⇒ Object
31 32 33 |
# File 'lib/divergence/webhook.rb', line 31 def ignore [200, {"Content-Type" => "text/html"}, ["IGNORE"]] end |
#link!(path) ⇒ Object
Links the application directory to the given path, which is always a cache directory in our case.
18 19 20 21 22 23 24 25 |
# File 'lib/divergence/application.rb', line 18 def link!(path) Application.log.info "Link: #{path} -> #{config.app_path}" config.callback :before_swap, path FileUtils.rm config.app_path if File.exists?(config.app_path) FileUtils.ln_s path, config.app_path, :force => true config.callback :after_swap, config.app_path end |
#ok ⇒ Object
27 28 29 |
# File 'lib/divergence/webhook.rb', line 27 def ok [200, {"Content-Type" => "text/html"}, ["OK"]] end |
#prepare(branch, opts = {}) ⇒ Object
Prepares the filesystem for loading up a branch
6 7 8 9 10 11 12 13 14 |
# File 'lib/divergence/application.rb', line 6 def prepare(branch, opts = {}) return nil if branch == @active_branch unless @cache.is_cached?(branch) @cache.add branch, @git.switch(branch) end @cache.path(branch) end |