Sha256: 367eaa5fc8b1f99fd94d9ee47c379b99fd8bb968eff1f52176d902d39d1c52cd

Contents?: true

Size: 967 Bytes

Versions: 3

Compression:

Stored size: 967 Bytes

Contents

class Servel::App
  def initialize(root)
    @root = Pathname.new(root)
    @file_server = Rack::File.new(root.to_s)
  end

  def call(env)
    url_root = env["SCRIPT_NAME"]
    url_path = clean_url_path(env["PATH_INFO"])

    return redirect("#{url_root}/") if env["PATH_INFO"] == ""

    fs_path = @root + url_path[1..-1]

    return @file_server.call(env) if fs_path.file?

    return redirect("#{url_root}#{url_path}/") unless env["PATH_INFO"].end_with?("/")

    index(Servel::Locals.new(url_root: url_root, url_path: url_path, fs_path: fs_path))
  end

  def redirect(location)
    [302, { "Location" => location }, []]
  end

  def clean_url_path(path)
    url_path = Rack::Utils.unescape_path(path)
    raise unless Rack::Utils.valid_path?(url_path)
    Rack::Utils.clean_path_info(url_path)
  end

  def index(locals)
    @haml_context ||= Servel::HamlContext.new
    body = @haml_context.render('index.haml', locals.resolve)

    [200, {}, [body]]
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
servel-0.7.0 lib/servel/app.rb
servel-0.6.0 lib/servel/app.rb
servel-0.5.0 lib/servel/app.rb