Skip to content

Commit

Permalink
Update the exception handler, add static dsl macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi Kavrelishvili authored and Giorgi Kavrelishvili committed May 12, 2024
1 parent 51f1ad8 commit 43bc8ac
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: grip
version: 2.0.4
version: 3.0.0

authors:
- Grip and its Contributors <https://github.com/grip-framework/>
Expand Down
32 changes: 5 additions & 27 deletions src/grip/application.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
16 changes: 11 additions & 5 deletions src/grip/handlers/exception.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/grip/handlers/static.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions src/grip/macros/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 43bc8ac

Please sign in to comment.