-
Notifications
You must be signed in to change notification settings - Fork 46
Middleware
Andrej Yaroshuk edited this page Oct 11, 2015
·
3 revisions
Middleware are implemented as classes. Middleware class inherits from Base::Middleware
(or, just type Middleware
if you prefer require amethyst/all
), and should have the call
method, and return an Amethyst::Http::Response
. (usually, by calling next midleware in MiddlewareStack
with @app.call(request)
)
def call(request)
# do something
response = @app.call(request) # here response came from 'upper' middleware
# do something
response # return response down to the stack
end
Here is an example of middleware that calculates time elapsed between request and response.
class TimeLogger < Middleware::Base
# This one will be called when app gets request. It accepts Http::Request
def call(request)
t_req = Time.now
response = @app.call(request) # sends request to next middleware
t_res = Time.now
elapsed = (t_res - t_req).to_f*1000
string = "%.4f ms" % elapsed
response.body += "<hr>"+string
response
end
end