Hello everyone,

Kemal 0.17.0 is here! It’s the most complete and polished release yet with lots of goodies. This release also adds support for the awesome Crystal 0.20.0 release.

Better Middleware / Filter Routing

Now all requests will first go through the Middleware stack then Filters (before_* / after_*) and will finally reach the matching route.

Which is illustrated as,

Request -> Middleware -> Filter -> Route

Let’s take a look at an example. Be aware that all custom handlers have to inherit from Kemal::Handler.

class MyCustomHandler < Kemal::Handler
  def call(env)
    puts "You shall pass me first!"
    call_next(env)
  end
end

before_get "/" do
  puts "This will work before get"
end

get "/" do
  puts "Hello Kemal!"
end

add_handler MyCustomHandler.new

Now what’s the output of this? Before Kemal 0.17.0

This will work before get
You shall pass me first!
Hello Kemal

As you can easily see before 0.17.0 handlers worked after the filters. After 0.17.0 it will work as expected

You shall pass me first!
This will work before get
Hello Kemal

Neat!

Only / Exclude for Handlers

Now that the lifecycle of a request / response starts from a Handler it’d be great to have a handler work for specified routes.

That’s where only/exclude comes to play.

Consider that we have a handler like this which will on run for / path.

class OnlyHandler < Kemal::Handler
  only ["/"]

  def call(env)
    return call_next(env) unless only_match?(env)
    puts "If the path is / i will be doing some processing here."
  end
end

Here’s 2 points to catch.

  • You define which routes the handler will run with only ["/"].
  • You check if the route is a only registered with only_match?(env).

Here’s also a sample for exclude

class ExcludeHandler < Kemal::Handler
  exclude ["/"]

  def call(env)
    return call_next(env) unless only_match?(env)
    puts "If the path is NOT / i will be doing some processing here."
  end
end

Simple enough!

Better Custom Error Handling

error handler now also yields error. For example you can get the error mesasage like

error 500 do |env, err|
  err.message
end

More

Be sure to check out the CHANGELOG for the full awesomeness and bug fixes :) And a big THANKS to everyone for making this release possible <3

Happy Crystalling!