Class: Flipper::Cloud::Middleware
- Inherits:
-
Object
- Object
- Flipper::Cloud::Middleware
- Defined in:
- lib/flipper/cloud/middleware.rb
Constant Summary collapse
- WEBHOOK_PATH =
Internal: The path to match for webhook requests.
%r{\A/webhooks\/?\Z}
- ROOT_PATH =
Internal: The root path to match for requests.
%r{\A/\Z}
Instance Method Summary collapse
- #call(env) ⇒ Object
- #call!(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app, options = {}) ⇒ Middleware
Returns a new instance of Middleware.
13 14 15 16 |
# File 'lib/flipper/cloud/middleware.rb', line 13 def initialize(app, = {}) @app = app @env_key = .fetch(:env_key, 'flipper') end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 |
# File 'lib/flipper/cloud/middleware.rb', line 18 def call(env) dup.call!(env) end |
#call!(env) ⇒ Object
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 |
# File 'lib/flipper/cloud/middleware.rb', line 22 def call!(env) request = Rack::Request.new(env) if request.post? && (request.path_info.match(ROOT_PATH) || request.path_info.match(WEBHOOK_PATH)) status = 200 headers = { Rack::CONTENT_TYPE => "application/json", } body = "{}" payload = request.body.read signature = request.env["HTTP_FLIPPER_CLOUD_SIGNATURE"] flipper = env.fetch(@env_key) begin = MessageVerifier.new(secret: flipper.sync_secret) if .verify(payload, signature) begin flipper.sync body = JSON.generate({ groups: Flipper.group_names.map { |name| {name: name}} }) rescue Flipper::Adapters::Http::Error => error status = error.response.code.to_i == 402 ? 402 : 500 headers["flipper-cloud-response-error-class"] = error.class.name headers["flipper-cloud-response-error-message"] = error. rescue => error status = 500 headers["flipper-cloud-response-error-class"] = error.class.name headers["flipper-cloud-response-error-message"] = error. end end rescue MessageVerifier::InvalidSignature status = 400 end [status, headers, [body]] else @app.call(env) end end |