Class: Syntropy::App

Inherits:
Object
  • Object
show all
Defined in:
lib/syntropy/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, src_path, mount_path, opts = {}) ⇒ App

Returns a new instance of App.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/syntropy/app.rb', line 17

def initialize(machine, src_path, mount_path, opts = {})
  @machine = machine
  @src_path = File.expand_path(src_path)
  @mount_path = mount_path
  @route_cache = {}
  @opts = opts

  @relative_path_re = calculate_relative_path_re(mount_path)
  @machine.spin do
    # we do startup stuff asynchronously, in order to first let TP2 do its
    # setup tasks
    @machine.sleep 0.15
    @opts[:logger]&.call("Serving from #{File.expand_path(@src_path)}")
    start_file_watcher if opts[:watch_files]
  end

  @module_loader ||= Syntropy::ModuleLoader.new(@src_path, @opts)
end

Instance Attribute Details

#route_cacheObject (readonly)

Returns the value of attribute route_cache.



15
16
17
# File 'lib/syntropy/app.rb', line 15

def route_cache
  @route_cache
end

Instance Method Details

#call(req) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/syntropy/app.rb', line 45

def call(req)
  entry = find_route(req.path)
  render_entry(req, entry)
rescue Syntropy::Error => e
  msg = e.message
  req.respond(msg.empty? ? nil : msg, ':status' => e.http_status)
rescue StandardError => e
  p e
  p e.backtrace
  req.respond(e.message, ':status' => Qeweney::Status::INTERNAL_SERVER_ERROR)
end

#find_route(path, cache: true) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/syntropy/app.rb', line 36

def find_route(path, cache: true)
  cached = @route_cache[path]
  return cached if cached

  entry = calculate_route(path)
  @route_cache[path] = entry if entry[:kind] != :not_found && cache
  entry
end