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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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

.env_stringObject



229
230
231
# File 'lib/ipcam/webserver.rb', line 229

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

.setup_signalsObject

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



250
251
252
# File 'lib/ipcam/webserver.rb', line 250

def @launch.setup_signals
  # nothing
end

.start(app) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# 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

.stopObject



269
270
271
272
273
274
275
# File 'lib/ipcam/webserver.rb', line 269

def stop
  @launch.stop
  @thread.join

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

Instance Method Details

#add_user(user, pass) ⇒ Object



202
203
204
205
206
207
208
209
# 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

#newObject



187
188
189
190
191
192
193
194
195
# 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