-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
handler.cr
37 lines (32 loc) · 887 Bytes
/
handler.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require "./context"
# A handler is a class which includes `HTTP::Handler` and implements the `call` method.
# You can use a handler to intercept any incoming request and can modify the response.
# These can be used for request throttling, ip-based filtering, adding custom headers e.g.
#
# ### A custom handler
#
# ```
# require "http/server/handler"
#
# class CustomHandler
# include HTTP::Handler
#
# def call(context)
# puts "Doing some stuff"
# call_next(context)
# end
# end
# ```
module HTTP::Handler
property next : Handler | HandlerProc | Nil
abstract def call(context : HTTP::Server::Context)
def call_next(context : HTTP::Server::Context)
if next_handler = @next
next_handler.call(context)
else
context.response.respond_with_status(:not_found)
end
end
alias HandlerProc = HTTP::Server::Context ->
end
require "./handlers/*"