Class: IPCam::WebServer

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/ipcam/webserver.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bind_urlObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ipcam/webserver.rb', line 197

def bind_url
  if $bind_addr.include?(":")
    addr = "[#{$bind_addr}]" if $bind_addr.include?(":")
  else
    addr = $bind_addr
  end

  if $use_ssl
    ret = "ssl://#{addr}:#{$http_port}?key=#{$ssl_key}&cert=#{$ssl_cert}"
  else
    ret = "tcp://#{addr}:#{$http_port}"
  end

  return ret
end

.env_stringObject



214
215
216
# File 'lib/ipcam/webserver.rb', line 214

def env_string
  return ($develop_mode)? 'development':'production'
end

.setup_signalsObject

pumaのランチャークラスでのシグナルのハンドリングが邪魔なのでオーバライドして無効化する



235
236
237
# File 'lib/ipcam/webserver.rb', line 235

def @launch.setup_signals
  # nothing
end

.start(app) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/ipcam/webserver.rb', line 218

def start(app)
  set :app, app

  config  = Puma::Configuration.new { |user_config|
    user_config.quiet
    user_config.threads(4, 4)
    user_config.bind(bind_url())
    user_config.environment(env_string())
    user_config.force_shutdown_after(-1)
    user_config.app(WebServer)
  }

  @events = Puma::Events.new($log_device, $log_device)
  @launch = Puma::Launcher.new(config, :events => @events)

  # pumaのランチャークラスでのシグナルのハンドリングが
  # 邪魔なのでオーバライドして無効化する
  def @launch.setup_signals
    # nothing
  end

  @thread = Thread.start {
    begin
      $logger.info('webserver') {"started #{bind_url()}"}
      @launch.run
    ensure
      $logger.info('webserver') {"stopped"}
    end
  }

  # サーバが立ち上がりきるまで待つ
  booted  = false
  @events.on_booted {booted = true}
  sleep 0.2 until booted
end

.stopObject



254
255
256
257
258
259
260
# File 'lib/ipcam/webserver.rb', line 254

def stop
  @launch.stop
  @thread.join

  remove_instance_variable(:@launch)
  remove_instance_variable(:@thread)
end

Instance Method Details

#newObject



186
187
188
189
190
191
192
193
194
# File 'lib/ipcam/webserver.rb', line 186

def new(*)
  ret = Rack::Auth::Digest::MD5.new(super) {|user| $pwd_db[user]}

  ret.realm            = TRADITIONAL_NAME
  ret.opaque           = SecureRandom.alphanumeric(32)
  ret.passwords_hashed = true

  return ret
end