Class: Mihari::Web::App

Inherits:
Object
  • Object
show all
Defined in:
lib/mihari/web/application.rb

Overview

Rack + Grape based web app

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



39
40
41
42
43
44
45
46
# File 'lib/mihari/web/application.rb', line 39

def initialize
  @filenames = ["", ".html", "index.html", "/index.html"]
  @rack_static = Rack::Static.new(
    -> { [404, {}, []] },
    root: File.expand_path("./public", __dir__),
    urls: ["/"]
  )
end

Instance Attribute Details

#filenamesArray<String> (readonly)

Returns:

  • (Array<String>)


34
35
36
# File 'lib/mihari/web/application.rb', line 34

def filenames
  @filenames
end

#rack_staticRack::Static (readonly)

Returns:

  • (Rack::Static)


37
38
39
# File 'lib/mihari/web/application.rb', line 37

def rack_static
  @rack_static
end

Class Method Details

.instanceObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mihari/web/application.rb', line 63

def instance
  Rack::Builder.new do
    use Rack::Cors do
      allow do
        origins "*"
        resource "*", headers: :any, methods: %i[get post put delete options]
      end
    end
    use Middleware::Connection
    use Middleware::CaptureExceptions
    use BetterErrors::Middleware if Mihari.development? && defined?(BetterErrors::Middleware)

    if Mihari.sidekiq?
      use Rack::Session::Cookie, secret: SecureRandom.hex(32), same_site: true, max_age: 86_400

      map "/sidekiq" do
        run Sidekiq::Web
      end
    end

    run App.new
  end.to_app
end

.run!(port: 9292, host: "localhost", threads: "0:5", verbose: false, worker_timeout: 60, open: true) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mihari/web/application.rb', line 87

def run!(port: 9292, host: "localhost", threads: "0:5", verbose: false, worker_timeout: 60, open: true)
  # set maximum number of threads to use as PARALLEL_PROCESSOR_COUNT (if it is not set)
  # ref. https://github.com/grosser/parallel#tips
  # TODO: is this the best way?
  _min_thread, max_thread = threads.split(":")
  ENV["PARALLEL_PROCESSOR_COUNT"] ||= max_thread

  Rackup::Handler::Puma.run(
    instance,
    Port: port,
    Host: host,
    Threads: threads,
    Verbose: verbose,
    worker_timeout:
  ) do |_|
    Launchy.open("http://#{host}:#{port}") if !Mihari.development? && open
  rescue Launchy::CommandNotFoundError
    # ref. https://github.com/ninoseki/mihari/issues/477
    # do nothing
  end
end

Instance Method Details

#call(env) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mihari/web/application.rb', line 48

def call(env)
  status, headers, body = API.call(env)
  return [status, headers, body] unless headers["x-cascade"] == "pass"

  req = Rack::Request.new(env)
  # Check if the App wants us to pass the response along to others
  filenames.each do |path|
    static_status, static_headers, static_body = rack_static.call(env.merge("PATH_INFO" => req.path_info + path))
    return [static_status, static_headers, static_body] if static_status != 404
  end

  [status, headers, body]
end