Sha256: 87fe55b1a42ee326979d084643ce32ee64e850184bbf4c5b8802e3f13fd6b324

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require "rack"
require "rack/handler"

module Rack
  module Handler
    class Toycol
      class << self
        attr_writer :preferred_background_server, :host, :port

        def run(app, _ = {})
          @app = app
          @host ||= ::Toycol::DEFAULT_HOST
          @port ||= "9292"

          if (child_pid = fork)
            ::Toycol::Proxy.new(@host, @port).start
            Process.waitpid(child_pid)
          else
            run_background_server
          end
        end

        private

        def select_background_server
          case @preferred_background_server
          when "puma"
            return "puma" if puma_requireable?

            raise LoadError, "Puma is not installed in your environment."
          when nil
            puma_requireable? ? "puma" : "build_in"
          else
            "build_in"
          end
        rescue LoadError
          Process.kill(:INT, Process.ppid)
          abort
        end

        def puma_requireable?
          require "rack/handler/puma"
          true
        rescue LoadError
          false
        end

        def run_background_server
          case select_background_server
          when "puma"
            puts "Toycol starts Puma in single mode, listening on unix://#{::Toycol::UNIX_SOCKET_PATH}"
            Rack::Handler::Puma.run(@app, **{ Host: ::Toycol::UNIX_SOCKET_PATH, Silent: true })
          else
            puts "Toycol starts build-in server, listening on unix://#{::Toycol::UNIX_SOCKET_PATH}"
            ::Toycol::Server.run(@app, **{ Path: ::Toycol::UNIX_SOCKET_PATH, Port: @port })
          end
        end
      end
    end

    register :toycol, Toycol
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
toycol-0.3.0 lib/rack/handler/toycol.rb
toycol-0.2.2 lib/rack/handler/toycol.rb