Sha256: f8b92d7a956da3fd14840ca04ab84b4b8da4805446e70e37e547f8a7bfcc3765

Contents?: true

Size: 920 Bytes

Versions: 1

Compression:

Stored size: 920 Bytes

Contents

# frozen_string_literal: true

require 'async'
require 'async/http/endpoint'
require 'falcon'

module Cosensee
  # Web server for serving static files
  class WebServer
    def initialize(dir:, server_url:, logger:)
      @dir = dir
      @server_url = server_url
      @logger = logger

      endpoint = Async::HTTP::Endpoint.parse(server_url)
      handler = StaticFileHandler.new(dir:, logger:)
      app = Falcon::Server.middleware(handler)
      @server = Falcon::Server.new(app, endpoint)
    end

    def start
      Async do |task|
        @logger.info("Serving files from #{File.expand_path(@dir)} at #{@server_url}")

        Signal.trap('INT') do
          @logger.info("\nShutting down the server...")
          task.stop
        end

        @server.run
      end
    rescue Interrupt
      @logger.warn("\nServer interrupted. Exiting.")
    ensure
      @logger.info('Cleanup complete.')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cosensee-0.8.0 lib/cosensee/web_server.rb