Skip to content

Commit

Permalink
Merge pull request #64 from yanecc/core
Browse files Browse the repository at this point in the history
Support setting routes for serving static files
  • Loading branch information
grkek authored May 12, 2024
2 parents 8dd4cc3 + bf23ab5 commit 51f1ad8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 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
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

0 comments on commit 51f1ad8

Please sign in to comment.