Class: Slimer::WebApplication

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

Overview

Defines routes and responses for the Slimer web app.

Constant Summary collapse

CONTENT_LENGTH =
"Content-Length"
REDIS_KEYS =
%w[redis_version uptime_in_days connected_clients used_memory_human used_memory_peak_human].freeze
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: 'unsafe-inline'",
  "style-src 'self' https: http: 'unsafe-inline'",
  "worker-src 'self'",
  "base-uri 'self'"
].join("; ").freeze

Constants included from WebRouter

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WebRouter

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

Constructor Details

#initialize(klass) ⇒ WebApplication



26
27
28
# File 'lib/slimer/web/application.rb', line 26

def initialize(klass)
  @klass = klass
end

Class Method Details

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



111
112
113
114
115
116
117
# File 'lib/slimer/web/application.rb', line 111

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

.settingsObject



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

def self.settings
  Slimer::Web.settings
end

Instance Method Details

#call(env) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/slimer/web/application.rb', line 86

def call(env)
  action = self.class.match(env)
  return [404, { "Content-Type" => "text/plain", "X-Cascade" => "pass" }, ["Not Found"]] unless action

  resp = catch(:halt) do
    action.instance_exec env, &action.block
  end

  resolve_response(resp)
end

#resolve_response(resp) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/slimer/web/application.rb', line 97

def resolve_response(resp)
  return resp if resp.is_a?(Array)

  # rendered content goes here
  headers = {
    "Content-Type" => "text/html",
    "Cache-Control" => "no-cache",
    "Content-Language" => "en",
    "Content-Security-Policy" => CSP_HEADER
  }
  # we'll let Rack calculate Content-Length for us.
  [200, headers, [resp]]
end

#root_pathObject



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

def root_path
  "#{env["SCRIPT_NAME"]}/"
end

#settingsObject



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

def settings
  @klass.settings
end