lib/logster/middleware/viewer.rb in logster-2.5.1 vs lib/logster/middleware/viewer.rb in logster-2.6.0

- old
+ new

@@ -36,11 +36,11 @@ elsif resource.start_with?("/messages.json") serve_messages(Rack::Request.new(env)) elsif resource =~ /\/message\/([0-9a-f]+)$/ if env[REQUEST_METHOD] != "DELETE" - return method_not_allowed("DELETE is needed for /clear") + return method_not_allowed("DELETE") end key = $1 message = Logster.store.get(key) unless message @@ -85,11 +85,11 @@ return [301, { "Location" => "#{@logs_path}" }, []] elsif resource =~ /\/clear$/ if env[REQUEST_METHOD] != "POST" - return method_not_allowed("POST is needed for /clear") + return method_not_allowed("POST") end Logster.store.clear return [200, {}, ["Messages cleared"]] elsif resource =~ /\/show\/([0-9a-f]+)(\.json)?$/ @@ -137,16 +137,16 @@ return not_allowed("Custom patterns via the UI is disabled. You can enable it by committing this line to your app source code:\nLogster.config.enable_custom_patterns_via_ui = true") end set_name = $1 req = Rack::Request.new(env) - return method_not_allowed if req.request_method == "GET" + return method_not_allowed(%w[POST PUT DELETE]) if req.request_method == "GET" update_patterns(set_name, req) elsif resource == "/reset-count.json" req = Rack::Request.new(env) - return method_not_allowed("PUT is needed for this endpoint") if req.request_method != "PUT" + return method_not_allowed("PUT") if req.request_method != "PUT" pattern = nil if [true, "true"].include?(req.params["hard"]) pattern = Logster.store.ignore.find do |patt| str = Regexp === patt ? patt.inspect : patt.to_s str == req.params["pattern"] @@ -168,10 +168,20 @@ if env [200, { "Content-Type" => "application/json; charset=utf-8" }, [JSON.generate(env)]] else not_found end + elsif resource == '/solve-group' + return not_allowed unless Logster.config.enable_custom_patterns_via_ui + req = Rack::Request.new(env) + return method_not_allowed("POST") if req.request_method != "POST" + group = Logster.store.find_pattern_groups do |patt| + patt.inspect == req.params["regex"] + end.first + return not_found("No such pattern group exists") if !group + group.messages_keys.each { |k| Logster.store.solve(k) } + return [200, {}, []] else not_found end else @app.call(env) @@ -241,11 +251,11 @@ when "PUT" record.modify(req.params["new_pattern"]) when "DELETE" record.destroy else - return method_not_allowed("Allowed methods: POST, PUT or DELETE") + return method_not_allowed(%w[POST PUT DELETE]) end [200, { "Content-Type" => "application/json" }, [JSON.generate(pattern: record.to_s)]] rescue => err error_message = err.message @@ -275,12 +285,15 @@ def not_allowed(message = "Not allowed") [403, {}, [message]] end - def method_not_allowed(message = "Method not allowed") - [405, {}, [message]] + def method_not_allowed(allowed_methods) + if Array === allowed_methods + allowed_methods = allowed_methods.join(", ") + end + [405, { "Allow" => allowed_methods }, []] end def parse_regex(string) if string =~ /\/(.+)\/(.*)/ s = $1 @@ -315,16 +328,39 @@ def to_json_and_escape(payload) Rack::Utils.escape_html(JSON.fast_generate(payload)) end + def preload_backtrace_data + gems_data = [] + Gem::Specification.find_all do |gem| + url = gem.metadata["source_code_uri"] || gem.homepage + if url && url.match(/^https?:\/\/github.com\//) + gems_data << { name: gem.name, url: url } + end + end + { + gems_data: gems_data, + directories: Logster.config.project_directories + } + end + def body(preload) root_url = @logs_path root_url += "/" if root_url[-1] != "/" preload.merge!( env_expandable_keys: Logster.config.env_expandable_keys, - patterns_enabled: Logster.config.enable_custom_patterns_via_ui + patterns_enabled: Logster.config.enable_custom_patterns_via_ui, + application_version: Logster.config.application_version ) + backtrace_links_enabled = Logster.config.enable_backtrace_links + gems_dir = Logster.config.gems_dir + gems_dir += "/" if gems_dir[-1] != "/" + preload.merge!(gems_dir: gems_dir, backtrace_links_enabled: backtrace_links_enabled) + + if backtrace_links_enabled + preload.merge!(preload_backtrace_data) + end <<~HTML <!doctype html> <html> <head> <link rel="shortcut icon" href="#{@logs_path}/images/icon_64x64.png">