Class: Simple::Httpd::Rack::DynamicMount

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Simple::Httpd::RouteDescriptions
Defined in:
lib/simple/httpd/rack/dynamic_mount.rb

Overview

The Simple::Httpd::Mountpoint.build returns a Rack compatible app, which serves HTTP requests according to a set of dynamic ruby scripts and some existing static files.

Constant Summary collapse

H =
::Simple::Httpd::Helpers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Simple::Httpd::RouteDescriptions

#describe_route!, #route_descriptions

Constructor Details

#initialize(mount_point, path, routes) ⇒ DynamicMount

Returns a new instance of DynamicMount.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 31

def initialize(mount_point, path, routes)
  @mount_point = mount_point
  @path = path.gsub(/\/\z/, "") # remove trailing "/"

  ::Simple::Httpd::Reloader.attach self, paths: service_files, reloading_instance: nil

  @rack_app = H.subclass ::Simple::Httpd::BaseController,
                         paths: helper_files + [routes],
                         description: "<controller:#{H.shorten_path(path)}>"

  @rack_app.route_descriptions.each do |route|
    describe_route! route.prefix(@mount_point)
  end
end

Instance Attribute Details

#mount_pointObject (readonly)

Returns the value of attribute mount_point.



29
30
31
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 29

def mount_point
  @mount_point
end

#pathObject (readonly)

Returns the value of attribute path.



28
29
30
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 28

def path
  @path
end

Class Method Details

.build(mount_point, path) ⇒ Object

Tries to build a DynamicMount mount point at a gievn path. This is only successful if the path location has a “routes.rb” file.



13
14
15
16
17
18
19
20
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 13

def self.build(mount_point, path)
  expect! path => String

  routes = File.join(path, "routes.rb")
  return unless File.exist?(routes)

  new(mount_point, path, routes)
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 22

def call(env)
  reload! if ::Simple::Httpd.env == "development"

  @rack_app.call(env)
end