Module: Iodine::Base::CLI

Extended by:
CLI
Included in:
CLI
Defined in:
lib/iodine/cli.rb

Overview

Command line interface. The Ruby CLI might be changed in future versions.

Instance Method Summary collapse

Instance Method Details

#callObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/iodine/cli.rb', line 53

def call
  if ARGV[0] =~ /(\-\?)|(help)|(\?)|(h)|(\-h)$/
    return print_help
  end

  app, opt = nil, nil
  filename = ((ARGV[-2].to_s[0] != '-' || ARGV[-2].to_s == '-warmup') && ARGV[-1].to_s[0] != '-' && ARGV[-1])
  if filename
    app, opt = try_file filename;
    unless opt
      puts "* Couldn't find #{filename}\n  testing for config.ru\n"
      app, opt = try_file "config.ru"
    end
  else
    app, opt = try_file "config.ru";
  end

  unless opt
    puts "WARNING: Ruby application not found#{ filename ? " - missing both #{filename} and config.ru" : " - missing config.ru"}."
    if ARGV.index('-www') && ARGV[ARGV.index('-www') + 1]
      puts "         Running only static file service."
      opt = ::Rack::Server::Options.new.parse!([])
    else
      puts "For help run:"
      puts "       iodine -?"
      return
    end
  end

  if ARGV.index('-maxbd') && ARGV[ARGV.index('-maxbd') + 1]
    Iodine::Rack.max_body_size = ARGV[ARGV.index('-maxbd') + 1].to_i
  end
  if ARGV.index('-maxms') && ARGV[ARGV.index('-maxms') + 1]
    Iodine::Rack.max_msg_size = ARGV[ARGV.index('-maxms') + 1].to_i
  end
  if ARGV.index('-ping') && ARGV[ARGV.index('-ping') + 1]
    Iodine::Rack.ws_timeout = ARGV[ARGV.index('-ping') + 1].to_i
  end
  if ARGV.index('-www') && ARGV[ARGV.index('-www') + 1]
    Iodine::Rack.public = ARGV[ARGV.index('-www') + 1]
  end
  if ARGV.index('-tout') && ARGV[ARGV.index('-tout') + 1]
    Iodine::Rack.timeout = ARGV[ARGV.index('-tout') + 1].to_i
    puts "WARNNING: Iodine::Rack.timeout set to 0 (ignored, timeout will be ~5 seconds)."
  end
  Iodine::Rack.log = true if ARGV.index('-v')
  Iodine::Rack.log = false if ARGV.index('-q')
  Iodine.warmup(app) if ARGV.index('-warmup')
  Iodine::Rack.run(app, opt)
end


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
# File 'lib/iodine/cli.rb', line 10

def print_help
  puts <<-EOS

Iodine's HTTP/Websocket server version #{Iodine::VERSION}

Use:

    iodine <options> <filename>

Both <options> and <filename> are optional.

Available options:
 -p          Port number. Default: 3000.
 -t          Number of threads. Default: CPU core count.
 -w          Number of worker processes. Default: CPU core count.
 -www        Public folder for static file serving. Default: nil (none).
 -v          Log responses. Default: never log responses.
 -warmup     Warmup invokes autoloading (lazy loading) during server startup.
 -tout       HTTP inactivity connection timeout. Default: 5 seconds.
 -maxbd      Maximum Mb per HTTP message (max body size). Default: 50Mib.
 -maxms      Maximum Bytes per Websocket message. Default: 250Kib.
 -ping       Websocket ping interval in seconds. Default: 40 seconds.
 <filename>  Defaults to: config.ru

Example:

    iodine -p 80

    iodine -p 8080 path/to/app/conf.ru

    iodine -p 8080 -w 4 -t 16

EOS
end

#try_file(filename) ⇒ Object



46
47
48
49
# File 'lib/iodine/cli.rb', line 46

def try_file filename
  return nil unless File.exist? filename
  return ::Rack::Builder.parse_file filename
end