Sha256: 8fc66767ee5b8d5bed4588ff8e8c6eba240143c73cb4548dda5a00935edc589c

Contents?: true

Size: 783 Bytes

Versions: 1

Compression:

Stored size: 783 Bytes

Contents

# frozen_string_literal: true

class Async::App::WebServer
  include Async::App::Component

  APP_ADDED = "async-app.web_app.added"

  class Router
    def initialize
      @apps = []
    end

    def add(app) = @apps << app

    def call(request)
      @apps.reverse_each { return Protocol::HTTP::Response[*_1.call(request)] if _1.can_handle?(request) }

      Protocol::HTTP::Response[404, {}, ["Not found"]]
    end
  end

  def initialize(port: 8080)
    @router = Router.new
    @endpoint = Async::HTTP::Endpoint.parse("http://0.0.0.0:#{port}")
  end

  def after_init = bus.subscribe(APP_ADDED) { add_app(_1) }

  def add_app(app) = @router.add(app)

  def run!
    Async { Async::HTTP::Server.new(@router, @endpoint).run }
    info { "Started on #{@endpoint.url}" }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
async-tools-0.2.10 lib/async/app/web_server.rb