Class: Propshaft::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/propshaft/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, assembly) ⇒ Server

Returns a new instance of Server.



5
6
7
8
# File 'lib/propshaft/server.rb', line 5

def initialize(app, assembly)
  @app = app
  @assembly = assembly
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/propshaft/server.rb', line 10

def call(env)
  execute_cache_sweeper_if_updated

  path = env["PATH_INFO"]
  method = env["REQUEST_METHOD"]

  if (method == "GET" || method == "HEAD") && path.start_with?(@assembly.prefix)
    path, digest = extract_path_and_digest(path)

    if (asset = @assembly.load_path.find(path)) && asset.fresh?(digest)
      compiled_content = asset.compiled_content

      [
        200,
        {
          Rack::CONTENT_LENGTH  => compiled_content.length.to_s,
          Rack::CONTENT_TYPE    => asset.content_type.to_s,
          VARY                  => "Accept-Encoding",
          Rack::ETAG            => "\"#{asset.digest}\"",
          Rack::CACHE_CONTROL   => "public, max-age=31536000, immutable"
        },
        method == "HEAD" ? [] : [ compiled_content ]
      ]
    else
      [ 404, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9" }, [ "Not found" ] ]
    end
  else
    @app.call(env)
  end
end

#inspectObject



41
42
43
# File 'lib/propshaft/server.rb', line 41

def inspect
  self.class.inspect
end