Also make the file headings more consistent with regards to spacingg.
74 lines
1.8 KiB
Perl
74 lines
1.8 KiB
Perl
package Pooru::API::V0;
|
|
use v5.40;
|
|
use Mojo::Base "Mojolicious";
|
|
|
|
use DBI;
|
|
use DBIx::Migration;
|
|
|
|
use Pooru::API::V0::Model::Media;
|
|
use Pooru::API::V0::Model::Tags;
|
|
|
|
use constant {
|
|
"Pooru::API::V0::Model::Media::ROWS" => 12,
|
|
"Pooru::API::V0::Model::Media::SIMILAR_ROWS" => 6,
|
|
"Pooru::API::V0::Model::Tags::ROWS" => 100,
|
|
"Pooru::API::V0::Model::Tags::SEARCH_ROWS" => 15,
|
|
};
|
|
|
|
|
|
sub startup ($self)
|
|
{
|
|
$self->moniker("pooru-api-v0");
|
|
|
|
my $config = $self->plugin("Config");
|
|
$self->secrets($config->{secrets});
|
|
|
|
DBIx::Migration->new({
|
|
dsn => $config->{dsn},
|
|
dir => $self->app->home->child("migrations")->to_string,
|
|
})->migrate;
|
|
|
|
$self->helper(dbh => sub {
|
|
state $dbh = DBI->connect(
|
|
$config->{dsn},
|
|
$config->{db_username},
|
|
$config->{db_password},
|
|
{
|
|
PrintError => 0,
|
|
RaiseError => 1,
|
|
AutoCommit => 1,
|
|
($config->{dbi_connect_args} // {})->%*,
|
|
}
|
|
)
|
|
});
|
|
|
|
$self->dbh->{FetchHashKeyName} = "NAME_lc";
|
|
if ($self->dbh->{Driver}->{Name} eq "SQLite") {
|
|
$self->dbh->do("PRAGMA foreign_keys = ON");
|
|
}
|
|
|
|
$self->helper(tags_model => sub {
|
|
state $model = Pooru::API::V0::Model::Tags->new($self->dbh)
|
|
});
|
|
$self->helper(media_model => sub {
|
|
state $model = Pooru::API::V0::Model::Media->new($self->dbh)
|
|
});
|
|
|
|
my $r = $self->routes;
|
|
|
|
$r->get("/meta")->to("meta#index")->name("meta");
|
|
|
|
$r->get("/media")->to("media#list")->name("list_media");
|
|
$r->get("/media/<media_id:num>")->to("media#show")->name("show_media");
|
|
$r->get("/media/similar/<media_id:num>")->to("media#similar")
|
|
->name("search_similar_media");
|
|
|
|
$r->get("/tag")->to("tags#show")->name("show_tag");
|
|
$r->get("/tags")->to("tags#list")->name("list_tags");
|
|
$r->get("/search")->to("tags#search")->name("search_tags");
|
|
|
|
$r->get("/random/media")->to("media#random")->name("random_media");
|
|
$r->get("/random/tag")->to("tags#random")->name("random_tag");
|
|
}
|
|
|
|
1;
|