Skip to content

Commit 0a3c69e

Browse files
Giorgi KavrelishviliGiorgi Kavrelishvili
authored andcommitted
Implement a better approach to the router
1 parent c5544e3 commit 0a3c69e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+968
-793
lines changed

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ This project exists due to the fact that Kemal lacks one crucial part of a frame
4343

4444
Add this to your application's `application.cr`:
4545

46-
```ruby
46+
```crystal
4747
require "grip"
4848
49-
class IndexController < Grip::Controllers::Http
49+
class IndexController
50+
include Grip::Controllers::HTTP
51+
5052
def get(context : Context) : Context
5153
context
52-
.put_status(200) # Assign the status code to 200 OK.
54+
.put_status(200) # Assign the status code as 200 OK.
5355
.json({"id" => 1}) # Respond with JSON content.
5456
.halt # Close the connection.
5557
end
@@ -68,23 +70,30 @@ class IndexController < Grip::Controllers::Http
6870
end
6971
7072
class Application < Grip::Application
71-
def initialize(environment : String)
73+
def initialize
7274
# By default the environment is set to "development".
73-
super(environment)
75+
super(
76+
environment: ENV["ENVIRONMENT"]? || "production"
77+
handlers: [
78+
Grip::Handlers::Log.new,
79+
Grip::Handlers::HTTP.new
80+
] of HTTP::Handler
81+
)
82+
83+
routes()
84+
end
7485
86+
def routes()
7587
scope "/api" do
7688
scope "/v1" do
7789
get "/", IndexController
7890
get "/:id", IndexController, as: :index
7991
end
8092
end
81-
82-
# Enable request/response logging.
83-
router.insert(0, Grip::Handlers::Log.new)
8493
end
8594
end
8695
87-
app = Application.new(environment: "development")
96+
app = Application.new
8897
app.run
8998
```
9099

@@ -107,7 +116,7 @@ shards install
107116
## API Reference
108117

109118
Documentation can be found on the [official website of the Grip framework](https://grip-framework.github.io/docs/) or
110-
the [CrystalDoc website](https://crystaldoc.info/github/grip-framework/grip/v2.0.3/index.html).
119+
the [CrystalDoc website](https://crystaldoc.info/github/grip-framework/grip/v4.0.0/index.html).
111120

112121
## Contribute
113122

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: grip
2-
version: 3.0.2
2+
version: 4.0.0
33

44
authors:
55
- Grip and its Contributors <https://github.com/grip-framework/>

spec/context_spec.cr

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require "./spec_helper"
33
describe "Context" do
44
context "headers" do
55
it "sets content type" do
6-
http_handler = Grip::Routers::Http.new
6+
http_handler = Grip::Handlers::HTTP.new
77
http_handler.add_route "GET", "/content_type", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
88
context.response.headers.merge!({"Content-Type" => "application/json"})
99
context
@@ -15,7 +15,7 @@ describe "Context" do
1515
end
1616

1717
it "parses headers" do
18-
http_handler = Grip::Routers::Http.new
18+
http_handler = Grip::Handlers::HTTP.new
1919
http_handler.add_route "GET", "/headers", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
2020
name = context.request.headers["name"]
2121
context.response.print("Hello #{name}")
@@ -30,7 +30,7 @@ describe "Context" do
3030
end
3131

3232
it "sets response headers" do
33-
http_handler = Grip::Routers::Http.new
33+
http_handler = Grip::Handlers::HTTP.new
3434
http_handler.add_route "GET", "/response_headers", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
3535
context.response.headers.add "Accept-Language", "ge"
3636
context
@@ -45,7 +45,7 @@ describe "Context" do
4545
context "methods" do
4646
it "sends a local file" do
4747
file_path = File.join(__DIR__, "./fixtures/index.html")
48-
http_handler = Grip::Routers::Http.new
48+
http_handler = Grip::Handlers::HTTP.new
4949
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
5050
context.send_file(file_path).halt
5151
end
@@ -57,9 +57,9 @@ describe "Context" do
5757
end
5858

5959
it "has binary() method with octet-stream" do
60-
http_handler = Grip::Routers::Http.new
60+
http_handler = Grip::Handlers::HTTP.new
6161
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
62-
context.binary(10).halt
62+
context.binary("10").halt
6363
end
6464

6565
request = HTTP::Request.new("GET", "/")
@@ -69,7 +69,7 @@ describe "Context" do
6969
end
7070

7171
it "encodes text in utf-8" do
72-
http_handler = Grip::Routers::Http.new
72+
http_handler = Grip::Handlers::HTTP.new
7373
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
7474
context.text("👋🏼 grip").halt
7575
end
@@ -81,7 +81,7 @@ describe "Context" do
8181
end
8282

8383
it "encodes html in utf-8" do
84-
http_handler = Grip::Routers::Http.new
84+
http_handler = Grip::Handlers::HTTP.new
8585
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
8686
context.html("👋🏼 grip").halt
8787
end
@@ -95,7 +95,7 @@ describe "Context" do
9595

9696
context "methods" do
9797
it "allows overriding text() content type" do
98-
http_handler = Grip::Routers::Http.new
98+
http_handler = Grip::Handlers::HTTP.new
9999
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
100100
context.text("👋🏼 grip", "text/html").halt
101101
end
@@ -106,7 +106,7 @@ describe "Context" do
106106
end
107107

108108
it "allows overriding json() content type" do
109-
http_handler = Grip::Routers::Http.new
109+
http_handler = Grip::Handlers::HTTP.new
110110
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
111111
context.json({:message => "👋🏼 grip"}, "application/json").halt
112112
end
@@ -117,7 +117,7 @@ describe "Context" do
117117
end
118118

119119
it "allows overriding html() content type" do
120-
http_handler = Grip::Routers::Http.new
120+
http_handler = Grip::Handlers::HTTP.new
121121
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
122122
context.html("👋🏼 grip", "text/html").halt
123123
end
@@ -128,9 +128,9 @@ describe "Context" do
128128
end
129129

130130
it "allows overriding binary() content type" do
131-
http_handler = Grip::Routers::Http.new
131+
http_handler = Grip::Handlers::HTTP.new
132132
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
133-
context.binary(10, "multipart/encrypted").halt
133+
context.binary("10", "multipart/encrypted").halt
134134
end
135135

136136
request = HTTP::Request.new("GET", "/")
@@ -141,7 +141,7 @@ describe "Context" do
141141

142142
context "cookies" do
143143
it "sets cookie" do
144-
http_handler = Grip::Routers::Http.new
144+
http_handler = Grip::Handlers::HTTP.new
145145
http_handler.add_route "GET", "/cookies", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
146146
context.put_resp_cookie(HTTP::Cookie.new("Foo", "Bar")).halt
147147
end
@@ -154,7 +154,7 @@ describe "Context" do
154154
end
155155

156156
it "sets string cookie" do
157-
http_handler = Grip::Routers::Http.new
157+
http_handler = Grip::Handlers::HTTP.new
158158
http_handler.add_route "GET", "/cookies", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
159159
context.put_resp_cookie("Foo", "Bar").halt
160160
end
@@ -167,7 +167,7 @@ describe "Context" do
167167
end
168168

169169
it "sets multiple cookie" do
170-
http_handler = Grip::Routers::Http.new
170+
http_handler = Grip::Handlers::HTTP.new
171171
http_handler.add_route "GET", "/cookies", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
172172
context.put_resp_cookie("Foo", "Bar").put_resp_cookie("Qux", "Baz").halt
173173
end
@@ -181,7 +181,7 @@ describe "Context" do
181181
end
182182

183183
it "overrides cookie" do
184-
http_handler = Grip::Routers::Http.new
184+
http_handler = Grip::Handlers::HTTP.new
185185
http_handler.add_route "GET", "/cookies", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
186186
context.put_resp_cookie("Foo", "Bar").put_resp_cookie("Foo", "Baz").halt
187187
end
@@ -194,7 +194,7 @@ describe "Context" do
194194
end
195195

196196
it "gets correkt cookie" do
197-
http_handler = Grip::Routers::Http.new
197+
http_handler = Grip::Handlers::HTTP.new
198198
http_handler.add_route "GET", "/get_cookie", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
199199
cookie = context.get_req_cookie("Foo")
200200
if cookie
@@ -213,7 +213,7 @@ describe "Context" do
213213
end
214214

215215
it "gets nil for not existing cookie" do
216-
http_handler = Grip::Routers::Http.new
216+
http_handler = Grip::Handlers::HTTP.new
217217
http_handler.add_route "GET", "/get_cookie", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
218218
cookie = context.get_req_cookie("Baz")
219219
if cookie
@@ -235,7 +235,7 @@ describe "Context" do
235235

236236
context "redirects" do
237237
it "redirects to home page when called without arguments" do
238-
http_handler = Grip::Routers::Http.new
238+
http_handler = Grip::Handlers::HTTP.new
239239
http_handler.add_route "GET", "/redirect_to_home", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
240240
context.redirect
241241
end
@@ -248,7 +248,7 @@ describe "Context" do
248248
end
249249

250250
it "redirects to another url with status code 301 using keyword arguments" do
251-
http_handler = Grip::Routers::Http.new
251+
http_handler = Grip::Handlers::HTTP.new
252252
http_handler.add_route "GET", "/redirect_to_another_url", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
253253
context.redirect(url: "/another_url", status_code: 301)
254254
end
@@ -261,7 +261,7 @@ describe "Context" do
261261
end
262262

263263
it "redirects to another url with status code 308 using positional arguments" do
264-
http_handler = Grip::Routers::Http.new
264+
http_handler = Grip::Handlers::HTTP.new
265265
http_handler.add_route "GET", "/redirect_to_another_url_with_308", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
266266
context.redirect("/another_url", HTTP::Status::PERMANENT_REDIRECT)
267267
end
File renamed without changes.

spec/exception_handler_spec.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ describe "Grip::Handlers::Exception" do
66
io = IO::Memory.new
77
response = HTTP::Server::Response.new(io)
88
context = HTTP::Server::Context.new(request, response)
9-
Grip::Handlers::Exception.new(environment: "test").call(context)
9+
Grip::Handlers::Exception.new.call(context)
1010
response.close
1111
io.rewind
1212
response = HTTP::Client::Response.from_io(io, decompress: false)
1313
response.status_code.should eq 404
1414
end
1515

1616
it "renders custom error" do
17-
error_handler = Grip::Handlers::Exception.new(environment: "test")
17+
error_handler = Grip::Handlers::Exception.new
1818
error_handler.handlers[Exceptions::Forbidden.name] = ForbiddenController.new
19-
http_handler = Grip::Routers::Http.new
19+
http_handler = Grip::Handlers::HTTP.new
2020

2121
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
2222
raise Exceptions::Forbidden.new

spec/http_router_spec.cr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require "./spec_helper"
22

3-
describe "Grip::Routers::Http" do
3+
describe "Grip::Handlers::HTTP" do
44
it "routes" do
5-
http_handler = Grip::Routers::Http.new
5+
http_handler = Grip::Handlers::HTTP.new
66
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
77
context.response.print("hello")
88
context
@@ -16,7 +16,7 @@ describe "Grip::Routers::Http" do
1616
it "routes with long response body" do
1717
long_response_body = "string" * 10_000
1818

19-
http_handler = Grip::Routers::Http.new
19+
http_handler = Grip::Handlers::HTTP.new
2020
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
2121
context.response.print(long_response_body)
2222
context
@@ -28,7 +28,7 @@ describe "Grip::Routers::Http" do
2828
end
2929

3030
it "routes request with query string" do
31-
http_handler = Grip::Routers::Http.new
31+
http_handler = Grip::Handlers::HTTP.new
3232
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
3333
context.response.print("hello #{context.fetch_query_params.["message"]}")
3434
context
@@ -39,7 +39,7 @@ describe "Grip::Routers::Http" do
3939
end
4040

4141
it "routes request with multiple query strings" do
42-
http_handler = Grip::Routers::Http.new
42+
http_handler = Grip::Handlers::HTTP.new
4343
http_handler.add_route "GET", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
4444
context.response.print("hello #{context.fetch_query_params.["message"]} time #{context.fetch_query_params.["time"]}")
4545
context
@@ -51,7 +51,7 @@ describe "Grip::Routers::Http" do
5151
end
5252

5353
it "route parameter has more precedence than query string arguments" do
54-
http_handler = Grip::Routers::Http.new
54+
http_handler = Grip::Handlers::HTTP.new
5555
http_handler.add_route "GET", "/:message", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
5656
context.response.print("hello #{context.fetch_path_params.["message"]}")
5757
context
@@ -62,7 +62,7 @@ describe "Grip::Routers::Http" do
6262
end
6363

6464
it "parses simple JSON body" do
65-
http_handler = Grip::Routers::Http.new
65+
http_handler = Grip::Handlers::HTTP.new
6666
http_handler.add_route "POST", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
6767
name = context.fetch_json_params.["name"]
6868
age = context.fetch_json_params.["age"]
@@ -82,7 +82,7 @@ describe "Grip::Routers::Http" do
8282
end
8383

8484
it "parses JSON with string array" do
85-
http_handler = Grip::Routers::Http.new
85+
http_handler = Grip::Handlers::HTTP.new
8686
http_handler.add_route "POST", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
8787
skills = context.fetch_json_params.["skills"].as(Array)
8888
context.response.print("Skills #{skills.each.join(',')}")
@@ -101,7 +101,7 @@ describe "Grip::Routers::Http" do
101101
end
102102

103103
it "parses JSON with json object array" do
104-
http_handler = Grip::Routers::Http.new
104+
http_handler = Grip::Handlers::HTTP.new
105105
http_handler.add_route "POST", "/", ExampleController.new, [:none], ->(context : HTTP::Server::Context) do
106106
skills = context.fetch_json_params.["skills"].as(Array)
107107
skills_from_languages = skills.map(&.["language"])

spec/param_parser_spec.cr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ describe "ParameterBox" do
1414
end
1515

1616
it "parses url params" do
17-
grip = Grip::Routers::Http.new
18-
grip.add_route "POST", "/hello/:name", ExampleController.new, [] of Symbol, nil
17+
http_handler = Grip::Handlers::HTTP.new
18+
http_handler.add_route "POST", "/hello/:name", ExampleController.new, [] of Symbol, nil
1919
request = HTTP::Request.new("POST", "/hello/crystal")
20-
_context = create_request_and_return_io_and_context(grip, request)[1]
21-
url_params = Grip::Parsers::ParameterBox.new(request, grip.find_route(request.method, request.path).params).url
20+
_context = create_request_and_return_io_and_context(http_handler, request)[1]
21+
url_params = Grip::Parsers::ParameterBox.new(request, http_handler.find_route(request.method, request.path).params).url
2222
url_params["name"].should eq "crystal"
2323
end
2424

2525
it "decodes url params" do
26-
grip = Grip::Routers::Http.new
27-
grip.add_route "POST", "/hello/:email/:money/:spanish", ExampleController.new, [] of Symbol, nil
26+
http_handler = Grip::Handlers::HTTP.new
27+
http_handler.add_route "POST", "/hello/:email/:money/:spanish", ExampleController.new, [] of Symbol, nil
2828
request = HTTP::Request.new("POST", "/hello/sam%2Bspec%40gmail.com/%2419.99/a%C3%B1o")
29-
_context = create_request_and_return_io_and_context(grip, request)[1]
30-
url_params = Grip::Parsers::ParameterBox.new(request, grip.find_route(request.method, request.path).params).url
29+
_context = create_request_and_return_io_and_context(http_handler, request)[1]
30+
url_params = Grip::Parsers::ParameterBox.new(request, http_handler.find_route(request.method, request.path).params).url
3131
url_params["email"].should eq "sam+spec@gmail.com"
3232
url_params["money"].should eq "$19.99"
3333
url_params["spanish"].should eq "año"

0 commit comments

Comments
 (0)