mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Merge pull request #23 from ilatif/master
Adding method for doing browser redirection from inside any route.
This commit is contained in:
commit
af9c9ba3af
3 changed files with 27 additions and 0 deletions
11
README.md
11
README.md
|
@ -119,6 +119,17 @@ Accessing the environment (query params, body, content_type, headers, status_cod
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Browser Redirect
|
||||||
|
Just like other things in `kemal`, browser redirection is super simple as well. Use `environment` variable in defined route's corresponding block and call `redirect` on it.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
# Redirect browser
|
||||||
|
get "/logout" do |env|
|
||||||
|
# important stuff like clearing session etc.
|
||||||
|
env.redirect "/login" # redirect to /login page
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
## Middlewares
|
## Middlewares
|
||||||
|
|
||||||
You can create your own middlewares by inheriting from ```HTTP::Handler```
|
You can create your own middlewares by inheriting from ```HTTP::Handler```
|
||||||
|
|
|
@ -184,4 +184,15 @@ describe "Kemal::Handler" do
|
||||||
response.status_code.should eq(404)
|
response.status_code.should eq(404)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "redirects user to provided url" do
|
||||||
|
kemal = Kemal::Handler.new
|
||||||
|
kemal.add_route "GET", "/" do |env|
|
||||||
|
env.redirect("/login")
|
||||||
|
end
|
||||||
|
request = HTTP::Request.new("GET", "/")
|
||||||
|
response = kemal.call(request)
|
||||||
|
response.status_code.should eq(301)
|
||||||
|
response.headers.has_key?("Location").should eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,6 +22,11 @@ class Kemal::Context
|
||||||
@response.content_type
|
@response.content_type
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def redirect(url)
|
||||||
|
@response.headers.add "Location", url
|
||||||
|
@response.status_code = 301
|
||||||
|
end
|
||||||
|
|
||||||
delegate headers, @request
|
delegate headers, @request
|
||||||
delegate status_code, @response
|
delegate status_code, @response
|
||||||
delegate :"status_code=", @response
|
delegate :"status_code=", @response
|
||||||
|
|
Loading…
Reference in a new issue