Sha256: 69757c44d6cce54673012fc5366739dc79c29168bc1cce08e7d719ee8f76146e
Contents?: true
Size: 961 Bytes
Versions: 2
Compression:
Stored size: 961 Bytes
Contents
class Servel::Middleware def initialize(app, options = {}) @app = app @root = Pathname.new(options[:root]) @file_server = Rack::File.new(@root.to_s) end def call(env) path = env["PATH_INFO"] url_path = url_path_for(path) fs_path = @root + url_path[1..-1] return @file_server.call(env) unless fs_path.directory? url_path << "/" unless url_path.end_with?("/") return [302, { "Location" => url_path }, []] unless path == "" || path.end_with?("/") index(url_path, fs_path) end def url_path_for(url_path) url_path = Rack::Utils.unescape_path(url_path) raise unless Rack::Utils.valid_path?(url_path) Rack::Utils.clean_path_info(url_path) end def index(url_path, fs_path) @haml_context ||= Servel::HamlContext.new locals = Servel::Locals.new(url_path: url_path, fs_path: fs_path, root: @root).locals body = @haml_context.render('index.haml', locals) [200, {}, [body]] end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
servel-0.4.0 | lib/servel/middleware.rb |
servel-0.3.0 | lib/servel/middleware.rb |