mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Added documentation
This commit is contained in:
parent
2cbe54b624
commit
d0ea2f049c
1 changed files with 54 additions and 2 deletions
56
README.md
56
README.md
|
@ -37,9 +37,61 @@ dependencies:
|
|||
branch: master
|
||||
```
|
||||
|
||||
## Status
|
||||
## Routes
|
||||
|
||||
Basic `get`, `put`, `post`, `patch`, `delete` and `head` routes can be matched, and request parameters can be obtained.
|
||||
In Kemal, a route is an HTTP method paired with a URL-matching pattern. Each route is associated with a block:
|
||||
|
||||
```ruby
|
||||
get '/' do
|
||||
.. show something ..
|
||||
end
|
||||
|
||||
post '/' do
|
||||
.. create something ..
|
||||
end
|
||||
|
||||
put '/' do
|
||||
.. replace something ..
|
||||
end
|
||||
|
||||
patch '/' do
|
||||
.. modify something ..
|
||||
end
|
||||
|
||||
delete '/' do
|
||||
.. annihilate something ..
|
||||
end
|
||||
```
|
||||
|
||||
## Context
|
||||
|
||||
Accessing the request context (query params, body, headers e.g) is super easy. You can use the context returned from the block:
|
||||
|
||||
```ruby
|
||||
# Matches /hello/kemal
|
||||
get '/hello/:name' do |ctx|
|
||||
name = ctx.params["name"]
|
||||
"Hello back to #{name}"
|
||||
end
|
||||
|
||||
# Matches /resize?width=200&height=200
|
||||
get '/resize' do |ctx|
|
||||
width = ctx.params["width"]
|
||||
height = ctx.params["height"]
|
||||
end
|
||||
```
|
||||
|
||||
## Content Type
|
||||
Kemal uses *text/html* as the default content type. You can change it via the context.
|
||||
|
||||
```ruby
|
||||
# Set the content as application/json and return JSON
|
||||
get '/user.json' do |ctx|
|
||||
kemal = {name: 'Kemal', language: 'Crystal'}
|
||||
ctx.set_content_type "application/json"
|
||||
kemal.to_json
|
||||
end
|
||||
```
|
||||
|
||||
## Thanks
|
||||
|
||||
|
|
Loading…
Reference in a new issue