Class: IPCam::WebServer

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

Class Method Summary collapse

Class Method Details

.bind_urlObject



138
139
140
141
142
143
144
145
146
# File 'lib/ipcam/webserver.rb', line 138

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

  return "tcp://#{addr}:#{$http_port}"
end

.env_stringObject



149
150
151
# File 'lib/ipcam/webserver.rb', line 149

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

.setup_signalsObject

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



170
171
172
# File 'lib/ipcam/webserver.rb', line 170

def @launch.setup_signals
  # nothing
end

.start(app) ⇒ Object



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
# File 'lib/ipcam/webserver.rb', line 153

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



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

def stop
  @launch.stop
  @thread.join

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