Class: Wrenchmode::Rack
- Inherits:
-
Object
- Object
- Wrenchmode::Rack
- Defined in:
- lib/wrenchmode/rack.rb
Constant Summary collapse
- CLIENT_NAME =
"wrenchmode-rack"
- VERSION =
'0.1.0'
- HEROKU_JWT_VAR =
The ENV var set on Heroku where we can retrieve the JWT
"WRENCHMODE_PROJECT_JWT"
- SWITCH_URL_KEY =
"switch_url"
- TEST_MODE_KEY =
"test_mode"
- IS_SWITCHED_KEY =
"is_switched"
- IP_WHITELIST_KEY =
"ip_whitelist"
- REVERSE_PROXY_KEY =
"reverse_proxy"
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Rack
constructor
A new instance of Rack.
- #update_status(json) ⇒ Object
Constructor Details
#initialize(app, opts = {}) ⇒ Rack
Returns a new instance of Rack.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/wrenchmode/rack.rb', line 20 def initialize(app, opts = {}) @app = app # Symbolize keys opts = symbolize_keys(opts) opts = { force_open: false, ignore_test_mode: true, disable_local_wrench: false, # LocalWrench is our "brand name", want to avoid scaring people will talk of proxies status_protocol: "https", status_host: "wrenchmode.com", status_path: "/api/projects/status", check_delay_secs: 5, logging: false, read_timeout_secs: 3, trust_remote_ip: true }.merge(opts) # The JWT can be set either explicity, or implicitly if Wrenchmode is added as a Heroku add-on # The WRENCHMODE_PROJECT_JWT variable is set as part of the Heroku add-on provisioning process @jwt = opts[:jwt] || ENV[HEROKU_JWT_VAR] @ignore_test_mode = opts[:ignore_test_mode] @disable_reverse_proxy = opts[:disable_local_wrench] @force_open = opts[:force_open] @status_url = "#{opts[:status_protocol]}://#{opts[:status_host]}#{opts[:status_path]}" @check_delay_secs = opts[:check_delay_secs] @logging = opts[:logging] @read_timeout_secs = opts[:read_timeout_secs] @ip_whitelist = [] @logger = nil @trust_remote_ip = opts[:trust_remote_ip] @enable_reverse_proxy = false @made_contact = false # Use a queue with 0 or 1 items to allow the threads to communicate. When a response from the main Wrenchmode server is received, # parse the JSON and put the hash in the queue. Then, the main request thread will update the underlying middleware state # the next time a request is received. @queue = Queue.new end |
Instance Method Details
#call(env) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/wrenchmode/rack.rb', line 63 def call(env) @logger = env['rack.logger'] if @logging && !@logger unless @jwt log("[Wrenchmode] No JWT specified so bypassing Wrenchmode. Please configure Wrenchmode with a JWT.", Logger::ERROR) return @app.call(env) end # On startup, we need to give it a chance to make contact @check_thread ||= start_check_thread() sleep(0.01) while !@made_contact # If we've gotten a new response from the server, use it # to update local status json = begin @queue.pop(true) rescue ThreadError nil end update_status(json) if json should_display_wrenchmode = false if @switched should_display_wrenchmode = !@force_open should_display_wrenchmode &&= !ip_whitelisted?(env) end if should_display_wrenchmode if @enable_reverse_proxy reverse_proxy else redirect end else @app.call(env) end end |
#update_status(json) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/wrenchmode/rack.rb', line 102 def update_status(json) @switch_url = json[SWITCH_URL_KEY] test_mode = json[TEST_MODE_KEY] || false @switched = json[IS_SWITCHED_KEY] && !(@ignore_test_mode && test_mode) @ip_whitelist = json[IP_WHITELIST_KEY] || [] @enable_reverse_proxy = false if json[REVERSE_PROXY_KEY] && !@disable_reverse_proxy @enable_reverse_proxy = json[REVERSE_PROXY_KEY]["enabled"] @reverse_proxy_config = symbolize_keys(json[REVERSE_PROXY_KEY]) end end |