Module: Syntropy::Utilities

Included in:
Syntropy
Defined in:
lib/syntropy/utils.rb

Overview

Utilities for use in modules

Constant Summary collapse

BUILTIN_APPLET_ROOT_DIR =
File.expand_path(File.join(__dir__, 'applets/builtin'))

Instance Method Summary collapse

Instance Method Details

#app(**env) ⇒ Object



37
38
39
# File 'lib/syntropy/utils.rb', line 37

def app(**env)
  Syntropy::App.new(**env)
end

#builtin_applet(env, mount_path: '/.syntropy') ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/syntropy/utils.rb', line 42

def builtin_applet(env, mount_path: '/.syntropy')
  app(
    machine:    env[:machine],
    root_dir:   BUILTIN_APPLET_ROOT_DIR,
    mount_path: mount_path,
    builtin_applet_path: nil,
    watch_files: nil
  )
end

#page_list(env, ref) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/syntropy/utils.rb', line 27

def page_list(env, ref)
  full_path = File.join(env[:root_dir], ref)
  raise 'Not a directory' if !File.directory?(full_path)

  Dir[File.join(full_path, '*.md')].sort.map {
    atts, markdown = Syntropy.parse_markdown_file(it, env)
    { atts:, markdown: }
  }
end

#route_by_host(env, map = nil) ⇒ Object

Returns a request handler that routes request according to



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/syntropy/utils.rb', line 7

def route_by_host(env, map = nil)
  root = env[:root_dir]
  sites = Dir[File.join(root, '*')]
          .reject { File.basename(it) =~ /^_/ }
          .select { File.directory?(it) }
          .each_with_object({}) { |fn, h|
            name = File.basename(fn)
            h[name] = Syntropy::App.new(**env.merge(root_dir: fn))
          }

  # copy over map refs
  map&.each { |k, v| sites[k] = sites[v] }

  #
  lambda { |req|
    site = sites[req.host]
    site ? site.call(req) : req.respond(nil, ':status' => Status::BAD_REQUEST)
  }
end