class IPCam::WebServer

Public Class Methods

add_user(user, pass) click to toggle source
# File lib/ipcam/webserver.rb, line 202
def add_user(user, pass)
  $passwd_db[user] = make_a1_string(user, pass)

  $passwd_file.open("w") { |f|
    f.chmod(0o600)
    f.write($passwd_db.to_yaml)
  }
end
env_string() click to toggle source
# File lib/ipcam/webserver.rb, line 229
def env_string
  return ($develop_mode)? 'development':'production'
end
make_a1_string(user, pass) click to toggle source
# File lib/ipcam/webserver.rb, line 197
def make_a1_string(user, pass)
  return Digest::MD5.hexdigest("#{user}:#{TRADITIONAL_NAME}:#{pass}")
end
new(*) click to toggle source
Calls superclass method
# File lib/ipcam/webserver.rb, line 187
def new(*)
  ret = Rack::Auth::Digest::MD5.new(super) {|user| $passwd_db[user]}

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

  return ret
end
start(app) click to toggle source
# File lib/ipcam/webserver.rb, line 233
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
stop() click to toggle source
# File lib/ipcam/webserver.rb, line 269
def stop
  @launch.stop
  @thread.join

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

Private Class Methods

bind_url() click to toggle source
# File lib/ipcam/webserver.rb, line 212
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

Public Instance Methods

app() click to toggle source
# File lib/ipcam/webserver.rb, line 38
def app
  return (@app ||= settings.app)
end
find_resource(type, name) click to toggle source
# File lib/ipcam/webserver.rb, line 42
def find_resource(type, name)
  ret = RESOURCE_DIR + "extern" + type + name
  return ret if ret.exist?

  ret = RESOURCE_DIR + "common" + type + name
  return ret if ret.exist?

  ret = APP_RESOURCE_DIR + type + name
  return ret if ret.exist?

  return nil
end
websock_url() click to toggle source
# File lib/ipcam/webserver.rb, line 55
def websock_url
  return "#{($use_ssl)? "wss":"ws"}://${location.hostname}:#{$ws_port}"
end