Modern-OpenAPI-Generator
view release on metacpan or search on metacpan
lib/Modern/OpenAPI/Generator/CodeGen/Server.pm view on Meta::CPAN
my $stubdata_use =
$local_test ? "use ${base}::StubData;\n" : '';
my @subs;
for my $op (@$ops) {
my $oid = _safe_operation_sub( $op->{operation_id} );
if ($local_test) {
( my $oid_q = $op->{operation_id} ) =~ s/'/\\'/g;
push @subs, <<SUB;
sub $oid {
my \$self = shift;
return unless \$self->openapi->valid_input;
my (\$st, \$body) = ${base}::StubData->for_operation('$oid_q');
return \$self->render( status => \$st, json => \$body );
}
SUB
}
else {
my $todo = $skeleton ? '' : "\n # TODO: implement business logic for $oid\n";
push @subs, <<SUB;
sub $oid {$todo
my \$self = shift;
return unless \$self->openapi->valid_input;
\$self->stash(
status => 501,
openapi => { errors => [ { message => 'Not implemented', path => '/' } ] },
);
return \$self->render;
}
SUB
}
}
my $body = join "\n", @subs;
return <<"PM";
package $pkg;
use v5.26;
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Controller', -signatures;
$stubdata_use
$body
1;
PM
}
sub _script_pm {
my ($base) = @_;
return <<"SCRIPT";
#!/usr/bin/env perl
use v5.26;
use strict;
use warnings;
use FindBin qw(\$Bin);
use File::Spec;
BEGIN {
\$ENV{MOJO_HOME} ||= File::Spec->catdir( \$Bin, '..' );
unshift \@INC, File::Spec->catdir( \$Bin, '..', 'lib' );
if ( grep { \$_ eq '--local-test' } \@ARGV ) {
\$ENV{OAPI_SWAGGER_LOCAL_ORIGIN} = 1;
\@ARGV = grep { \$_ ne '--local-test' } \@ARGV;
}
}
use ${base}::Server;
${base}::Server->new->start(\@ARGV);
SCRIPT
}
1;
__END__
=encoding utf8
=head1 NAME
Modern::OpenAPI::Generator::CodeGen::Server - Generate Mojolicious server and controller
=head1 DESCRIPTION
Writes C<::Server>, C<::Server::Controller>, C<script/server.pl>, and
F<share/openapi.mojo.yaml> for L<Mojolicious::Plugin::OpenAPI>.
=head2 generate
Full server mode: REST API from the spec, optional Swagger UI, optional
C<--local-test> stubs.
=head2 generate_spec_ui_only
Serves the spec and Swagger UI only (no generated controller routes).
=cut
( run in 1.080 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )