|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'securerandom' |
| 4 | + |
| 5 | +module Syntropy |
| 6 | + # Utilities for use in modules |
| 7 | + module ControllerExtensions |
| 8 | + # Returns a unique temporary path |
| 9 | + # |
| 10 | + # @param prefix [String] temp file prefix |
| 11 | + # @return [String] |
| 12 | + def tmp_path(prefix = 'syntropy') |
| 13 | + "/tmp/#{prefix}-#{SecureRandom.hex(16)}" |
| 14 | + end |
| 15 | + |
| 16 | + # Returns a request handler that routes request according to the host |
| 17 | + # header. Looks for site directories (named by host name) in the app's root |
| 18 | + # directory. A map may be given in order to provide additional hostnames to |
| 19 | + # site directories. |
| 20 | + # |
| 21 | + # @param dir [String, nil] relative directory path for host sites |
| 22 | + # @param map [Hash, nil] hash mapping host names to relative site directory |
| 23 | + # @return [Proc] router proc |
| 24 | + def dispatch_by_host(dir = nil, map = nil) |
| 25 | + raise Syntropy::Error, 'Must provide dir and/or map' if !dir && !map |
| 26 | + |
| 27 | + site_map = {} |
| 28 | + setup_directory_sites(dir, site_map) if dir |
| 29 | + setup_mapped_sites(map, site_map) if map |
| 30 | + |
| 31 | + ->(req) do |
| 32 | + site = site_map[req.host] |
| 33 | + site ? site.call(req) : req.respond(nil, ':status' => HTTP::BAD_REQUEST) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + # Returns a request handler that handles requests by calling the appropriate |
| 38 | + # module method (e.g. get, post, etc.) |
| 39 | + # |
| 40 | + # @return [Proc] |
| 41 | + def dispatch_by_http_method |
| 42 | + ->(req) do |
| 43 | + route_by_http_method(req) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + # Returns a list of parsed markdown pages at the given path. |
| 48 | + # |
| 49 | + # @param env [Hash] app environment hash |
| 50 | + # @param ref [String] directory path |
| 51 | + # @return [Array<Hash>] array of page entries |
| 52 | + def page_list(env, ref) |
| 53 | + full_path = File.join(env[:app_root], ref) |
| 54 | + raise 'Not a directory' if !File.directory?(full_path) |
| 55 | + |
| 56 | + Dir[File.join(full_path, '*.md')].sort.map { |
| 57 | + atts, markdown = Syntropy::Markdown.parse(it, env) |
| 58 | + { atts:, markdown: } |
| 59 | + } |
| 60 | + end |
| 61 | + |
| 62 | + # Instantiates a Syntropy app for the given environment hash. |
| 63 | + # |
| 64 | + # @return [Syntropy::App] |
| 65 | + def app(**) |
| 66 | + Syntropy::App.new(**) |
| 67 | + end |
| 68 | + |
| 69 | + BUILTIN_APPLET_app_root = File.expand_path(File.join(__dir__, 'applets/builtin')) |
| 70 | + |
| 71 | + # Creates a builtin applet with the given environment hash. By default the |
| 72 | + # builtin applet is mounted at /.syntropy. |
| 73 | + # |
| 74 | + # @param env [Hash] app environment |
| 75 | + # @param mount_path [String] mount path for the builtin applet |
| 76 | + # @return [Syntropy::App] applet |
| 77 | + def builtin_applet(env, mount_path: '/.syntropy') |
| 78 | + app( |
| 79 | + machine: env[:machine], |
| 80 | + app_root: BUILTIN_APPLET_app_root, |
| 81 | + mount_path: mount_path, |
| 82 | + builtin_applet_path: nil, |
| 83 | + watch_files: nil |
| 84 | + ) |
| 85 | + end |
| 86 | + |
| 87 | + private |
| 88 | + |
| 89 | + # Finds sites in the root directory for the given environment hash, adds |
| 90 | + # entries to the given site map. |
| 91 | + # |
| 92 | + # @param dir [String] relative or absolute path |
| 93 | + # @param site_map [Hash] site map |
| 94 | + # @return [void] |
| 95 | + def setup_directory_sites(ref, site_map) |
| 96 | + app_root = @app ? @app.app_root : @env[:app_root] |
| 97 | + ref = normalize_import_ref(ref) |
| 98 | + |
| 99 | + Dir[File.join(app_root, ref, '*')] |
| 100 | + .select { File.directory?(it) && File.basename(it) !~ /^_/ } |
| 101 | + .each { |entry| site_map[File.basename(entry)] = make_app(entry) } |
| 102 | + end |
| 103 | + |
| 104 | + # converts the given map entries by adding entries to the given site map. |
| 105 | + # |
| 106 | + # @param map [Hash] ref map |
| 107 | + # @param site_map [Hash] site map |
| 108 | + # @return [void] |
| 109 | + def setup_mapped_sites(map, site_map) |
| 110 | + app_root = @app ? @app.app_root : @env[:app_root] |
| 111 | + map.each do |name, ref| |
| 112 | + ref = File.join(File.dirname(@ref), ref) if ref !~ /^\// |
| 113 | + site_root = File.join(app_root, ref) |
| 114 | + site_map[name] = make_app(site_root) |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + # Creates an app loaded from the given root directory, with the present |
| 119 | + # mount path. |
| 120 | + def make_app(site_root) |
| 121 | + mount_path = @ref == '/_site' ? '/' : @ref |
| 122 | + env = @env.merge(app_root: site_root, mount_path:) |
| 123 | + Syntropy::App.new(**env) |
| 124 | + end |
| 125 | + |
| 126 | + # Handles the given request by calling the module method corresponding to |
| 127 | + # the request's HTTP method. If no method is found, raises a |
| 128 | + # method_not_allowed error. |
| 129 | + def route_by_http_method(req) |
| 130 | + sym = req.method.to_sym |
| 131 | + raise Syntropy::Error.method_not_allowed if !respond_to?(sym) |
| 132 | + |
| 133 | + send(sym, req) |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments