Class: BitClust::Subcommands::ServerCommand

Inherits:
BitClust::Subcommand show all
Defined in:
lib/bitclust/subcommands/server_command.rb

Instance Method Summary collapse

Methods inherited from BitClust::Subcommand

#align_progress_bar_title, #error, #help, #option_error

Constructor Details

#initializeServerCommand

Returns a new instance of ServerCommand.



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
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
# File 'lib/bitclust/subcommands/server_command.rb', line 16

def initialize
  super
  require 'uri'

  @params = {
    :BindAddress => "0.0.0.0",
    :Port => 10080
  }
  @baseurl = nil
  @dbpath = nil
  @srcdir = @datadir = @themedir = @theme = @templatedir = nil
  @encoding = 'utf-8'   # encoding of view
  if Object.const_defined?(:Encoding)
    begin
      verbose, $VERBOSE = $VERBOSE, false
      Encoding.default_external = @encoding
    ensure
      $VERBOSE = verbose
    end
  end

  @debugp = false
  @autop = false
  @browser = nil
  @pid_file = nil
  @capi = false

  @parser.banner = "#{$0} [--bind-address=ADDR] [--port=NUM] --baseurl=URL --database=PATH [--srcdir=PATH] [--datadir=PATH] [--themedir=PATH] [--debug] [--auto] [--browser=BROWSER] [--pid-file=PATH] [--capi]"
  @parser.on('--bind-address=ADDR', 'Bind address') {|addr|
    @params[:BindAddress] = addr
  }
  @parser.on('--port=NUM', "Listening port number (default: #{@params[:Port]})") {|num|
    @params[:Port] = num.to_i
  }
  @parser.on('--baseurl=URL', 'The base URL to host.') {|url|
    @baseurl = url
  }
  @parser.on('--database=PATH', 'MethodDatabase root directory.') {|path|
    @dbpath = path
  }
  @parser.on('--srcdir=PATH', 'BitClust source directory.') {|path|
    set_srcdir(path)
  }
  @parser.on('--datadir=PATH', 'BitClust data directory.') {|path|
    @datadir = path
  }
  @parser.on('--templatedir=PATH', 'Template directory.') {|path|
    @templatedir = path
  }
  @parser.on('--themedir=PATH', 'BitClust theme directory.') {|path|
    @themedir = path
  }
  @parser.on('--theme=THEME', 'BitClust theme.') {|th|
    @theme = th
  }
  @parser.on('--[no-]debug', 'Debug mode.') {|flag|
    @debugp = flag
  }
  @parser.on('--[no-]auto', 'Auto mode.') {|flag|
    @autop = flag
  }
  @parser.on('--browser=BROWSER', 'Open with the browser.') {|path|
    @browser = path
  }
  @parser.on('--pid-file=PATH', 'Write pid of the daemon to the specified file.') {|path|
    @pid_file = path
  }
  @parser.on('--capi', 'see also FunctionDatabase.') {|path|
    @capi = true
  }
end

Instance Method Details

#exec(argv, options) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/bitclust/subcommands/server_command.rb', line 118

def exec(argv, options)
  begin
    require 'webrick'
  rescue LoadError
    abort "webrick is not found. You may need to `gem install webrick` to install webrick."
  end
  require 'bitclust/app'
  if @debugp
    @params[:Logger] = WEBrick::Log.new($stderr, WEBrick::Log::DEBUG)
    @params[:AccessLog] = [
                           [ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT  ],
                           [ $stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT ],
                           [ $stderr, WEBrick::AccessLog::AGENT_LOG_FORMAT   ],
                          ]
  else
    @params[:Logger] = WEBrick::Log.new($stderr, WEBrick::Log::INFO)
    @params[:AccessLog] = []
  end
  basepath = URI.parse(@baseurl).path
  server = WEBrick::HTTPServer.new(@params)

  if @autop
    app = App.new(
                  :dbpath => Dir.glob("#{@database_prefix}-*"),
                  :baseurl => @baseurl,
                  :datadir => @datadir,
                  :templatedir => @templatedir,
                  :theme => @theme,
                  :encoding => @encoding,
                  :capi => @capi
                  )
    app.interfaces.each do |version, interface|
      server.mount(File.join(basepath, version), interface)
    end
    server.mount(File.join(basepath, '/'), app)
  else
    viewpath = File.join(basepath, 'view')
    app = App.new(
                  :viewpath => viewpath,
                  :dbpath => @dbpath,
                  :baseurl => @baseurl,
                  :datadir => @datadir,
                  :templatedir => @templatedir,
                  :theme => @theme,
                  :encoding => @encoding,
                  :capi => @capi
                  )
    app.interfaces.each do |viewpath, interface|
      server.mount viewpath, interface
    end
    # Redirect from '/' to "#{viewpath}/"
    server.mount('/', app)
  end

  server.mount File.join(basepath, 'theme/'), WEBrick::HTTPServlet::FileHandler, @themedir

  if @debugp
    trap(:INT) { server.shutdown }
  else
    WEBrick::Daemon.start do
      trap(:TERM) {
        server.shutdown
        begin
          File.unlink @pid_file if @pid_file
        rescue Errno::ENOENT
        end
      }
      File.open(@pid_file, 'w') {|f| f.write Process.pid } if @pid_file
    end
  end
  exit if $".include?("exerb/mkexy.rb")
  if @autop && !@browser
    case RUBY_PLATFORM
    when /mswin/
      @browser = "start"
    end
  end
  system("#{@browser} http://localhost:#{@params[:Port]}/") if @browser
  server.start
end

#parse(argv) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bitclust/subcommands/server_command.rb', line 88

def parse(argv)
  super
  load_config_file
  set_srcdir(srcdir_root) unless @srcdir

  unless @baseurl
    $stderr.puts "missing base URL.  Use --baseurl or check ~/.bitclust/config"
    exit 1
  end
  unless @dbpath || @autop
    $stderr.puts "missing database path.  Use --database"
    exit 1
  end
  unless @datadir
    $stderr.puts "missing datadir.  Use --datadir"
    exit 1
  end
  unless @themedir
    $stderr.puts "missing themedir.  Use --themedir"
    exit 1
  end
  if @pid_file
    if File.exist?(@pid_file)
      $stderr.puts "There is still #{@pid_file}.  Is another process running?"
      exit 1
    end
    @pid_file = File.expand_path(@pid_file)
  end
end