Class: OKF::CLI::Server

Inherits:
Command show all
Defined in:
lib/okf/cli/server.rb

Overview

Boot the graph server. One verb covers three intentions and the argument count is the whole interface: one dir serves it at /, several serve them behind a hub, none serves the registry. Passing dirs never registers them.

Constant Summary

Constants inherited from Command

Command::DUCK_TYPE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

hidden?, #initialize

Constructor Details

This class inherits a constructor from OKF::CLI::Command

Class Method Details

.groupObject



13
14
15
# File 'lib/okf/cli/server.rb', line 13

def self.group
  :act
end

.help_rowsObject



17
18
19
20
21
# File 'lib/okf/cli/server.rb', line 17

def self.help_rows
  [
    [ "server    [DIR|@slug…] [-p PORT] [--bind ADDR] [...]", "serve one bundle, or many behind a hub" ]
  ]
end

.idObject



9
10
11
# File 'lib/okf/cli/server.rb', line 9

def self.id
  :server
end

Instance Method Details

#call(argv) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/okf/cli/server.rb', line 23

def call(argv)
  require "okf/server/app"
  require "rack/deflater"

  options = { port: 8808, bind: "127.0.0.1", title: nil, link: nil, layout: "cose", read_only: false }
  parser = OptionParser.new do |o|
    o.banner = "Usage: okf server [DIR|@slug…] [-p PORT] [--bind ADDR] [--layout NAME] [-t title] [-l url]"
    o.on("-p", "--port PORT", Integer, "port to serve on (default #{options[:port]})") { |v| options[:port] = v }
    o.on("--bind ADDR", "address to bind (default #{options[:bind]})") { |v| options[:bind] = v }
    o.on("-t", "--title TITLE", "graph title, single bundle only (default: parent/bundle dir name)") { |v| options[:title] = v }
    o.on("-l", "--link URL", "source URL shown in the header, single bundle only") { |v| options[:link] = v }
    o.on("--layout NAME", OKF::Render::Graph::LAYOUTS, "initial layout (#{OKF::Render::Graph::LAYOUTS.join(", ")})") { |v| options[:layout] = v }
    o.on("--read-only", "serve the bundles list without its registry controls") { options[:read_only] = true }
    help_flag(o)
  end
  dirs = positional_dirs(parser, argv) or return 2

  # A flag that will have no effect in this mode gets a note, not silence.
  @err.puts "note: --title/--link apply to a single-bundle server; ignored" if dirs.size != 1 && (options[:title] || options[:link])

  # One dir keeps the historical single-bundle server at `/`; zero (the
  # persistent registry) or many (ephemeral) fan out behind a hub.
  if dirs.size == 1
    folder = OKF::Bundle::Folder.load(dirs.first)
    report_skipped(folder)
    run_server(folder, options)
  else
    run_hub(dirs, options)
  end
  0
rescue OKF::Error => e
  usage_error(e.message)
end