Module: Capsium::Reactor::Serving

Included in:
Capsium::Reactor
Defined in:
lib/capsium/reactor/serving.rb,
sig/capsium/reactor/serving.rbs

Overview

Serving of resolved routes (static files through the merged view, datasets, handler-route 501s), mixed into Reactor. Includes route inheritance processing per 05x-routing.

Instance Method Summary collapse

Instance Method Details

#content_type_for(route, content_path) ⇒ String

The route's declared MIME type, detected from the resolved file for resources the manifest does not list (dependency content reached through inheritance, layer-only files).

Parameters:

Returns:

  • (String)


53
54
55
56
57
# File 'lib/capsium/reactor/serving.rb', line 53

def content_type_for(route, content_path)
  route.mime(@package.manifest) ||
    Marcel::MimeType.for(Pathname.new(content_path),
                         name: File.basename(content_path))
end

#headers_for(route) ⇒ Hash[String, String]

Parameters:

Returns:

  • (Hash[String, String])


59
60
61
# File 'lib/capsium/reactor/serving.rb', line 59

def headers_for(route)
  route.headers || (@cache_control ? { "Cache-Control" => @cache_control } : {})
end

#inherited_processing(route, body, headers) ⇒ [String, Hash[String, String]]

Route inheritance processing (05x-routing section "Route Inheritance"): responseRewrite replaces the body and/or overrides headers; responseHeaders are merged over the served headers. requestHeaders are parsed and exposed on the route for forwarding reactors; this reactor serves statically without an upstream, so they do not alter its responses.

Parameters:

Returns:

  • ([String, Hash[String, String]])


40
41
42
43
44
45
46
47
48
# File 'lib/capsium/reactor/serving.rb', line 40

def inherited_processing(route, body, headers)
  rewrite = route.response_rewrite
  if rewrite
    body = rewrite.body unless rewrite.body.nil?
    headers = headers.merge(rewrite.headers) if rewrite.headers
  end
  headers = headers.merge(route.response_headers) if route.response_headers
  [body, headers]
end

#serve_dataset(dataset_name, response) ⇒ Object

Parameters:

  • dataset_name (String)
  • response (Object)

Returns:

  • (Object)


63
64
65
66
67
68
69
70
71
72
# File 'lib/capsium/reactor/serving.rb', line 63

def serve_dataset(dataset_name, response)
  dataset = @package.storage.dataset(dataset_name)
  if dataset
    response.status = 200
    response["Content-Type"] = "application/json"
    response.body = JSON.generate(dataset.data)
  else
    respond_not_found(response)
  end
end

#serve_file(route, response) ⇒ Object

Parameters:

Returns:

  • (Object)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/capsium/reactor/serving.rb', line 22

def serve_file(route, response)
  content_path = @merged_view.resolve(route.resource)
  return respond_not_found(response) unless content_path

  body, headers = inherited_processing(route, File.read(content_path),
                                       headers_for(route))
  response.status = 200
  response["Content-Type"] = content_type_for(route, content_path)
  headers.each { |name, value| response[name] = value }
  response.body = body
end

#serve_route(route, response) ⇒ Object

Parameters:

Returns:

  • (Object)


14
15
16
17
18
19
20
# File 'lib/capsium/reactor/serving.rb', line 14

def serve_route(route, response)
  case route.kind
  when :dataset then serve_dataset(route.dataset, response)
  when :resource then serve_file(route, response)
  else respond_not_implemented(response)
  end
end