-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6070 from perlpunk/routes
Add endpoint for listing all WebAPI routes
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright SUSE LLC | ||
# SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
package OpenQA::WebAPI::Controller::API::V1::Routes; | ||
use Mojo::Base 'Mojolicious::Controller', -signatures; | ||
|
||
=pod | ||
=head1 NAME | ||
OpenQA::WebAPI::Controller::API::V1::Routes | ||
=head1 SYNOPSIS | ||
use OpenQA::WebAPI::Controller::API::V1::Routes; | ||
=head1 DESCRIPTION | ||
=head1 METHODS | ||
=over 4 | ||
=item list() | ||
Lists all WebAPI routes | ||
=back | ||
=cut | ||
|
||
sub list ($self) { | ||
my $routes = $self->app->routes->children; | ||
my %hash; | ||
_walk($_, \%hash, '') for @$routes; | ||
my @list; | ||
for my $route (sort keys %hash) { | ||
my $methods = $hash{$route}; | ||
push @list, {path => $route, methods => [sort keys %$methods]}; | ||
} | ||
$self->render(json => {routes => \@list}); | ||
} | ||
|
||
sub _walk ($route, $hash, $path) { | ||
my $pattern = $route->pattern->unparsed || ''; | ||
my $methods = $route->can('methods') ? $route->methods : $route->via; | ||
my $endpoint = "$path$pattern" || '/'; | ||
$hash->{$endpoint}->{$_} = 1 for @{$methods || []}; | ||
for my $child (@{$route->children || []}) { | ||
_walk($child, $hash, "$path$pattern"); | ||
} | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters