2015-10-28 18:56:39 +00:00
|
|
|
<img src="https://avatars3.githubusercontent.com/u/15321198?v=3&s=200" width="100" height="100" />
|
2015-10-23 18:33:26 +00:00
|
|
|
# Kemal
|
2015-06-01 21:06:26 +00:00
|
|
|
|
2015-10-27 05:08:15 +00:00
|
|
|
Lightning Fast, Super Simple web framework for [Crystal](http://www.crystal-lang.org).
|
|
|
|
Inspired by [Sinatra](http://www.sinatrarb.com/)
|
2015-10-23 18:44:41 +00:00
|
|
|
|
2015-10-23 18:50:19 +00:00
|
|
|
Kemal is under heavy development and currently supports Crystal 0.9.0.
|
|
|
|
|
2015-10-23 19:43:33 +00:00
|
|
|
# Super Simple <3
|
|
|
|
|
|
|
|
```ruby
|
2015-10-24 13:48:47 +00:00
|
|
|
require "kemal"
|
|
|
|
|
2015-10-23 19:43:33 +00:00
|
|
|
get "/" do
|
|
|
|
"Hello World!"
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2015-10-24 16:20:49 +00:00
|
|
|
Build and run!
|
|
|
|
|
|
|
|
```
|
|
|
|
crystal build --release src/kemal_sample.cr
|
|
|
|
./kemal_sample
|
|
|
|
```
|
2015-10-23 19:43:33 +00:00
|
|
|
Go to *http://localhost:3000*
|
|
|
|
|
2015-10-29 09:20:32 +00:00
|
|
|
Check [samples](https://github.com/kemalcr/kemal/tree/master/samples) for more.
|
2015-10-24 13:50:26 +00:00
|
|
|
|
2015-10-23 18:50:19 +00:00
|
|
|
# Installation
|
|
|
|
|
|
|
|
Add it to your ```shard.yml```
|
|
|
|
|
|
|
|
```yml
|
|
|
|
dependencies:
|
|
|
|
kemal:
|
2015-10-26 11:28:23 +00:00
|
|
|
github: kemalcr/kemal
|
2015-10-23 18:50:19 +00:00
|
|
|
branch: master
|
|
|
|
```
|
2015-06-01 21:06:26 +00:00
|
|
|
|
2015-10-29 09:41:00 +00:00
|
|
|
## Routes
|
2015-06-01 21:06:26 +00:00
|
|
|
|
2015-10-29 09:41:00 +00:00
|
|
|
In Kemal, a route is an HTTP method paired with a URL-matching pattern. Each route is associated with a block:
|
|
|
|
|
|
|
|
```ruby
|
2015-10-29 09:42:54 +00:00
|
|
|
get "/" do
|
2015-10-29 09:41:00 +00:00
|
|
|
.. show something ..
|
|
|
|
end
|
|
|
|
|
2015-10-29 09:42:54 +00:00
|
|
|
post "/" do
|
2015-10-29 09:41:00 +00:00
|
|
|
.. create something ..
|
|
|
|
end
|
|
|
|
|
2015-10-29 09:42:54 +00:00
|
|
|
put "/" do
|
2015-10-29 09:41:00 +00:00
|
|
|
.. replace something ..
|
|
|
|
end
|
|
|
|
|
2015-10-29 09:42:54 +00:00
|
|
|
patch "/" do
|
2015-10-29 09:41:00 +00:00
|
|
|
.. modify something ..
|
|
|
|
end
|
|
|
|
|
2015-10-29 09:42:54 +00:00
|
|
|
delete "/" do
|
2015-10-29 09:41:00 +00:00
|
|
|
.. 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
|
2015-10-29 09:42:54 +00:00
|
|
|
get "/hello/:name" do |ctx|
|
2015-10-29 09:41:00 +00:00
|
|
|
name = ctx.params["name"]
|
|
|
|
"Hello back to #{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Matches /resize?width=200&height=200
|
2015-10-29 09:42:54 +00:00
|
|
|
get "/resize" do |ctx|
|
2015-10-29 09:41:00 +00:00
|
|
|
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
|
2015-10-29 09:42:54 +00:00
|
|
|
get "/user.json" do |ctx|
|
|
|
|
kemal = {name: "Kemal", language: "Crystal"}
|
2015-10-29 09:41:00 +00:00
|
|
|
ctx.set_content_type "application/json"
|
|
|
|
kemal.to_json
|
|
|
|
end
|
|
|
|
```
|
2015-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
## Thanks
|
|
|
|
|
|
|
|
Thanks to Manas for their awesome work on [Frank](https://github.com/manastech/frank).
|