Class: MissionControl::Jobs::Theme::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/mission_control/jobs/theme/middleware.rb

Overview

Intercept HTML responses from the Mission Control Jobs engine and inject theme CSS and optional Prism.js syntax highlighting before </head>.

Only rewrites responses that match the engine mount path and have an text/html content type.

The color scheme is resolved per-request: a mc_jobs_color_scheme cookie (set by the client-side color scheme switcher) takes precedence over the configured default. When the effective scheme is :auto, both light and dark stylesheets are injected with prefers-color-scheme media queries. When an explicit scheme is active, only that variant’s CSS is loaded.

Examples:

Manual Rack usage (typically wired by Railtie)

config = MissionControl::Jobs::Theme::Configuration.new
config.color_scheme = :dark
config.syntax_highlighting = false

use MissionControl::Jobs::Theme::Middleware, mount_path: "/admin/jobs", config: config

See Also:

Constant Summary collapse

PRISM_JS =
'<script src="/mission_control/js/prism.min.js" data-manual></script>'
PRISM_INIT =
'<script src="/mission_control/js/prism-init.js"></script>'

Instance Method Summary collapse

Constructor Details

#initialize(app, mount_path: RouteDiscovery::FALLBACK, config: Configuration.new) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • app (#call)

    the next Rack application in the middleware stack

  • mount_path (String) (defaults to: RouteDiscovery::FALLBACK)

    engine mount path to match against requests

  • config (Configuration) (defaults to: Configuration.new)

    theme configuration (defaults to current global configuration)



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mission_control/jobs/theme/middleware.rb', line 37

def initialize(app, mount_path: RouteDiscovery::FALLBACK, config: Configuration.new)
  @app = app
  @mount_path = mount_path
  @mount_path_prefix = "#{mount_path}/"
  @theme = config.theme
  @default_color_scheme = config.color_scheme
  @syntax_highlighting = config.syntax_highlighting
  @color_scheme_switcher = config.color_scheme_switcher
  @injections =
    (Configuration::COLOR_SCHEMES + [:auto]).to_h { |scheme| [scheme, build_injection(scheme)] }
end

Instance Method Details

#call(env) ⇒ Array(Integer, Hash, #each)

Process a Rack request, injecting theme assets into matching HTML responses.

Parameters:

  • env (Hash)

    Rack environment hash

Returns:

  • (Array(Integer, Hash, #each))

    Rack-compatible response triplet



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mission_control/jobs/theme/middleware.rb', line 53

def call(env)
  status, headers, response = @app.call(env)

  if inject_theme?(env, status, headers)
    injection = @injections[resolve_color_scheme(env)]

    body = +""
    response.each { |part| body << part }
    response.close if response.respond_to?(:close)

    body.sub!("</head>", injection)
    headers["content-length"] = body.bytesize.to_s if headers.key?("content-length")

    [status, headers, [body]]
  else
    [status, headers, response]
  end
end