Class: Mihari::App

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



21
22
23
24
25
26
27
28
# File 'lib/mihari/web/app.rb', line 21

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

Class Method Details

.instanceObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mihari/web/app.rb', line 31

def instance
  @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::ConnectionAdapter
    use Middleware::ErrorNotificationAdapter

    run App.new
  end.to_app
end

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mihari/web/app.rb', line 47

def run!(port: 9292, host: "localhost", threads: "0:5", verbose: false, worker_timeout: 60, open: true)
  url = "http://#{host}:#{port}"

  # 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 if ENV["PARALLEL_PROCESSOR_COUNT"].nil?
  Rackup::Handler::Puma.run(
    instance,
    Port: port,
    Host: host,
    Threads: threads,
    Verbose: verbose,
    worker_timeout: worker_timeout
  ) do |_|
    Launchy.open(url) if ENV["RACK_ENV"] != "development" && open
  rescue Launchy::CommandNotFoundError
    # ref. https://github.com/ninoseki/mihari/issues/477
    # do nothing
  end
end

Instance Method Details

#call(env) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mihari/web/app.rb', line 71

def call(env)
  # api
  api_response = API.call(env)

  # Check if the App wants us to pass the response along to others
  if api_response[1]["X-Cascade"] == "pass"
    # static files
    request_path = env["PATH_INFO"]
    @filenames.each do |path|
      response = @rack_static.call(env.merge("PATH_INFO" => request_path + path))
      return response if response[0] != 404
    end
  end

  api_response
end