api: initial migration

This commit is contained in:
Lucas 2023-03-04 21:58:18 +00:00
parent 13a6d287af
commit 7ccbc3af12
8 changed files with 190 additions and 0 deletions

20
lib/PoorBooru.pm Normal file
View File

@ -0,0 +1,20 @@
package PoorBooru;
use v5.36;
use strict;
use warnings;
use Mojo::Base "Mojolicious";
sub startup ($self)
{
$self->moniker("poorbooru");
my $config = $self->plugin("Config");
$self->secrets($config->{secrets});
$self->plugin(Mount => {
"/api/v0" => $self->home->child("script", "poorbooru-api-v0"),
});
}
1;

29
lib/PoorBooru/API/V0.pm Normal file
View File

@ -0,0 +1,29 @@
package PoorBooru::API::V0;
use v5.36;
use strict;
use warnings;
use Mojo::Base "Mojolicious";
use PoorBooru::Schema;
sub startup ($self)
{
$self->moniker("poorbooru-api-v0");
my $config = $self->plugin("Config");
$self->secrets($config->{secrets});
$self->helper(schema => sub {
state $schema = PoorBooru::Schema->connect($config->{dsn})
});
my $r = $self->routes;
$r->get("/meta")->to("meta#index");
$r->get("/tags")->to("tags#list");
$r->get("/tag/:tag_id_or_name")->to("tags#show");
}
1;

View File

@ -0,0 +1,25 @@
package PoorBooru::API::V0::Controller::Meta;
use v5.36;
use strict;
use warnings;
use Mojo::Base "Mojolicious::Controller";
sub index ($self)
{
return $self->render(json => [
{ path => "/meta", verb => "GET" },
{ path => "/tags", verb => "GET" },
{ path => "/tags", verb => "POST" },
{ path => "/tag/:tag_id_or_name", verb => "GET" },
{ path => "/media/:media_id", verb => "GET" },
{ path => "/media/download/:media_id", verb => "GET" },
{ path => "/random/media", verb => "GET" },
{ path => "/random/tag", verb => "GET" },
]);
};
1;

View File

@ -0,0 +1,82 @@
package PoorBooru::API::V0::Controller::Tags;
use v5.36;
use strict;
use warnings;
use Mojo::Base "Mojolicious::Controller";
my $MEDIA_SEARCH_OPTS = {
order_by => { -desc => "media_id" },
rows => 5,
};
my $TAGS_COUNT_VIEW_SEARCH_OPTS = {
order_by => { -desc => "count" },
rows => 5,
};
sub _pager_links ($c, $pager)
{
+{ map +( $_ => ($pager->$_ and
$c->url_with->query({page => $pager->$_})) ),
qw(first_page previous_page current_page next_page last_page) }
}
sub list ($self)
{
my $v = $self->validation;
my $page = $v->optional("page")->num(1, undef)->param // 1;
return $self->render(
json => {error => "Invalid page number"},
status => 400,
) if $v->has_error;
my $paged_tags = $self->schema->resultset("TagCountView")
->search({}, $TAGS_COUNT_VIEW_SEARCH_OPTS)->page($page);
my @tags = map +{
id => $_->tag_id,
name => $_->name,
count => $_->count,
}, $paged_tags->all;
return $self->render(json => {
pager => _pager_links($self, $paged_tags->pager),
tags => \@tags,
});
};
sub show ($self)
{
my $v = $self->validation;
my $page = $v->optional("page")->num(1, undef)->param // 1;
return $self->render(
json => {error => "Invalid page number"},
status => 400,
) if $v->has_error;
my $tag_id_or_name = $self->stash("tag_id_or_name");
my $tag = $self->schema->resultset("Tag")->single([
{ tag_id => $tag_id_or_name },
{ name => $tag_id_or_name },
]);
return $self->render(
json => {error => "Tag not found"},
status => 404,
) if !defined($tag);
my $paged_media = $self->schema->resultset("MediaTag")
->search({ tag_id => $tag->tag_id }, $MEDIA_SEARCH_OPTS)
->page($page);
my @media = map { $_->media_id } $paged_media->all;
return $self->render(json => {
id => $tag->tag_id,
name => $tag->name,
pager => _pager_links($self, $paged_media->pager),
media => \@media,
});
};
1;

8
poorbooru-api-v0.conf Normal file
View File

@ -0,0 +1,8 @@
{
dsn => "dbi:SQLite:dbname=db/PoorBooru.db",
secrets => [
# Generate with "openssl rand -hex 32".
pack("H*", "a0b1..."),
],
}

6
poorbooru.conf Normal file
View File

@ -0,0 +1,6 @@
{
secrets => [
# Generate with "openssl rand -hex 32".
pack("H*", "a0b1..."),
],
}

10
script/poorbooru Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env perl
use v5.36;
use strict;
use warnings;
use Mojo::File qw(curfile);
use lib curfile->dirname->sibling("lib")->to_string;
use Mojolicious::Commands;
Mojolicious::Commands->start_app("PoorBooru");

10
script/poorbooru-api-v0 Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env perl
use v5.36;
use strict;
use warnings;
use Mojo::File qw(curfile);
use lib curfile->dirname->sibling("lib")->to_string;
use Mojolicious::Commands;
Mojolicious::Commands->start_app("PoorBooru::API::V0");