Skip to content
Snippets Groups Projects
Verified Commit 1edeb32c authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Pridana podpora openapi

parent 7cc5b19a
No related branches found
No related tags found
No related merge requests found
...@@ -27,7 +27,8 @@ RUN cpanm \ ...@@ -27,7 +27,8 @@ RUN cpanm \
Mojo::Pg \ Mojo::Pg \
Mojo::Redis \ Mojo::Redis \
Mojo::JWT \ Mojo::JWT \
Mojolicious::Plugin::Authentication Mojolicious::Plugin::Authentication \
Mojolicious::Plugin::OpenAPI
ADD . /opt/PZ ADD . /opt/PZ
WORKDIR /opt/PZ WORKDIR /opt/PZ
......
...@@ -20,6 +20,7 @@ sub startup { ...@@ -20,6 +20,7 @@ sub startup {
# Delka session # Delka session
$self->sessions->default_expiration($cfg->{session}{lifetime}); $self->sessions->default_expiration($cfg->{session}{lifetime});
$self->plugin('PZ::Helpers::Core');
$self->plugin('PZ::Helpers::OIDC'); $self->plugin('PZ::Helpers::OIDC');
my $redis = Mojo::Redis->new( 'redis://' . $cfg->{redis}{server} ); my $redis = Mojo::Redis->new( 'redis://' . $cfg->{redis}{server} );
...@@ -43,7 +44,10 @@ sub startup { ...@@ -43,7 +44,10 @@ sub startup {
autoload_user => 1, autoload_user => 1,
load_user => sub { load_user => sub {
my $c = shift; my $c = shift;
return $c->session->{user}; my $user = $c->schema->resultset('User')->find({
id => $c->session->{user}{id},
});
return $user;
}, },
validate_user => sub { validate_user => sub {
my $c = shift; my $c = shift;
...@@ -52,8 +56,40 @@ sub startup { ...@@ -52,8 +56,40 @@ sub startup {
}, },
}); });
$self->plugin("OpenAPI" => {
url => $self->home->rel_file("openapi.yaml"),
schema => 'v3',
plugins => [qw(+SpecRenderer +Cors +Security)],
render_specification => 1,
render_specification_for_paths => 1,
default_response_codes => [400, 401, 403, 404, 500, 501],
security => {
Token => sub {
my ($c, $definition, $scopes, $cb ) = @_;
my $token = $c->req->headers->header('X-Auth-Token');
return $c->$cb('Authorization header not present') if ! $token;
my $user = $c->schema->resultset('User')->find(
{ token => $token }
);
if (! $user ) {
return $c->$cb('Invalid user');
}
$c->stash->{user} = $user;
return $c->$cb();
}
}
});
# defautni globalni promenne ve stash # defautni globalni promenne ve stash
$self->defaults(); $self->defaults(
openapi_cors_allowed_origins => ['*'],
);
# vypnuti cache templatu pri vyvoji # vypnuti cache templatu pri vyvoji
$self->renderer->cache->max_keys(0) if $cfg->{dev_mode}; $self->renderer->cache->max_keys(0) if $cfg->{dev_mode};
......
...@@ -74,4 +74,18 @@ sub qr ($c) { ...@@ -74,4 +74,18 @@ sub qr ($c) {
$c->render( data => $png ); $c->render( data => $png );
} }
sub list ($c) {
my @shortcuts = ();
SHORTCUT:
foreach my $shortcut ( $c->stash->{user}->shortcuts ) {
push @shortcuts, $c->spec_filter(
{ $shortcut->get_columns }, 'Shortcut'
);
}
$c->render(json => \@shortcuts );
}
1; 1;
package PZ::Helpers::Core;
use base 'Mojolicious::Plugin';
use YAML;
sub register {
my ($class, $self ) = @_;
$self->helper(error => sub {
my $c = shift;
my $status = shift;
my $errors = [];
$c->cirpack_ws->rollback();
if ( scalar @_ == 2 ) {
$errors = [{ code => shift, message => shift }];
}
elsif ( ref $_[0] eq 'ARRAY' ) {
$errors = shift;
}
elsif ( ref $_[0] eq 'HASH' ) {
$errors = [ shift ];
}
else {
$errors = [{ message => shift, code => undef }];
}
$c->stash(
status => $status,
openapi => { errors => $errors }
);
return undef;
});
$self->helper( trace => sub {
my $c = shift;
my $data = shift // '';
$data = Dump $data if ref $data;
$c->app->log->debug($data);
});
$self->helper( spec_filter => sub {
my $c = shift;
my $data = shift;
my $class = shift;
if (my $def =$c->openapi->spec("/components/schemas/$class")) {
my $filtered = {};
KEY:
foreach my $key ( keys %{ $def->{properties} } ) {
my $value = $data->{$key};
my $nullable = 0;
my $types = $def->{properties}{$key}{type};
if ( ref $types eq 'ARRAY' ) {
TYPE:
foreach my $type ( @{ $types } ) {
$nullable = 1, last if $type eq 'null';
}
}
if ( $key =~ /^cirpack_/ && $value eq 'XXX' ) {
$value = $nullable ? undef : '';
}
$filtered->{$key} = $value;
}
$data = $filtered;
}
return $data;
});
}
1;
...@@ -39,6 +39,11 @@ __PACKAGE__->add_unique_constraint( ...@@ -39,6 +39,11 @@ __PACKAGE__->add_unique_constraint(
'token' => [qw(token)] 'token' => [qw(token)]
); );
__PACKAGE__->has_many(
shortcuts => 'PZ::Schema::Result::Shortcut',
{ 'foreign.user_id' => 'self.id', },
);
sub set_token { sub set_token {
my $self = shift; my $self = shift;
my $new = shift; my $new = shift;
......
openapi: 3.0.3
info:
title: Piratský zkracovač
description: Piratský zkracovač API
version: 1.15.1
license:
name: Artistic License 2.0
url: https://www.perlfoundation.org/artistic-license-20.html
contact:
name: Andrej Ramašeuski
email: andrej@x2.cz
url: https://pardubicky.pirati.cz/lide/andrej-ramaseuski/
servers:
- url: https://z.pirati.cz/api
description: Production server
- url: http://127.0.0.1:3000/api
description: Test server
components:
schemas:
Shortcut:
type: object
properties:
id:
type: integer
shortcut:
type: string
description: Zkratka
url:
type: string
description: URL pro přesměrování
code:
type: integer
description: Kód přesměrování
securitySchemes:
Token:
type: apiKey
in: header
name: X-Auth-Token
paths:
/shortcuts:
get:
tags:
- shortcuts
security:
- Token: []
summary: "Seznam zkratek"
operationId: Shortcuts
x-mojo-to: shortcut#list
responses:
200:
description: Seznam zkratek
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Shortcut'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment