backend: add /

This commit is contained in:
Lucas 2023-03-05 17:23:13 +00:00
parent 302ec269d5
commit 6dfabdf29f
5 changed files with 83 additions and 2 deletions

View File

@ -4,6 +4,7 @@ use strict;
use warnings;
use Mojo::Base "Mojolicious";
use SeaweedFS::Client;
sub startup ($self)
{
@ -12,9 +13,14 @@ sub startup ($self)
my $config = $self->plugin("Config");
$self->secrets($config->{secrets});
$self->plugin(Mount => {
"/api/v0" => $self->home->child("script", "poorbooru-api-v0"),
$self->helper(seaweed => sub {
state $client =
SeaweedFS::Client->new($config->{seaweedfs_master})
});
my $r = $self->routes;
$r->get("/")->to("gallery#index");
}
1;

View File

@ -0,0 +1,33 @@
package PoorBooru::Controller::Gallery;
use v5.36;
use strict;
use warnings;
use Mojo::Base "Mojolicious::Controller";
use Mojo::JSON qw(decode_json);
sub index ($self)
{
$self->title("Pooru~");
my $v = $self->validation;
my $page = $v->optional("page")->num(1, undef)->param // 1;
return $self->render(
status => 404,
) if $v->has_error;
my $poorbooru_api = $self->config("poorbooru_api");
my $ua = Mojo::UserAgent->new;
my $res = $ua->get("$poorbooru_api/media" => form => { page => $page })->result;
return $self->render(status => $res->code) if $res->is_error;
my @media = map +{
media_src => $self->seaweed->url_for(SeaweedFS::FID->from_bin($_->{seaweedfs_fid})),
}, @{$res->json->{media}};
return $self->render(template => "gallery", media => \@media);
};
1;

View File

@ -3,4 +3,8 @@
# Generate with "openssl rand -hex 32".
pack("H*", "a0b1..."),
],
poorbooru_api => "http://localhost:3001",
seaweedfs_master => "http://localhost:9333",
}

View File

@ -0,0 +1,9 @@
% layout "main";
<div class="flex-c-horizontal flex-c-wrap gallery">
% for my $m (@$media) {
<%= link_to $m->{media_src} => begin %>
<%= image $m->{media_src}, class => "gallery-image" %>
<% end %>
% }
</div>

View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>PoorBooru - <%= title %></title>
<%= stylesheet "/css/style.css" %>
</head>
<body class="bg-default fg-default flex-c-vertical">
<header>
<nav class="viewport flex-c-horizontal main-nav-link-gap">
<%= link_to "Pooru~" => "/" => (class => "main-nav-link") %>
<%= link_to "random" => "/random" => (class => "main-nav-link") %>
<%= link_to "tags" => "/tags" => (class => "main-nav-link") %>
<span class="flex-i-fullsize"><!-- spacer --></span>
<%= link_to login => "/login" => (class => "main-nav-link") %>
</nav>
</header>
<main class="border-bottom border-top border-accent flex-i-fullsize flex-c-vertical">
<div class="viewport flex-i-fullsize flex-c-vertical">
<h1><%= title %></h1>
<div class="viewport flex-i-fullsize"><%= content %></div>
</div>
</main>
<footer class="text-center">
<p class="viewport text-small">Powered by <a href="https://www.openbsd.org">OpenBSD</a> and <a href="https://mojolicious.org/">Mojolicious</a></p>
</footer>
</body>
</html>