separate openapi to own module

This commit is contained in:
Oliver Gorwits
2019-03-22 07:38:24 +00:00
parent a872b7f5cc
commit 3e0af5888c
2 changed files with 60 additions and 49 deletions

View File

@@ -5,7 +5,6 @@ use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC; use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible; use Dancer::Plugin::Auth::Extensible;
use Dancer::Plugin::Swagger;
use URI (); use URI ();
use Socket6 (); # to ensure dependency is met use Socket6 (); # to ensure dependency is met
@@ -17,6 +16,7 @@ use App::Netdisco::Util::Web
qw/request_is_api interval_to_daterange/; qw/request_is_api interval_to_daterange/;
use App::Netdisco::Web::AuthN; use App::Netdisco::Web::AuthN;
use App::Netdisco::Web::OpenAPI;
use App::Netdisco::Web::Static; use App::Netdisco::Web::Static;
use App::Netdisco::Web::Search; use App::Netdisco::Web::Search;
use App::Netdisco::Web::Device; use App::Netdisco::Web::Device;
@@ -77,28 +77,6 @@ eval {
}; };
Dancer::Session::Cookie::init(session); Dancer::Session::Cookie::init(session);
# setup for swagger API
my $swagger = Dancer::Plugin::Swagger->instance->doc;
$swagger->{schemes} = ['http','https'];
$swagger->{consumes} = 'application/json';
$swagger->{produces} = 'application/json';
$swagger->{tags} = [
{name => 'General'},
{name => 'Devices',
description => 'Operations relating to Devices (switches, routers, etc)'},
{name => 'Nodes',
description => 'Operations relating to Nodes (end-stations such as printers)'},
{name => 'NodeIPs',
description => 'Operations relating to MAC-IP mappings (IPv4 ARP and IPv6 Neighbors)'},
];
$swagger->{securityDefinitions} = {
APIKeyHeader =>
{ type => 'apiKey', name => 'Authorization', in => 'header' },
BasicAuth =>
{ type => 'basic' },
};
$swagger->{security} = [ { APIKeyHeader => [] } ];
# workaround for https://github.com/PerlDancer/Dancer/issues/935 # workaround for https://github.com/PerlDancer/Dancer/issues/935
hook after_error_render => sub { setting('layout' => 'main') }; hook after_error_render => sub { setting('layout' => 'main') };
@@ -123,11 +101,6 @@ hook after_error_render => sub { setting('layout' => 'main') };
for @port_columns; for @port_columns;
} }
# support for checking if this is an api request even after forward
hook 'before' => sub {
vars->{'orig_path'} = request->path unless request->is_forward;
};
hook 'before' => sub { hook 'before' => sub {
my $key = request->path; my $key = request->path;
if (param('tab') and ($key !~ m/ajax/)) { if (param('tab') and ($key !~ m/ajax/)) {
@@ -211,16 +184,6 @@ hook 'before_template' => sub {
$Template::Stash::PRIVATE = undef; $Template::Stash::PRIVATE = undef;
}; };
# workaround for Swagger plugin weird response body
hook 'after' => sub {
my $r = shift; # a Dancer::Response
if (request->path eq '/swagger.json') {
$r->content( to_json( $r->content ) );
header('Content-Type' => 'application/json');
}
};
# remove empty lines from CSV response # remove empty lines from CSV response
# this makes writing templates much more straightforward! # this makes writing templates much more straightforward!
hook 'after' => sub { hook 'after' => sub {
@@ -238,17 +201,6 @@ hook 'after' => sub {
} }
}; };
# forward API calls to AJAX route handlers
any '/api/:type/:identifier/:method' => require_login sub {
pass unless setting('api_enabled')
->{ params->{'type'} }->{ params->{'method'} };
vars->{'is_api'} = 1;
my $target =
sprintf '/ajax/content/%s/%s', params->{'type'}, params->{'method'};
forward $target, { tab => params->{'method'}, q => params->{'identifier'} };
};
any qr{.*} => sub { any qr{.*} => sub {
if (request_is_api()) { if (request_is_api()) {
status(404); status(404);

View File

@@ -0,0 +1,59 @@
package App::Netdisco::Web;
use Dancer ':syntax';
use Dancer::Plugin::Ajax;
use Dancer::Plugin::Swagger;
use App::Netdisco::Util::Web
qw/request_is_api interval_to_daterange/;
# setup for swagger API
my $swagger = Dancer::Plugin::Swagger->instance->doc;
$swagger->{schemes} = ['http','https'];
$swagger->{consumes} = 'application/json';
$swagger->{produces} = 'application/json';
$swagger->{tags} = [
{name => 'General'},
{name => 'Devices',
description => 'Operations relating to Devices (switches, routers, etc)'},
{name => 'Nodes',
description => 'Operations relating to Nodes (end-stations such as printers)'},
{name => 'NodeIPs',
description => 'Operations relating to MAC-IP mappings (IPv4 ARP and IPv6 Neighbors)'},
];
$swagger->{securityDefinitions} = {
APIKeyHeader =>
{ type => 'apiKey', name => 'Authorization', in => 'header' },
BasicAuth =>
{ type => 'basic' },
};
$swagger->{security} = [ { APIKeyHeader => [] } ];
# support for checking if this is an api request even after forward
hook 'before' => sub {
vars->{'orig_path'} = request->path unless request->is_forward;
};
# workaround for Swagger plugin weird response body
hook 'after' => sub {
my $r = shift; # a Dancer::Response
if (request->path eq '/swagger.json') {
$r->content( to_json( $r->content ) );
header('Content-Type' => 'application/json');
}
};
# forward API calls to AJAX route handlers
any '/api/:type/:identifier/:method' => require_login sub {
pass unless setting('api_enabled')
->{ params->{'type'} }->{ params->{'method'} };
vars->{'is_api'} = 1;
my $target =
sprintf '/ajax/content/%s/%s', params->{'type'}, params->{'method'};
forward $target, { tab => params->{'method'}, q => params->{'identifier'} };
};
true;