Class: SolidQueueUi::WebApplication

Inherits:
Object
  • Object
show all
Extended by:
WebRouter
Defined in:
lib/solid_queue_ui/web/application.rb

Constant Summary collapse

CSP_HEADER =
[
  "default-src 'self' https: http:",
  "child-src 'self'",
  "connect-src 'self' https: http: wss: ws:",
  "font-src 'self' https: http:",
  "frame-src 'self'",
  "img-src 'self' https: http: data:",
  "manifest-src 'self'",
  "media-src 'self'",
  "object-src 'none'",
  "script-src 'self' https: http:",
  "style-src 'self' https: http: 'unsafe-inline'",
  "worker-src 'self'",
  "base-uri 'self'"
].join("; ").freeze
METRICS_PERIODS =
{
  "1h" => 60,
  "2h" => 120,
  "4h" => 240,
  "8h" => 480
}

Constants included from WebRouter

SolidQueueUi::WebRouter::DELETE, SolidQueueUi::WebRouter::GET, SolidQueueUi::WebRouter::HEAD, SolidQueueUi::WebRouter::PATCH, SolidQueueUi::WebRouter::PATH_INFO, SolidQueueUi::WebRouter::POST, SolidQueueUi::WebRouter::PUT, SolidQueueUi::WebRouter::REQUEST_METHOD, SolidQueueUi::WebRouter::ROUTE_PARAMS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WebRouter

delete, get, head, match, patch, post, put, route

Constructor Details

#initialize(klass) ⇒ WebApplication

Returns a new instance of WebApplication.



30
31
32
# File 'lib/solid_queue_ui/web/application.rb', line 30

def initialize(klass)
  @klass = klass
end

Class Method Details

.after(path = nil, &block) ⇒ Object



146
147
148
# File 'lib/solid_queue_ui/web/application.rb', line 146

def self.after(path = nil, &block)
  afters << [path && Regexp.new("\\A#{path.gsub("*", ".*")}\\z"), block]
end

.aftersObject



167
168
169
# File 'lib/solid_queue_ui/web/application.rb', line 167

def self.afters
  @afters ||= []
end

.before(path = nil, &block) ⇒ Object



142
143
144
# File 'lib/solid_queue_ui/web/application.rb', line 142

def self.before(path = nil, &block)
  befores << [path && Regexp.new("\\A#{path.gsub("*", ".*")}\\z"), block]
end

.beforesObject



163
164
165
# File 'lib/solid_queue_ui/web/application.rb', line 163

def self.befores
  @befores ||= []
end

.helpers(mod = nil, &block) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/solid_queue_ui/web/application.rb', line 134

def self.helpers(mod = nil, &block)
  if block
    WebAction.class_eval(&block)
  else
    WebAction.send(:include, mod)
  end
end

.run_afters(app, action) ⇒ Object



154
155
156
# File 'lib/solid_queue_ui/web/application.rb', line 154

def self.run_afters(app, action)
  run_hooks(afters, app, action)
end

.run_befores(app, action) ⇒ Object



150
151
152
# File 'lib/solid_queue_ui/web/application.rb', line 150

def self.run_befores(app, action)
  run_hooks(befores, app, action)
end

.run_hooks(hooks, app, action) ⇒ Object



158
159
160
161
# File 'lib/solid_queue_ui/web/application.rb', line 158

def self.run_hooks(hooks, app, action)
  hooks.select { |p, _| !p || p =~ action.env[WebRouter::PATH_INFO] }
    .each { |_, b| action.instance_exec(action.env, app, &b) }
end

.set(key, val) ⇒ Object



46
47
48
# File 'lib/solid_queue_ui/web/application.rb', line 46

def self.set(key, val)
  # nothing, backwards compatibility
end

.settingsObject



38
39
40
# File 'lib/solid_queue_ui/web/application.rb', line 38

def self.settings
  SolidQueueUi::Web.settings
end

.tabsObject



42
43
44
# File 'lib/solid_queue_ui/web/application.rb', line 42

def self.tabs
  SolidQueueUi::Web.tabs
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/solid_queue_ui/web/application.rb', line 105

def call(env)
  action = self.class.match(env)
  return [404, {Rack::CONTENT_TYPE => "text/plain", Web::X_CASCADE => "pass"}, ["Not Found"]] unless action

  app = @klass
  resp = catch(:halt) do
    self.class.run_befores(app, action)
    action.instance_exec env, &action.block
  ensure
    self.class.run_afters(app, action)
  end

  case resp
  when Array
    # redirects go here
    resp
  else
    # rendered content goes here
    headers = {
      Rack::CONTENT_TYPE => "text/html",
      Rack::CACHE_CONTROL => "private, no-store",
      Web::CONTENT_LANGUAGE => action.locale,
      Web::CONTENT_SECURITY_POLICY => CSP_HEADER
    }
    # we'll let Rack calculate Content-Length for us.
    [200, headers, [resp]]
  end
end

#settingsObject



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

def settings
  @klass.settings
end