Class: Logster::Middleware::Viewer

Inherits:
Object
  • Object
show all
Defined in:
lib/logster/middleware/viewer.rb

Constant Summary collapse

PATH_INFO =
"PATH_INFO".freeze
SCRIPT_NAME =
"SCRIPT_NAME".freeze
REQUEST_METHOD =
"REQUEST_METHOD".freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Viewer

Returns a new instance of Viewer.



11
12
13
14
15
16
17
18
19
20
# File 'lib/logster/middleware/viewer.rb', line 11

def initialize(app)
  @app = app

  @logs_path = Logster.config.subdirectory
  @path_regex = Regexp.new("^(#{@logs_path}$)|^(#{@logs_path}(/.*))$")
  (@store = Logster.store) || raise(ArgumentError.new("store"))

  @assets_path = File.expand_path("../../../../assets", __FILE__)
  @fileserver = Rack::File.new(@assets_path)
end

Instance Method Details

#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
61
62
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/logster/middleware/viewer.rb', line 22

def call(env)
  path = env[PATH_INFO]
  script_name = env[SCRIPT_NAME]

  if script_name && script_name.length > 0
    path = script_name + path
  end

  if resource = resolve_path(path)
    if resource =~ /\.ico$|\.js$|\.png|\.handlebars$|\.css$|\.woff$|\.ttf$|\.woff2$|\.svg$|\.otf$|\.eot$/
      serve_file(env, resource)

    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")
      end

      key = $1
      message = Logster.store.get(key)
      unless message
        return [404, {}, ["Message not found"]]
      end

      Logster.store.delete(message)
      return [301, { "Location" => "#{@logs_path}/" }, []]

    elsif resource =~ /\/(un)?protect\/([0-9a-f]+)$/
      off = $1 == "un"
      key = $2

      message = Logster.store.get(key)
      unless message
        return [404, {}, ["Message not found"]]
      end

      if off
        if Logster.store.unprotect(key)
          return [301, { "Location" => "#{@logs_path}/show/#{key}?protected=false" }, []]
        else
          return [500, {}, ["Failed"]]
        end
      else
        if Logster.store.protect(key)
          return [301, { "Location" => "#{@logs_path}/show/#{key}?protected=true" }, []]
        else
          return [500, {}, ["Failed"]]
        end
      end

    elsif resource =~ /\/solve\/([0-9a-f]+)$/
      key = $1

      message = Logster.store.get(key)
      unless message
        return [404, {}, ["Message not found"]]
      end

      Logster.store.solve(key)

      return [301, { "Location" => "#{@logs_path}" }, []]

    elsif resource =~ /\/clear$/
      if env[REQUEST_METHOD] != "POST"
        return method_not_allowed("POST is needed for /clear")
      end
      Logster.store.clear
      return [200, {}, ["Messages cleared"]]

    elsif resource =~ /\/show\/([0-9a-f]+)(\.json)?$/
      key = $1
      json = $2 == ".json"

      message = Logster.store.get(key)
      unless message
        return [404, {}, ["Message not found"]]
      end

      if json
        [200, { "Content-Type" => "application/json; charset=utf-8" }, [message.to_json]]
      else
        preload = preload_json("/show/#{key}" => message)
        [200, { "Content-Type" => "text/html; charset=utf-8" }, [body(preload)]]
      end

    elsif resource =~ /\/settings(\.json)?$/
      json = $1 == ".json"
      if json
        coded_patterns = Logster.store.ignore&.map(&:inspect) || []
        custom_patterns = Logster::SuppressionPattern.find_all(raw: true)
        [200, { "Content-Type" => "application/json; charset=utf-8" }, [JSON.generate(coded_patterns: coded_patterns, custom_patterns: custom_patterns)]]
      else
        [200, { "Content-Type" => "text/html; charset=utf-8" }, [body(preload_json)]]
      end
    elsif resource =~ /\/patterns\/([a-zA-Z0-9_]+)\.json$/
      unless Logster.config.enable_custom_patterns_via_ui
        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"

      update_patterns(set_name, req)
    elsif resource == "/"
      [200, { "Content-Type" => "text/html; charset=utf-8" }, [body(preload_json)]]
    else
      not_found
    end
  else
    @app.call(env)
  end
end