Skip to content

Commit

Permalink
add kemal examples
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 3, 2021
1 parent 8b7fa09 commit 11d706f
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 11 deletions.
7 changes: 7 additions & 0 deletions crystal/examples/http_client/http_request_get.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "http"

req = HTTP::Request.new("GET", "https://code-maven.com/page?name=Foo&[email protected]")
p! req
p req.resource
p req.query_params
p req.body
8 changes: 8 additions & 0 deletions crystal/examples/http_client/http_request_post.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "http"

req = HTTP::Request.new("POST",
"https://code-maven.com/page",
body: "name=Foo&[email protected]",
headers: HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded"})
p! req
p req.body # IO::Memory
21 changes: 21 additions & 0 deletions crystal/examples/kemal/spec/post_params_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ENV["KEMAL_ENV"] = "test"
require "spec-kemal"
require "../src/post_params"

describe "Web Application" do
it "renders /" do
get "/"
response.status_code.should eq 200
response.headers["Content-Type"].should eq "text/html"
response.body.should contain(%{<form method="POST" action="/">})
response.body.should_not contain(%{You typed in <b>})
end

it "renders / POST text=Foo Bar" do
post "/", body: "text=Foo Bar", headers: HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded"}
response.status_code.should eq 200
response.headers["Content-Type"].should eq "text/html"
response.body.should contain(%{<form method="POST" action="/">})
response.body.should contain(%{You typed in <b>Foo Bar</b>})
end
end
43 changes: 43 additions & 0 deletions crystal/examples/kemal/spec/route_params_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ENV["KEMAL_ENV"] = "test"
require "spec-kemal"
require "../src/route_params"

describe "Web Application" do
it "renders /" do
get "/"
response.status_code.should eq 200
response.headers["Content-Type"].should eq "text/html"
response.body.should contain(%{<a href="/user/foo">/user/foo</a><br>})
response.body.should_not contain(%{received})
end

it "renders /user/one" do
get "/user/one"
response.status_code.should eq 200
response.headers["Content-Type"].should eq "text/html"
response.body.should_not contain(%{<a})
response.body.should contain(%{received fname: one})
end

it "renders /user/one/two" do
get "/user/one/two"
response.status_code.should eq 200
response.headers["Content-Type"].should eq "text/html"
response.body.should_not contain(%{<a})
response.body.should contain(%{received fname: one and lname: two})
end

it "renders /user/one/two/three" do
get "/user/one/two/three"
response.status_code.should eq 200 # TODO should be 404
response.headers["Content-Type"].should eq "text/html"
response.body.should eq "" # TODO: where is the page?
end

it "renders /path/1/2/3/4/5" do
get "/path/1/2/3/4/5"
response.status_code.should eq 200
response.headers["Content-Type"].should eq "text/html"
response.body.should eq "received 1/2/3/4/5"
end
end
12 changes: 12 additions & 0 deletions crystal/examples/kemal/src/ecr_template.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "kemal"

get "/" do |env|
response = ""
text = env.params.query["text"]?
if !text.nil?
response = text
end
render "src/views/page.ecr", "src/views/layouts/layout.ecr"
end

Kemal.run
26 changes: 26 additions & 0 deletions crystal/examples/kemal/src/post_params.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "kemal"

get "/" do
form("")
end

post "/" do |env|
response = ""
text = env.params.body["text"]?
if !text.nil?
response = "You typed in <b>#{text}</b>"
end
form(response)
end

def form(response)
%{
<form method="POST" action="/">
<input name="text">
<input type="submit" value="Echo">
</form>
#{response}
}
end

Kemal.run
28 changes: 28 additions & 0 deletions crystal/examples/kemal/src/route_params.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "kemal"

get "/" do
%{
<a href="/user/foo">/user/foo</a><br>
<a href="/user/bar/bados">/user/bar/bados</a><br>
<a href="/user/a/b/c">/user/a/b/c</a><br>
<a href="/path/a/b/c/d">/path/a/b/c/d</a></br>
}
end

get "/user/:fname" do |env|
fname = env.params.url["fname"]
"received fname: #{fname}"
end

get "/user/:fname/:lname" do |env|
fname = env.params.url["fname"]
lname = env.params.url["lname"]
"received fname: #{fname} and lname: #{lname}"
end

get "/path/*all" do |env|
all = env.params.url["all"]
"received #{all}"
end

Kemal.run
11 changes: 11 additions & 0 deletions crystal/examples/kemal/src/views/layouts/layout.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">
<title><%= title %></title>
</head>
<body>
<%= content %>
</body>
</html>
8 changes: 8 additions & 0 deletions crystal/examples/kemal/src/views/page.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<form method="GET" action="/">
<input name="text">
<input type="submit" value="Echo">
</form>
<% if response %>
You typed in <b><%= response %></b>
<% end %>

8 changes: 8 additions & 0 deletions crystal/http_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@

![](examples/http_client/parse_url.cr)

## HTTP::Request GET
{id: http-request-get}

![](examples/http_client/http_request_get.cr)

## HTTP::Request POST
{id: http-request-post}

![](examples/http_client/http_request_post.cr)

4 changes: 3 additions & 1 deletion crystal/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
{id: crystal-language}

* [Crystal lang](https://crystal-lang.org/)
* Similar to Ruby
* Syntax similar to Ruby
* [LLVM](https://llvm.org/) under the hood
* Ideas from Go, Erlang, Rust, Swift
* Statically type checked
* Built-in type inference
* Types are non-nilable (compile time check for lack of assignment)
Expand Down
50 changes: 41 additions & 9 deletions crystal/kemal.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,66 @@ http://localhost:3000/
crystal spec/hello_world_spec.cr
```

## Kemal Autorestart
## Kemal Autorestart (autoreload)
{id: kemal-autorestart}

## Kemal GET parameters
{id: kemal-get-parameters}
{i: GET}

![](examples/kemal/src/get_params.cr)

![](examples/kemal/spec/get_params_spec.cr)

```
crystal spec/get_params_spec.cr
crystal spec/get_params_spec.cr
```

## Kemal POST parameters
{id: kemal-post-parameters}
{i: POST}

![](examples/kemal/src/post_params.cr)

![](examples/kemal/spec/post_params_spec.cr)

```
crystal spec/post_params_spec.cr
```


## Kemal Route parameters
{id: kemal-route-parameters}

![](examples/kemal/src/route_params.cr)

![](examples/kemal/spec/route_params_spec.cr)

```
crystal spec/post_params_spec.cr
```

## Kemal Any Route parameters
{id: kemal-any-route-parameters}

## Kemal Templates
{id: kemal-templates}
## Kemal ECR Templates
{id: kemal-ecr-templates}

## Kemal with Jinja templates
{id: kemal-jinja-templates}

## Kemal Elapsed time
{id: kemal-elapsed-time}

## Accept GET, POST, and route parameter in the same POST route
{id: kemal-get-post-route}

## Kemal indicate 404
{id: kemal-indicate-404}

## Kemal redirect
{id: kemal-redirect}

## Kemal in Docker
{id: kemal-in-docker}

* Templates
* Jinja templates
* GET
* POST
* Elapsed time
2 changes: 1 addition & 1 deletion crystal/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ All the elements of an array "inherit" the type from the array
{id: resources}

* [Crystal Programming](https://www.youtube.com/watch?v=DxFP-Wjqtsc) by Derek Banas from 2018
* [Crystal: Fast as C, Slick as Ruby](https://www.youtube.com/watch?v=m8bYqfrGjGo) by Tom Richards form 2017
* [Crystal: Fast as C, Slick as Ruby](https://www.youtube.com/watch?v=m8bYqfrGjGo) by [Tom Richards](https://github.com/t-richards) from [Delegator](https://www.delegator.com/) form 2017
* [LuckyCast](https://luckycasts.com/)

* [Crystal for Rubyists](https://www.crystalforrubyists.com/)
Expand Down

0 comments on commit 11d706f

Please sign in to comment.