/
redirect.cr
87 lines (72 loc) · 2.36 KB
/
redirect.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module Marten
module Handlers
# Handler allowing to conveniently return redirect responses.
class Redirect < Base
# Returns the configured route name that should be resolved to produce the URL to redirect to.
#
# Defaults to `nil`.
class_getter route_name : String?
# Returns the configured raw URL to redirect to.
#
# Defaults to `nil`.
class_getter url : String?
# Indicates whether query strings should be copied into the redirect URL or not.
#
# Defaults to `false`.
class_getter? forward_query_string : Bool = false
# Indicates whether the redirection is permanent or not.
#
# Defaults to `false`.
class_getter? permanent : Bool = false
# Allows to configure whether query strings should be copied into the redirect URL.
def self.forward_query_string(forward_query_string : Bool)
@@forward_query_string = forward_query_string
end
# Allows to configure whether the redirection is permanent or not.
def self.permanent(permanent : Bool)
@@permanent = permanent
end
# Allows to configure the route name that should be resolved to produce the URL to redirect to.
def self.route_name(route_name : String?)
@@route_name = route_name
end
# Allows to configure a raw URL to redirect to.
def self.url(url : String?)
@@url = url
end
def get
url = redirect_url
return HTTP::Response::Gone.new if url.nil?
redirect(url, permanent: self.class.permanent?)
end
def head
get
end
def post
get
end
def options
get
end
def delete
get
end
def put
get
end
def patch
get
end
# Returns the URL to redirect to.
#
# By default, the URL will be determined from the configured `#url` and `#route_name` values. This method can be
# overridden on subclasses in order to define any arbitrary logics that might be necessary in order to determine
# the final redirection URL.
def redirect_url
url = self.class.url || (self.class.route_name && reverse(self.class.route_name.not_nil!, params))
url = "#{url}?#{request.query_params.as_query}" if self.class.forward_query_string?
url
end
end
end
end