Sha256: 179075e4402131b41088cb5787c1919d7e630ad645c772034a49f0daf6ac3a41
Contents?: true
Size: 1.36 KB
Versions: 1
Compression:
Stored size: 1.36 KB
Contents
# frozen_string_literal: true require 'mini_mime' require 'uri' module Cosensee class WebServer # Rack application for Falcon that serves static files from a directory. class StaticFileHandler def initialize(dir:, logger:) @dir = dir @logger = logger end attr_reader :dir, :logger def call(env) path_info = env['PATH_INFO'] logger.info("Request path: #{env['PATH_INFO']}") path_info = if path_info.start_with?('/') "/#{URI.decode_www_form_component(path_info.slice(1..-1))}" else URI.decode_www_form_component(path_info) end path = File.join(dir, path_info) path = File.join(path, 'index.html') if File.directory?(path) if File.exist?(path) && !File.directory?(path) content = File.read(path) content_type = MiniMime.lookup_by_filename(path).content_type content_length = content.bytesize.to_s logger.info("Response size: #{content_length}") [200, { 'Content-Type' => content_type, 'Content-Length' => content_length }, [content]] else logger.info("Error File not found path: #{env['PATH_INFO']}") [404, { 'Content-Type' => 'text/plain' }, ["File not found: #{env['PATH_INFO']}"]] end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
cosensee-0.8.0 | lib/cosensee/web_server/static_file_handler.rb |