class Midori::Runner

Abstract runner class to control instance of Midori Server @attr [String] bind the address to bind @attr [Integer] port the port to bind @attr [Logger] logger midori logger

Attributes

bind[R]
logger[R]
port[R]

Public Class Methods

new(api) click to toggle source

Define status of a runner @param [Class] api inherited from [Midori::API]

# File lib/midori/runner.rb, line 11
def initialize(api)
  configure = Midori::Configure
  @logger = configure.logger
  Midori.logger = configure.logger
  @bind = configure.bind
  @port = configure.port
  @api = (api.is_a? Midori::APIEngine) ? api : Midori::APIEngine.new(api, configure.route_type)
  @before = configure.before
end

Public Instance Methods

running?() click to toggle source

Get Midori server whether running @return [Boolean] [true] running @return [Boolean] [false] not running

# File lib/midori/runner.rb, line 24
def running?
  !!@server
end
start() click to toggle source

Start the Midori server @note This is an async method, but no callback

# File lib/midori/runner.rb, line 30
def start
  return false if running?
  @logger.info "Midori #{Midori::VERSION} is now running on #{bind}:#{port}".blue
  init_socket
  Fiber.schedule do
    @logger.info 'Midori is booting...'.blue
    @before.call
    @logger.info 'Midori is serving...'.blue
    Fiber.schedule do
      loop do
        socket = @server.accept
        connection = Midori::Connection.new(socket)
        connection.server_initialize(@api, @logger)
        connection.listen
      end
    end
  end
  nil
end
stop() click to toggle source

Stop the Midori server @note This is an async method, but no callback @return [Boolean] [true] stop successfully @return [Boolean] [false] nothing to stop

# File lib/midori/runner.rb, line 65
def stop
  if running?
    @logger.info 'Stopping Midori'.blue
    @server.close
    @server = nil
    true
  else
    @logger.error 'Midori Server has NOT been started'.red
    false
  end
end

Private Instance Methods

init_socket() click to toggle source
# File lib/midori/runner.rb, line 50
        def init_socket
  @server = Socket.new Socket::AF_INET, Socket::SOCK_STREAM
  @server.reuse_port if Midori::Configure.socket_reuse_port
  @server.bind Addrinfo.tcp @bind, @port
  @server.listen Socket::SOMAXCONN
  if Midori::Configure.tcp_fast_open
    tfo = @server.tcp_fast_open
    @logger.warn 'Failed to use TCP Fast Open feature on your OS'.yellow unless tfo
  end
end