Class: Centralbank::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/centralbank/tool.rb

Instance Method Summary collapse

Instance Method Details

#run(args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
56
57
58
59
60
61
# File 'lib/centralbank/tool.rb', line 8

def run( args )
  opts = {}

  parser = OptionParser.new do |cmd|
    cmd.banner = "Usage: centralbank [options]"

    cmd.separator ""
    cmd.separator "  Wallet options:"

    cmd.on("-n", "--name=NAME", "Address name (default: Alice)") do |name|
      ## use -a or --adr or --address as option flag - why? why not?
      ##  note: default now picks a random address from WALLET_ADDRESSES
      opts[:address] = name
    end


    cmd.separator ""
    cmd.separator "  Server (node) options:"

    cmd.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") do |host|
      opts[:Host] = host    ## note: rack server handler expects :Host
    end

    cmd.on("-p", "--port PORT", "use PORT (default: 4567)") do |port|
      opts[:Port] = port    ## note: rack server handler expects :Post
    end

    cmd.on("-h", "--help", "Prints this help") do
      puts cmd
      exit
    end
  end

  parser.parse!( args )
  pp opts


  ###################
  ## startup server (via rack interface/handler)

  app_class = Service    ##  use  app = Service.new  -- why? why not?
  host = opts[:Host] || '0.0.0.0'
  port = opts[:Port] || '4567'

  Centralbank.configure do |config|
    config.address = opts[:address] || 'Alice'
  end

  Rack::Handler::WEBrick.run( app_class, Host: host, Port: port ) do |server|
      ## todo: add traps here - why, why not??
  end


end