iyi

Playground

samples/iyi/webapp.iyi

The Sinatra feel, kept — but written by the consumer rather than injected by.

samples/iyi/webapp.iyi · iyi-webapp.wasm · 163 KB
# The Sinatra feel, kept — but written by the consumer rather than injected by
# the library. This is the whole argument of SPEC.md II.3 in one file.
module samples/webapp

import kemal/dsl::*
import kemal/router::{Router, Context}

before_all do |env|
  nil
end

get "/" do |env|
  "home"
end

get "/users" do |env|
  "all users"
end

post "/users" do |env|
  "created"
end

# A route may return anything implementing `IntoBody`, which is strictly more
# than Kemal allows: Kemal coerces non-String results to "" at runtime.
get "/count" do |env|
  42
end

# A route returning a type with no impl is refused where it is written:
#
#   get "/bad" do |env|
#     [1, 2, 3]
#   end
#   # => Array(Int32) does not implement Kemal::Router::IntoBody,
#   #    required by `B` in `get`
#
# Kemal cannot say this. `HTTP::Server::Context -> _` accepts the block and
# silently returns an empty body on every request.

users = Router.new
users.get "/" do |env|
  "users index"
end

# The sub-router is a block parameter rather than the block's `self`: R-2 asks
# an exported `def` to describe its block, and a block whose `self` changes is
# the one shape no annotation can describe yet. See `kemal/router.iyi`.
users.namespace "/admin" do |admin|
  admin.get "/dashboard" do |env|
    "dashboard"
  end
end

mount "/v1", users

app.routes.each { |route| puts "#{route[0]} #{route[1]}" }
puts app.dispatch(Context.new("GET", "/count"))
output
Press Run.

recorded with iyi 0.18.0 (built on Crystal 1.22.0-dev [2a9653052] (2026-10-18)) · Linux 7.0.0-38-generic x86_64, QEMU Virtual CPU version 2.5+ · 2026-10-19

You can type in the pane: nothing here compiles, so what you type cannot run, and the page says which bytes it is running the moment the text stops being the recorded file. There is no standard input, no filesystem and no threads on this target, and one sample fails on purpose for exactly that reason.

Run executes iyi-webapp.wasm, the module the compiler built from this file, in your browser. How this works

Try it on your machine

Copy the program into webapp.iyi, change something, and run it. Only a compiler on your own machine can run what you wrote.

sh, after installing
$ iyi run webapp.iyi