Class: Mascot::Server

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

Overview

Mount inside of a config.ru file to run this as a server.

Constant Summary collapse

ROOT_PATH =
Pathname.new("/")

Instance Method Summary collapse

Constructor Details

#initialize(site:, relative_to: "/") ⇒ Server

Returns a new instance of Server.



26
27
28
29
# File 'lib/mascot-server.rb', line 26

def initialize(site: , relative_to: "/")
  @relative_to = Pathname.new(relative_to)
  @site = site
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mascot-server.rb', line 31

def call(env)
  req = Rack::Request.new(env)
  resource = @site.get req.path
  # TODO: Memoize this per request and between requests eventually.
  resources = @site.root

  if resource
    body = if resource.asset.template_extensions.empty?
      # TODO: This is not efficient for huge files. Research how Rack::File
      # serves this up (or just take that, maybe mount it as a cascading middleware.)
      resource.body
    else
      TiltResourceRenderer.new(resource).render(locals: {resources: resources})
    end

    [ 200, {"Content-Type" => resource.mime_type.to_s}, Array(body) ]
  else
    [ 404, {"Content-Type" => "text/plain"}, ["Not Found"]]
  end
end