module Kemal::HandlerInterface

Overview

Kemal::HandlerInterface provides helpful methods for use in middleware creation

More specifically, only, #only_match?, exclude, #exclude_match? allows one to define the conditional execution of custom handlers.

To use, simply include it within your type.

It is an implementation of HTTP::Handler and can be used anywhere that requests an HTTP::Handler type.

Included Modules

Direct including types

Defined in:

kemal/handler.cr

Macro Summary

Instance Method Summary

Macro Detail

macro exclude(paths, method = "GET") #

[View source]
macro only(paths, method = "GET") #

[View source]

Instance Method Detail

def call(context : HTTP::Server::Context) #

[View source]
def exclude_match?(env : HTTP::Server::Context) #

Processes the path based on exclude paths which is a Array(String). If the path is not found on exclude conditions the handler will continue processing. If the path is found in exclude conditions it'll stop processing and will pass the request to next handler.

However this is not done automatically. All handlers must inherit from Kemal::Handler.

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

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

[View source]
def only_match?(env : HTTP::Server::Context) #

Processes the path based on only paths which is a Array(String). If the path is not found on only conditions the handler will continue processing. If the path is found in only conditions it'll stop processing and will pass the request to next handler.

However this is not done automatically. All handlers must inherit from Kemal::Handler.

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

[View source]