| 1 | package Dancer2::Plugin::HostSpecificRoute; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | our $VERSION = '1.0000'; # VERSION | |||||||||||||||||||||||||||
| 5 | 2 | our $AUTHORITY = 'cpan:GEEKRUTH'; # AUTHORITY | |||||||||||||||||||||||||||
| 6 | # ABSTRACT: Allow designating routes to respond only on hostname match | ||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Dancer2::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | plugin_keywords qw/host/; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | sub host { | ||||||||||||||||||||||||||
|
Subroutine
host: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 2 | my ($plugin, $hostname, $coderef) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.6
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | if (!$coderef || ref $coderef ne 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $coderef or ref $coderef ne "CODE"
MC/DC0%0/2
not $coderef or ref $coderef ne "CODE"
not $coderef
ref $coderef ne "CODE"
Truth table33%1/3
not(A) or B
A= not $coderefB= ref $coderef ne "CODE"
| |||||||||||||||||||||||||||||
| 16 | 0 | $plugin->app->log( | |||||||||||||||||||||||||||
| 17 | warning => 'Invalid host usage, please see docs'); | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | 4 | my $request_host = $plugin->app->request->base->host; | |||||||||||||||||||||||||||
| 20 | 4 | ▶ | if (ref $hostname eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | return $coderef->($plugin) if $request_host =~ $hostname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | else { | ||||||||||||||||||||||||||||
| 24 | 2 | ▶ | return $coderef->($plugin) if $request_host eq $hostname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 2 | $plugin->app->pass; | |||||||||||||||||||||||||||
| 27 | 2 | }; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | 1; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =pod | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 NAME | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Dancer2::Plugin::HostSpecificRoute - Allow designating routes to respond only on hostname match | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 VERSION | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | version 1.0000 | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | package MyApp; | ||||||||||||||||||||||||||||
| 47 | use Dancer2 appname => 'MyApp'; | ||||||||||||||||||||||||||||
| 48 | use Dancer2::Plugin::HostSpecificRoute; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | get '/special_route' => host 'special.host.example' => sub { | ||||||||||||||||||||||||||||
| 51 | # route code to run only when special.host.example is | ||||||||||||||||||||||||||||
| 52 | # the request host | ||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | get '/special_route' => sub { | ||||||||||||||||||||||||||||
| 56 | # default code to run /special_route when host is not | ||||||||||||||||||||||||||||
| 57 | # special.host.example | ||||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | get '/special_route_2' => host qr/\.funkyhost.example$/ => sub { | ||||||||||||||||||||||||||||
| 61 | # route code to run only when funkyhost.example is | ||||||||||||||||||||||||||||
| 62 | # the request host is *.funkyhost.example | ||||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # There is no default route for /special_route_2; it will 404, if you | ||||||||||||||||||||||||||||
| 66 | # don't address your request to *.funkyhost.example. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | It is not difficult to have your L<Dancer2> application answer to more | ||||||||||||||||||||||||||||
| 71 | than one URL or even IP address; just adding C<server_name> directives in | ||||||||||||||||||||||||||||
| 72 | your nginx config, or C<ServerName> in Apache, will do the trick nicely. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | It may be that you want to have different route code for a given path, | ||||||||||||||||||||||||||||
| 75 | depending on which host URL is requested. If that's the case, this plugin | ||||||||||||||||||||||||||||
| 76 | will make it trivially easy to do so, without having to add a C<before> | ||||||||||||||||||||||||||||
| 77 | hook to adjust the behavior of B<all> your routes. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | This plugin introduces one new keyword, C<host>, to be used as a | ||||||||||||||||||||||||||||
| 82 | predicate for your routes. It will work with any of L<Dancer2>'s | ||||||||||||||||||||||||||||
| 83 | method/route declaratives (C<get>, C<put>, C<post>, C<patch>, C<del> or | ||||||||||||||||||||||||||||
| 84 | C<any>), and can be chained with other predicates, like | ||||||||||||||||||||||||||||
| 85 | authorization-plugin directives (e.g. L<Dancer2::Plugin::Auth::Extensible>). | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | The C<host> predicate takes one parameter, which must be either: | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =over | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item * A scalar string, the FQDN of a host. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item * A quoted regex that will match the desired FQDNs to which the route should respond. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =back | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | If you wish to have a second route that can serve as a default, be sure | ||||||||||||||||||||||||||||
| 98 | to list it B<after> any matching routes with the predicate. Routes without | ||||||||||||||||||||||||||||
| 99 | a C<host> predicate are handled normally. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =over | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item * L<Dancer2> | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =back | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 BUGS AND LIMITATIONS | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | None found so far; if you find any, please post an issue on the bug tracker | ||||||||||||||||||||||||||||
| 112 | for this module. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | L<GitHub|https://github.com> user L<xoid|https://github.com/xoid> suggested | ||||||||||||||||||||||||||||
| 117 | this functionality in L<this discussion|https://github.com/PerlDancer/Dancer2/discussions/1699>. | ||||||||||||||||||||||||||||
| 118 | The idea intrigued me, and I'm doing something similar using a hook (which fires | ||||||||||||||||||||||||||||
| 119 | on Every Single Request), so here we are. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | A small bit of blame goes to L<Jason Crome|https://metacpan.org/author/CROMEDOME> for | ||||||||||||||||||||||||||||
| 122 | his constant encouragement in this sort of madness. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | D Ruth Holloway <ruth@hiruthie.me> | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | This software is copyright (c) 2023 by D Ruth Holloway. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 133 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||