Class: Radical::App

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

Direct Known Subclasses

App

Class Method Summary collapse

Class Method Details

.appObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/radical/app.rb', line 80

def app
  router = @routes.router
  env = self.env
  assets = @assets
  serve_assets = @serve_assets
  security_headers = @security_headers || {}
  session = @session || self.session

  @app ||= Rack::Builder.app do
    use Rack::CommonLogger
    use Rack::ShowExceptions if env.development?
    use Rack::Runtime
    use Rack::MethodOverride
    use Rack::ContentLength
    use Rack::ETag
    use Rack::Deflater
    use Rack::Head
    use Rack::ConditionalGet
    use Rack::ContentType
    use Rack::Session::Cookie, session
    use Rack::Csrf, raise: env.development?, skip: router.routes.values.flatten.select { |a| a.is_a?(Class) }.uniq.map(&:skip_csrf_actions).flatten(1)
    use Flash
    use SecurityHeaders, security_headers

    if serve_assets || env.development?
      use Rack::Static, urls: ['/assets', '/public'],
                        header_rules: [
                          [/\.(?:css\.gz)$/, { 'Content-Type' => 'text/css', 'Content-Encoding' => 'gzip' }],
                          [/\.(?:js\.gz)$/, { 'Content-Type' => 'application/javascript', 'Content-Encoding' => 'gzip' }],
                          [/\.(?:css\.br)$/, { 'Content-Type' => 'text/css', 'Content-Encoding' => 'br' }],
                          [/\.(?:js\.br)$/, { 'Content-Type' => 'application/javascript', 'Content-Encoding' => 'br' }]
                        ]
    end

    run lambda { |rack_env|
      begin
        router.route(Rack::Request.new(rack_env), options: { assets: assets }).finish
      rescue ModelNotFound
        raise unless env.production?

        Rack::Response.new('404 Not Found', 404).finish
      end
    }
  end
end

.assets(&block) ⇒ Object



45
46
47
48
49
# File 'lib/radical/app.rb', line 45

def assets(&block)
  @assets = Assets.new

  block.call(@assets)
end

.call(env) ⇒ Object



126
127
128
# File 'lib/radical/app.rb', line 126

def call(env)
  app.call(env)
end

.compile_assetsObject



51
52
53
# File 'lib/radical/app.rb', line 51

def compile_assets
  @assets.compile
end

.envObject



76
77
78
# File 'lib/radical/app.rb', line 76

def env
  Env
end

.routes(route_class) ⇒ Object



41
42
43
# File 'lib/radical/app.rb', line 41

def routes(route_class)
  @routes = route_class
end

.security_headers(headers = {}) ⇒ Object



59
60
61
# File 'lib/radical/app.rb', line 59

def security_headers(headers = {})
  @security_headers = headers
end

.serve_assetsObject



55
56
57
# File 'lib/radical/app.rb', line 55

def serve_assets
  @serve_assets = true
end

.session(options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/radical/app.rb', line 63

def session(options = {})
  defaults = {
    path: '/',
    secret: session_secret,
    http_only: true,
    same_site: :lax,
    secure: env.production?,
    expire_after: 2_592_000 # 30 days
  }

  @session = defaults.merge(options)
end