allow swagger_path installed routes to be relocated

This commit is contained in:
Oliver Gorwits
2020-04-19 18:00:15 +01:00
parent da33478504
commit b9c002477b
6 changed files with 23 additions and 4 deletions

View File

@@ -89,6 +89,7 @@ Module::Build->new(
'Term::UI' => '0',
'Try::Tiny' => '0',
'URI' => '0',
'URI::Based' => '0',
'URL::Encode' => '0.03',
'URL::Encode::XS' => '0',
'YAML' => '0.84',

View File

@@ -7,6 +7,7 @@ use Dancer ':script';
use Path::Class 'dir';
use Net::Domain 'hostdomain';
use File::ShareDir 'dist_dir';
use URI::Based;
BEGIN {
if (setting('include_paths') and ref [] eq ref setting('include_paths')) {
@@ -224,4 +225,11 @@ config->{'reports'} = [ @{setting('system_reports')}, @{setting('reports')} ];
#config->{plugins}->{Swagger}->{ui_dir} =
#dir(dist_dir('App-Netdisco'), 'share', 'public', 'swagger-ui')->absolute;
# setup helpers for when request->uri_for() isn't available
# (for example when inside swagger_path())
config->{url_base}
= URI::Based->new((config->{path} eq '/') ? '' : config->{path});
config->{api_base}
= config->{url_base}->with('/api/v1')->path;
true;

View File

@@ -32,6 +32,7 @@ BEGIN {
$response->status($status || 302);
$response->headers('Location' => $destination);
};
# neater than using Dancer::Plugin::Res to handle JSON differently
*Dancer::send_error = sub {
my ($body, $status) = @_;

View File

@@ -9,6 +9,7 @@ use Try::Tiny;
swagger_path {
tags => ['Objects'],
path => setting('api_base').'/object/device/{ip}',
description => 'Returns a row from the device table',
parameters => [
ip => {
@@ -27,6 +28,7 @@ swagger_path {
foreach my $rel (qw/device_ips vlans ports modules port_vlans wireless_ports ssids powered_ports/) {
swagger_path {
tags => ['Objects'],
path => setting('api_base')."/object/device/{ip}/$rel",
description => "Returns $rel rows for a given device",
parameters => [
ip => {
@@ -46,7 +48,7 @@ foreach my $rel (qw/device_ips vlans ports modules port_vlans wireless_ports ssi
swagger_path {
tags => ['Objects'],
description => 'Returns a row from the device_port table',
path => '/api/v1/object/device/{ip}/port/{port}',
path => setting('api_base').'/object/device/{ip}/port/{port}',
parameters => [
ip => {
description => 'Canonical IP of the Device. Use Search methods to find this.',
@@ -72,7 +74,7 @@ foreach my $rel (qw/nodes active_nodes nodes_with_age active_nodes_with_age vlan
swagger_path {
tags => ['Objects'],
description => "Returns $rel rows for a given port",
path => "/api/v1/object/device/{ip}/port/{port}/$rel",
path => setting('api_base')."/object/device/{ip}/port/{port}/$rel",
parameters => [
ip => {
description => 'Canonical IP of the Device. Use Search methods to find this.',
@@ -99,7 +101,7 @@ foreach my $rel (qw/power properties ssid wireless agg_master neighbor last_node
swagger_path {
tags => ['Objects'],
description => "Returns the related $rel table entry for a given port",
path => "/api/v1/object/device/{ip}/port/{port}/$rel",
path => setting('api_base')."/object/device/{ip}/port/{port}/$rel",
parameters => [
ip => {
description => 'Canonical IP of the Device. Use Search methods to find this.',
@@ -124,6 +126,7 @@ foreach my $rel (qw/power properties ssid wireless agg_master neighbor last_node
swagger_path {
tags => ['Objects'],
path => setting('api_base').'/object/device/{ip}/nodes',
description => "Returns the nodes found on a given Device",
parameters => [
ip => {
@@ -149,6 +152,7 @@ swagger_path {
swagger_path {
tags => ['Objects'],
path => setting('api_base').'/object/vlan/{vlan}/nodes',
description => "Returns the nodes found in a given VLAN",
parameters => [
vlan => {

View File

@@ -85,6 +85,7 @@ hook 'before' => sub {
swagger_path {
description => 'Obtain an API Key',
tags => ['General'],
path => setting('url_base')->with('/login')->path,
parameters => [],
responses => { default => { examples => {
'application/json' => { api_key => 'cc9d5c02d8898e5728b7d7a0339c0785' } } },
@@ -157,13 +158,15 @@ post '/login' => sub {
# ugh, *puke*, but D::P::Swagger has no way to set this with swagger_path
# must be after the path is declared, above.
Dancer::Plugin::Swagger->instance->doc->{paths}->{'/login'}
Dancer::Plugin::Swagger->instance->doc
->{paths}->{ setting('url_base')->with('/login')->path }
->{post}->{security}->[0]->{BasicAuth} = [];
# we override the default login_handler, so logout has to be handled as well
swagger_path {
description => 'Destroy user API Key and session cookie',
tags => ['General'],
path => setting('url_base')->with('/logout')->path,
parameters => [],
responses => { default => { examples => { 'application/json' => {} } } },
},

View File

@@ -157,6 +157,7 @@ register 'register_search_tab' => sub {
my $tag = $config->{tag};
swagger_path {
tags => ['Search'],
path => setting('api_base')."/search/$tag",
description => $config->{label} .' Search',
parameters => $config->{api_parameters},
responses =>
@@ -197,6 +198,7 @@ register 'register_report' => sub {
(my $category_path = lc $config->{category}) =~ s/ /-/g;
swagger_path {
tags => ['Reports'],
path => setting('api_base')."/report/$category_path/$tag",
description => $config->{label} .' Report',
parameters =>
($config->{api_parameters} ||