Sha256: a48002582875f78a2d78bda633e4611f445948e68fda6422380d57b9683a4120

Contents?: true

Size: 1.58 KB

Versions: 14

Compression:

Stored size: 1.58 KB

Contents

# frozen-string-literal: true

require 'rack'
require 'puma'
require 'roda'

module Rodbot
  class Services
    class App
      include Rodbot::Memoize

      class << self
        include Rodbot::Memoize

        # URL (including port) to reach the app service locally
        #
        # @return [String] URL
        memoize def url
          [
            (ENV['RODBOT_APP_URL'] || 'http://localhost'),
            Rodbot.config(:port)
          ].join(':')
        end
      end

      def tasks(**)
        puts "Starting app service on http://#{bind.join(':')}"
        [method(:run)]
      end

      private

      def run
        Dir.chdir(Rodbot.env.root)
        Puma::Server.new(app, nil, options).tap do |server|
          server.add_tcp_listener(*bind)
          server.app = ::Rack::CommonLogger.new(app, logger)
        end.run.join
      end

      memoize def bind
        [
          (ENV['RODBOT_APP_HOST'] || 'localhost'),
          Rodbot.config(:port)
        ]
      end

      memoize def app
        ::Rack::Builder.parse_file(Rodbot.env.root.join('config.ru').to_s)
      end

      def options
        {
          lowlevel_error_handler: method(:lowlevel_error_handler),
          log_writer: Puma::LogWriter.null,
          min_threads: Rodbot.config(:app, :threads).min,
          max_threads: Rodbot.config(:app, :threads).max
        }
      end

      memoize def logger
        Rodbot::Log.logger('app')
      end

      def lowlevel_error_handler(error)
        logger.error("#{error.message}: #{error.backtrace.first}")
        [500, {}, ['Oops!']]
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rodbot-0.4.5 lib/rodbot/services/app.rb
rodbot-0.4.4 lib/rodbot/services/app.rb
rodbot-0.4.3 lib/rodbot/services/app.rb
rodbot-0.4.2 lib/rodbot/services/app.rb
rodbot-0.4.1 lib/rodbot/services/app.rb
rodbot-0.4.0 lib/rodbot/services/app.rb
rodbot-0.3.4 lib/rodbot/services/app.rb
rodbot-0.3.3 lib/rodbot/services/app.rb
rodbot-0.3.2 lib/rodbot/services/app.rb
rodbot-0.3.1 lib/rodbot/services/app.rb
rodbot-0.3.0 lib/rodbot/services/app.rb
rodbot-0.2.0 lib/rodbot/services/app.rb
rodbot-0.1.1 lib/rodbot/services/app.rb
rodbot-0.1.0 lib/rodbot/services/app.rb