Skip to content

Commit

Permalink
Support setting routes for serving static files
Browse files Browse the repository at this point in the history
  • Loading branch information
yanecc committed May 2, 2024
1 parent 7dbfcde commit 8aa4099
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/grip/application.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Grip
getter exception_handler : Grip::Handlers::Exception
getter pipeline_handler : Grip::Handlers::Pipeline
getter websocket_handler : Grip::Routers::WebSocket
getter static_handler : Grip::Handlers::Static?
getter static_handler : Array(Grip::Handlers::Static) = [] of Grip::Handlers::Static

property router : Array(HTTP::Handler)

Expand All @@ -26,8 +26,8 @@ module Grip
@pipeline_handler = Grip::Handlers::Pipeline.new(@http_handler, @websocket_handler)
@exception_handler = Grip::Handlers::Exception.new(@environment)

if serve_static
@static_handler = Grip::Handlers::Static.new(pubilc_dir, fallthrough, directory_listing)
serve_static && static_routes.each do |route, path|
@static_handler << Grip::Handlers::Static.new(path, fallthrough, directory_listing, route)
end

@router = [
Expand All @@ -54,6 +54,10 @@ module Grip
"./public"
end

def static_routes : Hash(String, String)
{"/" => pubilc_dir}
end

def fallthrough : Bool
false
end
Expand All @@ -63,8 +67,8 @@ module Grip
end

def server : HTTP::Server
if serve_static
@router.insert(1, @static_handler.not_nil!)
serve_static && @static_handler.each do |handler|
@router.insert(1, handler)
end

HTTP::Server.new(@router)
Expand Down
25 changes: 18 additions & 7 deletions src/grip/handlers/static.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
module Grip
module Handlers
class Static < HTTP::StaticFileHandler
def initialize(public_dir : String, fallthrough = false, directory_listing = false)
super
def initialize(public_dir : String, @fallthrough = true, @directory_listing = true, @routing = "/")
@public_dir = Path.new(public_dir).expand
end

def call(context : HTTP::Server::Context)
return allow_get_or_head(context) unless method_get_or_head?(context.request.method)
# 1. / -> ./public
# 2. /foo/bar -> ./public
# 3. /foo/bar/ -> ./public

original_path = context.request.path.not_nil!
original_path = context.request.path.not_nil! # / /foo/bar /foo/bar/
request_path = URI.decode(original_path)

# File path cannot contain '\0' (NUL) because all filesystem I know
Expand All @@ -28,20 +31,26 @@ module Grip
is_dir_path = dir_path? original_path
expanded_path = Path.posix(request_path).expand("/").to_s
expanded_path += "/" if is_dir_path && !dir_path?(expanded_path)
is_dir_path = dir_path? expanded_path
file_path = File.join(@public_dir, expanded_path)
root_file = File.join(@public_dir, expanded_path, "index.html")
relative_path = request_path.lchop(routing) # "" "" /

is_dir_path = dir_path? expanded_path # false false true
file_path = File.join([@public_dir] + relative_path.split("/")) # ./public ./public ./public
root_file = File.join([@public_dir] + relative_path.split("/") + ["index.html"]) # ./public/index.html ./public/index.html ./public/index.html

if is_dir_path && File.exists? root_file
return if etag(context, root_file)
return context.send_file(root_file, gzip_enabled: self.class.config_gzip?(static_config))
end

is_dir_path = Dir.exists?(file_path) && !is_dir_path
is_dir_path = Dir.exists?(file_path) && !is_dir_path # true
if request_path != expanded_path || is_dir_path
redirect_to context, file_redirect_path(expanded_path, is_dir_path)
end

# private def redirect_to(context, url)
# context.response.redirect url.to_s
# end

call_next_with_file_path(context, request_path, file_path)
end

Expand Down Expand Up @@ -103,6 +112,8 @@ module Grip
def self.config_gzip?(config)
config.is_a?(Hash) && config["gzip"] == true
end

getter routing : String
end
end
end

0 comments on commit 8aa4099

Please sign in to comment.