Class: Athar::Middleware::AssetServer

Inherits:
Object
  • Object
show all
Defined in:
lib/athar/middleware/asset_server.rb

Overview

Serves Athar’s dashboard assets (‘dashboard.js`, `dashboard.css`) directly to hosts that don’t have an asset pipeline configured. Rails apps with Sprockets or Propshaft never reach this middleware — the layout helper resolves to a digested asset path served by the asset pipeline.

The URL embeds Athar::VERSION as a cache-busting prefix (‘/athar-assets/<version>/dashboard.js`); the version segment is stripped before resolving to disk so a single canonical file lives in `app/assets/`. New gem version → new URL → fresh cache.

Constant Summary collapse

PREFIX_PATTERN =
%r{\A/athar-assets/[^/]+/(.+)\z}
MIME_TYPES =
{
  ".js" => "application/javascript",
  ".css" => "text/css",
  ".png" => "image/png",
  ".svg" => "image/svg+xml"
}.freeze
EXTENSION_DIRS =

Where each file extension lives, relative to the engine root.

{
  ".js" => "app/assets/javascripts/athar",
  ".css" => "app/assets/stylesheets/athar",
  ".png" => "app/assets/images/athar",
  ".svg" => "app/assets/images/athar"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, engine_root) ⇒ AssetServer

Returns a new instance of AssetServer.



32
33
34
35
# File 'lib/athar/middleware/asset_server.rb', line 32

def initialize(app, engine_root)
  @app  = app
  @root = File.expand_path(engine_root.to_s)
end

Instance Method Details

#call(env) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/athar/middleware/asset_server.rb', line 37

def call(env)
  path = env["PATH_INFO"].to_s
  match = PREFIX_PATTERN.match(path)
  return @app.call(env) unless match

  file = resolve(match[1])
  return not_found unless file

  [200, response_headers(file), [File.binread(file)]]
end