Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting routes for serving static files #64

Merged
merged 4 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 11 additions & 4 deletions src/grip/handlers/static.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
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 = false, @directory_listing = false, @routing = "/")
@public_dir = Path.new(public_dir).expand
end

def call(context : HTTP::Server::Context)
Expand All @@ -28,9 +28,14 @@ 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)
relative_path = request_path.lchop?(routing) || begin
call_next(context)
expanded_path
end

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")
file_path = File.join(@public_dir, Path[relative_path])
root_file = File.join(@public_dir, Path[relative_path], "index.html")

if is_dir_path && File.exists? root_file
return if etag(context, root_file)
Expand Down Expand Up @@ -103,6 +108,8 @@ module Grip
def self.config_gzip?(config)
config.is_a?(Hash) && config["gzip"] == true
end

getter routing : String
end
end
end
Loading