From 43bc8ac60705129515d1507f5458f0151e76b318 Mon Sep 17 00:00:00 2001 From: Giorgi Kavrelishvili Date: Sun, 12 May 2024 10:44:48 +0400 Subject: [PATCH] Update the exception handler, add static dsl macro --- shard.yml | 2 +- src/grip/application.cr | 32 +++++--------------------------- src/grip/handlers/exception.cr | 16 +++++++++++----- src/grip/handlers/static.cr | 2 +- src/grip/macros/dsl.cr | 16 ++++++++++++++++ 5 files changed, 34 insertions(+), 34 deletions(-) diff --git a/shard.yml b/shard.yml index 9363571..f2b7b47 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: grip -version: 2.0.4 +version: 3.0.0 authors: - Grip and its Contributors diff --git a/src/grip/application.cr b/src/grip/application.cr index a824391..9ccc04c 100644 --- a/src/grip/application.cr +++ b/src/grip/application.cr @@ -5,31 +5,25 @@ module Grip include Grip::Macros::Dsl getter environment : String = "development" - getter serve_static : Bool = false getter http_handler : Grip::Routers::Http getter exception_handler : Grip::Handlers::Exception getter pipeline_handler : Grip::Handlers::Pipeline getter websocket_handler : Grip::Routers::WebSocket - getter static_handler : Array(Grip::Handlers::Static) = [] of Grip::Handlers::Static - - property router : Array(HTTP::Handler) + getter static_handlers : Array(Grip::Handlers::Static) = [] of Grip::Handlers::Static getter scopes : Array(String) = [] of String getter valves : Array(Symbol) = [] of Symbol - getter valve : Symbol? + + property router : Array(HTTP::Handler) - def initialize(@environment : String = "development", @serve_static : Bool = false) + def initialize(@environment : String = "development") @http_handler = Grip::Routers::Http.new @websocket_handler = Grip::Routers::WebSocket.new @pipeline_handler = Grip::Handlers::Pipeline.new(@http_handler, @websocket_handler) @exception_handler = Grip::Handlers::Exception.new(@environment) - serve_static && static_routes.each do |route, path| - @static_handler << Grip::Handlers::Static.new(path, fallthrough, directory_listing, route) - end - @router = [ @exception_handler, @pipeline_handler, @@ -50,24 +44,8 @@ module Grip false end - def pubilc_dir : String - "./public" - end - - def static_routes : Hash(String, String) - {"/" => pubilc_dir} - end - - def fallthrough : Bool - false - end - - def directory_listing : Bool - false - end - def server : HTTP::Server - serve_static && @static_handler.each do |handler| + @static_handlers.each do |handler| @router.insert(1, handler) end diff --git a/src/grip/handlers/exception.cr b/src/grip/handlers/exception.cr index bbdf5d8..5c1ea32 100644 --- a/src/grip/handlers/exception.cr +++ b/src/grip/handlers/exception.cr @@ -15,10 +15,7 @@ module Grip rescue ex return context if context.response.closed? - context.response.status_code = 500 if !context.response.status_code.in?([400, 401, 403, 404, 405, 500]) - if ex.is_a?(Grip::Exceptions::Base) - context.response.status_code = ex.status_code.value call_exception(context, ex, ex.status_code.value) else call_exception(context, ex, context.response.status_code) @@ -27,20 +24,29 @@ module Grip private def call_exception(context : HTTP::Server::Context, exception : ::Exception, status_code : Int32) return context if context.response.closed? + if @handlers.has_key?(exception.class.name) context.response.status_code = status_code context.exception = exception + context.response.close @handlers[exception.class.name].call(context) else + if status_code.in?(400..599) + context.response.status_code = status_code + else + context.response.status_code = 500 + end + + context.response.headers.merge!({"Content-Type" => "text/html; charset=UTF-8"}) + if @environment == "production" - context.response.headers.merge!({"Content-Type" => "text/html; charset=UTF-8"}) context.response.print("An error occured, please try again later.") else - context.response.headers.merge!({"Content-Type" => "text/html; charset=UTF-8"}) context.response.print(Grip::Minuscule::ExceptionPage.for_runtime_exception(context, exception).to_s) end + context.response.close context end end diff --git a/src/grip/handlers/static.cr b/src/grip/handlers/static.cr index 6cc5277..28df93e 100644 --- a/src/grip/handlers/static.cr +++ b/src/grip/handlers/static.cr @@ -78,7 +78,7 @@ module Grip if Dir.exists?(file_path) if config.is_a?(Hash) && config["dir_listing"] == true - context.response.content_type = "text/html" + context.response.content_type = "text/html;charset=UTF-8;" directory_listing(context.response, request_path, file_path) else call_next(context) diff --git a/src/grip/macros/dsl.cr b/src/grip/macros/dsl.cr index 5450731..5b574a7 100644 --- a/src/grip/macros/dsl.cr +++ b/src/grip/macros/dsl.cr @@ -43,6 +43,22 @@ module Grip end {% end %} + macro static(source, destination, **kwargs) + {% if kwargs[:fallthrough] %} + {% fallthrough = kwargs[:fallthrough] %} + {% else %} + {% fallthrough = false %} + {% end %} + + {% if kwargs[:directory_listing] %} + {% directory_listing = kwargs[:directory_listing] %} + {% else %} + {% directory_listing = false %} + {% end %} + + @static_handlers.push(Grip::Handlers::Static.new({{destination}}, {{fallthrough}}, {{directory_listing}}, {{source}})) + end + macro forward(route, resource, **kwargs) @http_handler.add_route("ALL", [@scopes.join(), {{route}}].join, {{resource}}.new({{**kwargs}}).as(HTTP::Handler), @valves.clone(), nil) end