Skip to content

Commit 8ff228e

Browse files
authored
[rails] Return plain json from route (#9573)
Return JSON from the routes is a lot faster: +------------+------------------------------+------+ | name| branch_name| json| +------------+------------------------------+------+ |rails-iodine| master|105369| |rails-iodine| rails/json-from-route|144006| +------------+------------------------------+------+
1 parent 6f8ba24 commit 8ff228e

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

frameworks/Ruby/rails/app/controllers/application_controller_metal.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

frameworks/Ruby/rails/app/controllers/json_controller.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
11
Rails.application.routes.draw do
22
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
33

4-
get "json", to: JsonController.action(:index)
4+
JsonApp = if defined?(Falcon) || defined?(Puma) || defined?(Agoo)
5+
->(env) do
6+
[200,
7+
{
8+
'Server' => 'Rails',
9+
'Content-Type' => 'application/json',
10+
'Date' => Time.now.httpdate,
11+
},
12+
[{ 'message' => 'Hello, World!' }.to_json]]
13+
end
14+
else
15+
->(env) do
16+
[200,
17+
{
18+
'Server' => 'Rails',
19+
'Content-Type' => 'application/json'
20+
},
21+
[{ 'message' => 'Hello, World!' }.to_json]]
22+
end
23+
end
24+
25+
PlaintextApp = if defined?(Falcon) || defined?(Puma) || defined?(Agoo)
26+
->(env) do
27+
[200,
28+
{
29+
'Server' => 'Rails',
30+
'Content-Type' => 'text/plain',
31+
'Date' => Time.now.httpdate
32+
},
33+
['Hello, World!']]
34+
end
35+
else
36+
->(env) do
37+
[200,
38+
{
39+
'Server' => 'Rails',
40+
'Content-Type' => 'text/plain'
41+
},
42+
['Hello, World!']]
43+
end
44+
end
45+
46+
get "json", to: JsonApp
547
get "db", to: "hello_world#db"
648
get "queries", to: "hello_world#query"
749
get "fortunes", to: "hello_world#fortune"
850
get "updates", to: "hello_world#update"
9-
get "plaintext", to: ->(env) do
10-
[200,
11-
{
12-
'Content-Type' => 'text/plain',
13-
'Date' => Time.now.httpdate,
14-
'Server' => 'Rails'
15-
},
16-
['Hello, World!']]
17-
end
51+
get "plaintext", to: PlaintextApp
1852
get "cached", to: "hello_world#cached_query"
1953
end

0 commit comments

Comments
 (0)