In Mojolicious app, I need to figure out which controller method will be handling the incoming request so that I can log details of the remote client and add some logic. I want to do it at only one place and not in every controller methods. I have tried some of the HOOKS but could not figure out. Much appreciate any help on this.
Routes are generated/created from several OpenApi Plugin files and also use Oauth2 Plugin for OAuth.
CORE
Perl (v5.16.3, linux)
Mojolicious (8.22, Supervillain)
OPTIONAL
Cpanel::JSON::XS 4.04+ (n/a)
EV 4.0+ (4.22)
IO::Socket::Socks 0.64+ (n/a)
IO::Socket::SSL 2.009+ (2.060)
Net::DNS::Native 0.15+ (n/a)
Role::Tiny 2.000001+ (2.000005)
Sort of "intercepting" a request before it is handled by the controller, and only then letting the controller handle it.
Let’s go over some of the different use cases that could come up when using MVC handler interceptors. One of the things you might want to do is retrieve a header from the incoming request to perform some type of validation. The request object (of type HttpServletRequest) contains a method called getHeader.
Any time you create a handler interceptor there will be two key components involved: In order for you to create a handler interceptor, you will need to create a class that extends the HandlerInterceptorAdapter class. This class will let you override two methods which are the preHandle and postHandle methods.
The idea is that EVERY (!) incoming request is parsed and its contents get decoded without the programmer of the rest controller getting having to care about this at all. They should just access data via/from the MyClass instance.
I asked in mojolicious mailing list and got the reply from the creator of the Mojolicious, Sebastian Riedel. Thanks.
For everybody's benifit. $c->match->stack
in around_action
hook has the info I was looking for.
In your application startup
method:
sub startup {
my $self = shift;
...
$self->hook(
around_action => sub {
my ($next, $c, $action, $last) = @_;
use DDP;
p $c->match;
# prints all the info about the controller and the method/action
# it is going to call
...
}
);
...
}
Mojolicious::Routes::Match {
Parents Mojo::Base
public methods (7) : endpoint, find, has, path_for, position, root, stack
private methods (1) : _match
internals: {
endpoint Mojolicious::Routes::Route,
position 0,
root Mojolicious::Routes,
stack [
[0] {
action "controller_method_name",
controller "ControllerClassName",
handler "openapi",
id 3336,
openapi.object Mojolicious::Plugin::OpenAPI,
openapi.op_path [
[0] "paths",
[1] "/api/endpoint/path/{id}/status",
[2] "get"
],
openapi.parameters [
[0] {
description "job id",
in "path",
name "id",
required JSON::PP::Boolean,
type "number"
}
]
}
]
}
}
$c->match->stack
: action "controller_method_name",
controller "ControllerClassName",
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With