|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Syntropy |
| 4 | + class Router |
| 5 | + attr_reader :cache |
| 6 | + |
| 7 | + def initialize(opts, module_loader = nil) |
| 8 | + raise 'Invalid location given' if !File.directory?(opts[:location]) |
| 9 | + |
| 10 | + @opts = opts |
| 11 | + @machine = opts[:machine] |
| 12 | + @root = File.expand_path(opts[:location]) |
| 13 | + @mount_path = opts[:mount_path] || '/' |
| 14 | + @rel_path_re ||= /^#{@root}/ |
| 15 | + @module_loader = module_loader |
| 16 | + |
| 17 | + @cache = {} # maps url path to route entry |
| 18 | + @routes = {} # maps canonical path to route entry (actual routes) |
| 19 | + @files = {} # maps filename to entry |
| 20 | + @deps = {} # maps filenames to array of dependent entries |
| 21 | + @x = {} # maps directories to hook chains |
| 22 | + |
| 23 | + scan_routes |
| 24 | + start_file_watcher if opts[:watch_files] |
| 25 | + end |
| 26 | + |
| 27 | + def [](path) |
| 28 | + get_route_entry(path) |
| 29 | + end |
| 30 | + |
| 31 | + private |
| 32 | + |
| 33 | + HIDDEN_RE = /^_/ |
| 34 | + |
| 35 | + def scan_routes(dir = nil) |
| 36 | + dir ||= @root |
| 37 | + |
| 38 | + Dir[File.join(dir, '*')].each do |
| 39 | + basename = File.basename(it) |
| 40 | + next if (basename =~ HIDDEN_RE) |
| 41 | + |
| 42 | + File.directory?(it) ? scan_routes(it) : add_route(it) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + def add_route(fn) |
| 47 | + kind = route_kind(fn) |
| 48 | + rel_path = path_rel(fn) |
| 49 | + canonical_path = path_canonical(rel_path, kind) |
| 50 | + entry = { kind:, fn:, canonical_path: } |
| 51 | + entry[:handle_subtree] = true if (kind == :module) && !!(fn =~ /\+\.rb$/) |
| 52 | + |
| 53 | + @routes[canonical_path] = entry |
| 54 | + @files[fn] = entry |
| 55 | + end |
| 56 | + |
| 57 | + def route_kind(fn) |
| 58 | + case File.extname(fn) |
| 59 | + when '.md' |
| 60 | + :markdown |
| 61 | + when '.rb' |
| 62 | + :module |
| 63 | + else |
| 64 | + :static |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def path_rel(path) |
| 69 | + path.gsub(@rel_path_re, '') |
| 70 | + end |
| 71 | + |
| 72 | + def path_abs(path, base) |
| 73 | + File.join(base, path) |
| 74 | + end |
| 75 | + |
| 76 | + PATH_PARENT_RE = /^(.+)?\/([^\/]+)$/ |
| 77 | + |
| 78 | + def path_parent(path) |
| 79 | + return nil if path == '/' |
| 80 | + |
| 81 | + path.match(PATH_PARENT_RE)[1] || '/' |
| 82 | + end |
| 83 | + |
| 84 | + MD_EXT_RE = /\.md$/ |
| 85 | + RB_EXT_RE = /[+]?\.rb$/ |
| 86 | + INDEX_RE = /^(.*)\/index[+]?\.(?:rb|md|html)$/ |
| 87 | + |
| 88 | + def path_canonical(rel_path, kind) |
| 89 | + clean = path_clean(rel_path, kind) |
| 90 | + clean.empty? ? @mount_path : File.join(@mount_path, clean) |
| 91 | + end |
| 92 | + |
| 93 | + def path_clean(rel_path, kind) |
| 94 | + if (m = rel_path.match(INDEX_RE)) |
| 95 | + return m[1] |
| 96 | + end |
| 97 | + |
| 98 | + case kind |
| 99 | + when :static |
| 100 | + rel_path |
| 101 | + when :markdown |
| 102 | + rel_path.gsub(MD_EXT_RE, '') |
| 103 | + when :module |
| 104 | + rel_path.gsub(RB_EXT_RE, '') |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + def get_route_entry(path, use_cache: true) |
| 109 | + if use_cache |
| 110 | + cached = @cache[path] |
| 111 | + return cached if cached |
| 112 | + end |
| 113 | + |
| 114 | + entry = find_route_entry(path) |
| 115 | + set_cache(path, entry) if use_cache && entry[:kind] != :not_found |
| 116 | + entry |
| 117 | + end |
| 118 | + |
| 119 | + def set_cache(path, entry) |
| 120 | + @cache[path] = entry |
| 121 | + (entry[:cache_keys] ||= {})[path] = true |
| 122 | + end |
| 123 | + |
| 124 | + # We don't allow access to path with /.., or entries that start with _ |
| 125 | + FORBIDDEN_RE = %r{(/_)|((/\.\.)/?)} |
| 126 | + NOT_FOUND = { kind: :not_found }.freeze |
| 127 | + |
| 128 | + def find_route_entry(path) |
| 129 | + return NOT_FOUND if path =~ FORBIDDEN_RE |
| 130 | + |
| 131 | + @routes[path] || find_up_tree_module(path) || NOT_FOUND |
| 132 | + end |
| 133 | + |
| 134 | + def find_up_tree_module(path) |
| 135 | + parent_path = path_parent(path) |
| 136 | + return nil if !parent_path |
| 137 | + |
| 138 | + entry = @routes[parent_path] |
| 139 | + return entry if entry && entry[:handle_subtree] |
| 140 | + |
| 141 | + find_up_tree_module(parent_path) |
| 142 | + end |
| 143 | + |
| 144 | + def start_file_watcher |
| 145 | + @opts[:logger]&.call('Watching for file changes...', nil) |
| 146 | + @machine.spin { file_watcher_loop } |
| 147 | + end |
| 148 | + |
| 149 | + def file_watcher_loop |
| 150 | + wf = @opts[:watch_files] |
| 151 | + period = wf.is_a?(Numeric) ? wf : 0.1 |
| 152 | + Syntropy.file_watch(@machine, @root, period: period) do |event, fn| |
| 153 | + handle_changed_file(event, fn) |
| 154 | + rescue Exception => e |
| 155 | + p e |
| 156 | + p e.backtrace |
| 157 | + exit! |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + def handle_changed_file(event, fn) |
| 162 | + @opts[:logger]&.call("Detected changed file: #{event} #{fn}") |
| 163 | + @module_loader&.invalidate(fn) |
| 164 | + case event |
| 165 | + when :added |
| 166 | + add_route(fn) |
| 167 | + @cache.clear # TODO: remove only relevant cache entries |
| 168 | + when :removed |
| 169 | + entry = @files[fn] |
| 170 | + if entry |
| 171 | + remove_entry_cache_keys(entry) |
| 172 | + @routes.delete(entry[:canonical_path]) |
| 173 | + @files.delete(fn) |
| 174 | + end |
| 175 | + when :modified |
| 176 | + entry = @files[fn] |
| 177 | + if entry && entry[:kind] == :module |
| 178 | + # invalidate the entry proc, so it will be recalculated |
| 179 | + entry[:proc] = nil |
| 180 | + end |
| 181 | + end |
| 182 | + end |
| 183 | + |
| 184 | + def remove_entry_cache_keys(entry) |
| 185 | + entry[:cache_keys]&.each_key { @cache.delete(it) }.clear |
| 186 | + end |
| 187 | + end |
| 188 | +end |
0 commit comments