Coming from Kemal
Kemal and Runcobo are both Crystal frameworks built on HTTP::Server handlers, and both use radix for routing, so requests flow much the same way. The difference is structure: Kemal routes are blocks, while each Runcobo route is a class with a typed contract, its own filters and a compiled template. Runcobo also brings a database layer and a CLI.
At a glance
| Kemal | Runcobo |
|---|---|
get "/posts/:id" do |env| ... end | class Posts::ShowPost < BaseAction with get "/posts/:id" and call do ... end |
env.params.url["id"] | validate(ShowPostContract).id — already an Int64 |
env.params.query["page"] | A contract field, read from the query string |
env.params.json["name"].as(String) | A contract field, read from the JSON body — typed, no casts |
before_all / before_get "/admin/*" | before filters on a base action |
halt env, status_code: 401 | render_json(..., status_code: 401) then return byebye |
env.json(data) | render_json(data) or a .jbuilder template |
render "src/views/post.ecr" | render → a Water or Jbuilder template beside the action |
add_handler MyHandler.new | The handlers: array of Runcobo.start (Server) |
Kemal.run | Runcobo.start |
env.set / env.get | Typed properties on the context (dependency injection) |
| Your own database code | Generated, typed queries (Database Access) |
| Running with a file watcher | bin/cobo dev |
A route block becomes an action
# Kemal
get "/posts/:id" do |env|
id = env.params.url["id"].to_i64?
halt env, status_code: 422, response: "bad id" unless id
post = DB_CONN.query_one?("SELECT id, title FROM posts WHERE id = $1", id, as: {Int64, String})
if post
env.response.content_type = "application/json"
{id: post[0], title: post[1]}.to_json
else
halt env, status_code: 404, response: "not found"
end
end# Runcobo
class Posts::ShowPostContract < RequestValidation::Contract
schema do
required :id, Int64
end
end
class Posts::ShowPost < Blog::BaseAction
get "/posts/:id"
call do
input = validate(ShowPostContract) # "abc" → a structured 422, automatically
if post = store.get_post?(id: input.id)
render_json({id: post.id, title: post.title})
else
render_json({error: "post_not_found"}, status_code: 404)
end
end
endIn a project, the action, its contract and its template are three files in src/actions/posts/ — see Project Structure.
Reading input
Kemal gives you strings and JSON::Any from env.params, and you convert them. A Runcobo contract does the conversion and checking for the whole request at once:
class Posts::CreatePostContract < RequestValidation::Contract
schema do
required :title, String, filled: true, max_size: 200
optional :tags, Array(String), max_size: 5
end
end
call do
input = validate(CreatePostContract)
input.title # String
input.tags # Array(String)?
endEvery problem is reported in one 422 response, with a code per field that clients can map to a form.
Filters and halting
# Kemal
before_post "/posts" do |env|
unless env.request.headers["Authorization"]? == "Bearer #{ENV["TOKEN"]}"
halt env, status_code: 401
end
end# Runcobo
abstract class Blog::AdminAction < Blog::BaseAction
before authenticate
private def authenticate
unless request.headers["Authorization"]? == "Bearer #{ENV["TOKEN"]}"
render_json({error: "unauthorized"}, status_code: 401)
return byebye
end
end
end
class Posts::CreatePost < Blog::AdminAction
post "/posts"
# ...
endFilters attach to classes, not path patterns, so an action's protection is visible in its declaration. Use skip to opt one action out. Request-wide behavior — CORS, compression — still belongs in an HTTP::Handler, as in Kemal. (For production code, compare tokens in constant time — see Security.)
Views
Kemal renders ECR files by path. Runcobo finds the template named after the action and compiles it in:
class Posts::ShowPost < Blog::BaseAction
get "/posts/:id"
call do
post = ...
render # → src/actions/posts/show_post.jbuilder
end
endUse Jbuilder for JSON and Water for HTML. Both can use the local variables of call.
What you gain
- Typed input and uniform
422errors without writing conversion code. - One file per endpoint, with routes you can list:
bin/cobo routes. - SQL queries checked against your schema and generated as typed methods.
- Migrations, a dev server that rebuilds on save, a console and test databases from
bin/cobo. - Request IDs, structured logging, upload limits and graceful shutdown by default.
Next: Installation, then the blog tutorial.