From 6dfabdf29fb84433397ee2790dffdd58e27eb271 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 5 Mar 2023 17:23:13 +0000 Subject: [PATCH] backend: add / --- lib/PoorBooru.pm | 10 +++++++-- lib/PoorBooru/Controller/Gallery.pm | 33 +++++++++++++++++++++++++++++ poorbooru.conf | 4 ++++ templates/gallery.html.ep | 9 ++++++++ templates/layouts/main.html.ep | 29 +++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 lib/PoorBooru/Controller/Gallery.pm create mode 100644 templates/gallery.html.ep create mode 100644 templates/layouts/main.html.ep diff --git a/lib/PoorBooru.pm b/lib/PoorBooru.pm index d20d767..ed894e9 100644 --- a/lib/PoorBooru.pm +++ b/lib/PoorBooru.pm @@ -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; diff --git a/lib/PoorBooru/Controller/Gallery.pm b/lib/PoorBooru/Controller/Gallery.pm new file mode 100644 index 0000000..c0730b7 --- /dev/null +++ b/lib/PoorBooru/Controller/Gallery.pm @@ -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; diff --git a/poorbooru.conf b/poorbooru.conf index c53fe99..7bdd2b8 100644 --- a/poorbooru.conf +++ b/poorbooru.conf @@ -3,4 +3,8 @@ # Generate with "openssl rand -hex 32". pack("H*", "a0b1..."), ], + + poorbooru_api => "http://localhost:3001", + + seaweedfs_master => "http://localhost:9333", } diff --git a/templates/gallery.html.ep b/templates/gallery.html.ep new file mode 100644 index 0000000..4fa3cb2 --- /dev/null +++ b/templates/gallery.html.ep @@ -0,0 +1,9 @@ +% layout "main"; + + diff --git a/templates/layouts/main.html.ep b/templates/layouts/main.html.ep new file mode 100644 index 0000000..019cb60 --- /dev/null +++ b/templates/layouts/main.html.ep @@ -0,0 +1,29 @@ + + + + + + PoorBooru - <%= title %> + <%= stylesheet "/css/style.css" %> + + +
+ +
+
+
+

<%= title %>

+
<%= content %>
+
+
+ + +