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 11d706f commit 4f3be0a
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crystal/.ameba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ Lint/UselessAssign:
Excluded:
- examples/files/append_to_file_bug.cr
- examples/functions/implicit.cr
- examples/kemal/src/elapsed_time.cr
- examples/kemal/src/ecr_template.cr
Excluded:
- examples/kemal/lib/
Lint/UnusedArgument:
Excluded:
- examples/kemal/src/elapsed_time.cr
34 changes: 34 additions & 0 deletions crystal/examples/kemal/spec/send_404_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ENV["KEMAL_ENV"] = "test"
require "spec-kemal"
require "../src/send_404"

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/foo" do
get "/user/foo"
response.status_code.should eq 200
response.headers["Content-Type"].should eq "text/html"
response.body.should contain(%{received fname: foo})
end

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

it "renders /user/other" do
get "/user/other"
response.status_code.should eq 404
response.headers["Content-Type"].should eq "text/html"
response.body.should contain(%{We don't have this user <b>other</b>})
end
end
19 changes: 19 additions & 0 deletions crystal/examples/kemal/spec/set_header_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ENV["KEMAL_ENV"] = "test"
require "spec-kemal"
require "../src/set_header"

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(%{Hello Kemal})
end

it "renders /sitemap.xml" do
get "/sitemap.xml"
response.status_code.should eq 200
response.headers["Content-Type"].should eq "application/xml"
response.body.should contain(%{<?xml version="1.0" encoding="UTF-8"?>})
end
end
18 changes: 18 additions & 0 deletions crystal/examples/kemal/src/elapsed_time.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "kemal"

class HTTP::Server::Context
getter start_time : Time::Span = Time.monotonic
end

get "/" do |env|
# spend some time
x = 0
loop do
x += 1
break if x > 10_000_000
end
text = "This is some text"
render "src/views/time.ecr", "src/views/layouts/layout_with_elapsed_time.ecr"
end

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

get "/" do
%{
<a href="/user/foo">/user/foo</a><br>
<a href="/user/bar">/user/bar</a><br>
<a href="/user/zorg">/user/zorg</a><br>
}
end

DATABASE = Set{"foo", "bar"}

get "/user/:fname" do |env|
fname = env.params.url["fname"]
if ! DATABASE.includes?(fname)
halt env, status_code: 404, response: "We don't have this user <b>#{fname}</b>"
end
"received fname: #{fname}"
end



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

base_url = "https://code-maven.com"

get "/" do
"Hello Kemal"
end

# source
get "/sitemap.xml" do |env|
now = Time.utc
now_str = now.to_s("%Y-%m-%d")
env.response.content_type = "application/xml"
xml = %{
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>#{base_url}/</loc>
<lastmod>#{now_str}</lastmod>
</url>
</urlset>
}
xml
end

Kemal.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!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>
</head>
<body>
<%= content %>
<hr>
<div>
Elapsed time <%= (Time.monotonic - env.start_time).total_milliseconds %> ms
</div>
</body>
</html>
2 changes: 2 additions & 0 deletions crystal/examples/kemal/src/views/time.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<%= text %>
22 changes: 22 additions & 0 deletions crystal/kemal.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,40 @@ crystal spec/post_params_spec.cr
## Kemal ECR Templates
{id: kemal-ecr-templates}

![](examples/kemal/src/ecr_template.cr)
![](examples/kemal/src/views/page.ecr)
![](examples/kemal/src/views/layouts/layout.ecr)

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

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

![](examples/kemal/src/elapsed_time.cr)
![](examples/kemal/src/views/layouts/layout_with_elapsed_time.ecr)
![](examples/kemal/src/views/time.ecr)

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

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

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

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

## Kemal Styling 404 pages
{id: kemal-styling-404-pages}


## Kemal set headers (change content-type)
{id: kemal-set-header}

![](examples/kemal/src/set_header.cr)
![](examples/kemal/spec/set_header_spec.cr)

## Kemal redirect
{id: kemal-redirect}

Expand Down

0 comments on commit 4f3be0a

Please sign in to comment.