Skip to content

Commit

Permalink
Removed forward handler and moved the logic in the http router, added…
Browse files Browse the repository at this point in the history
… a helper function in the context
  • Loading branch information
grkek committed Sep 12, 2022
1 parent 5a37739 commit 237cfe8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 89 deletions.
48 changes: 43 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Add this to your application's `application.cr`:
```ruby
require "grip"


class IndexController < Grip::Controllers::Http
def get(context : Context) : Context
context
Expand Down Expand Up @@ -86,9 +85,33 @@ class ExceptionController < Grip::Controllers::Exception
end
end

class Swigger::Swagger < HTTP::Handler
def call(context : Context) : Context
context.html("<h1>Hello, World!</h1>")
class Swigger::Swagger
include HTTP::Handler

def call(context : HTTP::Server::Context) : HTTP::Server::Context
context
.html("<h1>Hello, World!</h1>")
.halt
end
end

class PoweredByGrip
include HTTP::Handler

def call(context : HTTP::Server::Context) : HTTP::Server::Context
context.put_resp_header("Server", "grip/#{Grip::VERSION}")

context
end
end

class Authorization
include HTTP::Handler

def call(context : HTTP::Server::Context) : HTTP::Server::Context
raise Grip::Exceptions::Unauthorized.new unless context.get_req_header?("Authorization")

context
end
end

Expand All @@ -97,18 +120,33 @@ class Application < Grip::Application
# By default the environment is set to "development" and serve_static is false.
super(environment, serve_static)

exceptions [Grip::Exceptions::Unauthorized, Grip::Exceptions::NotFound], ExceptionController
exception ArgumentError, ExceptionController

pipeline :api, [
Authorization.new
]

pipeline :docs, [
PoweredByGrip.new
]

scope "/api" do
get "/error", IndexController, as: :error

scope "/v1" do
pipe_through :api

get "/", IndexController
get "/:id", IndexController, as: :index
end
end

forward "/swagger/*", Swigger::Swagger
scope "/docs" do
pipe_through :docs

forward "/swagger", Swigger::Swagger
end

# Enable request/response logging.
router.insert(0, Grip::Handlers::Log.new)
Expand Down
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: 0.2.1
version: 0.3.0

authors:
- Grip and its Contributors <https://github.com/grip-framework/>
Expand Down
3 changes: 0 additions & 3 deletions src/grip/application.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module Grip
getter exception_handler : Grip::Handlers::Exception
getter pipeline_handler : Grip::Handlers::Pipeline
getter websocket_handler : Grip::Routers::WebSocket
getter forward_handler : Grip::Handlers::Forward
getter static_handler : Grip::Handlers::Static?

property router : Array(HTTP::Handler)
Expand All @@ -26,7 +25,6 @@ module Grip
@websocket_handler = Grip::Routers::WebSocket.new
@pipeline_handler = Grip::Handlers::Pipeline.new(@http_handler, @websocket_handler)
@exception_handler = Grip::Handlers::Exception.new(@environment)
@forward_handler = Grip::Handlers::Forward.new

if serve_static
@static_handler = Grip::Handlers::Static.new(pubilc_dir, fallthrough, directory_listing)
Expand All @@ -35,7 +33,6 @@ module Grip
@router = [
@exception_handler,
@pipeline_handler,
@forward_handler,
@websocket_handler,
@http_handler,
] of HTTP::Handler
Expand Down
7 changes: 6 additions & 1 deletion src/grip/extensions/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ module Grip
self
end

# Gets request header.
# Gets request header, raises if not found.
def get_req_header(key)
@request.headers[key]
end

# Gets request header, returns nil if not found.
def get_req_header?(key)
@request.headers[key]?
end

# Get cookie from request or nil if key does not exist
def get_req_cookie(key)
return @request.cookies[key] if @request.cookies[key]?
Expand Down
74 changes: 0 additions & 74 deletions src/grip/handlers/forward.cr

This file was deleted.

6 changes: 2 additions & 4 deletions src/grip/handlers/pipeline.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ module Grip
end

def match_via_http(context : HTTP::Server::Context) : Bool
route = @http_handler.find_route(
context.request.method.as(String),
context.request.path
)
route = @http_handler.find_route(context.request.method.as(String), context.request.path)
route = @http_handler.find_route("ALL", context.request.path) unless route.found?

unless route.found?
return false
Expand Down
2 changes: 1 addition & 1 deletion src/grip/macros/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module Grip
{% end %}

macro forward(route, resource, **kwargs)
@forward_handler.add_route("ALL", [@scopes.join(), {{route}}].join, {{resource}}.new({{**kwargs}}).as(HTTP::Handler), @valves.clone(), nil)
@http_handler.add_route("ALL", [@scopes.join(), {{route}}].join, {{resource}}.new({{**kwargs}}).as(HTTP::Handler), @valves.clone(), nil)
end

macro exception(exception, resource)
Expand Down
1 change: 1 addition & 0 deletions src/grip/routers/http.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Grip
return context if context.response.closed?

route = find_route(context.request.method.as(String), context.request.path)
route = find_route("ALL", context.request.path) unless route.found?

raise Exceptions::NotFound.new unless route.found?

Expand Down

0 comments on commit 237cfe8

Please sign in to comment.