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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Viewer

Returns a new instance of Viewer.



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

def initialize(app)
  @app = app

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

  @assets_path = File.expand_path("../../../../assets", __FILE__)
  @fileserver = Rack::File.new(@assets_path)
  @authorize_callback = Logster.config.authorize_callback
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
# 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)

    return @app.call(env) if @authorize_callback && !@authorize_callback.call(env)

    if resource =~ /\.js$|\.handlebars$|\.css$/
      env[PATH_INFO] = resource
      @fileserver.call(env)
    elsif resource.start_with?("/messages.json")
      serve_messages(Rack::Request.new(env))
    elsif resource == "/"
      [200, {"Content-Type" => "text/html; charset=utf-8"}, [body(preload_json)]]
    else
      [404, {}, ["Not found"]]
    end
  else
    @app.call(env)
  end
end