update post endpoint, add upload script

This commit is contained in:
Rawley Fowler
2023-07-10 12:59:07 -06:00
parent 1442f95a72
commit 8f79ecba00
2 changed files with 48 additions and 3 deletions

12
site.pl
View File

@@ -61,9 +61,17 @@ post '/blog' => sub {
my $auth = $c->req->headers->to_hash->{ $ENV{AUTH_HEADER} };
return $c->rendered(400) unless $auth and ( $auth eq $ENV{AUTH_KEY} );
$c->db->insert( 'posts', $c->req->json );
if ( my $old_post =
$c->db->select( 'posts', ['id'], { slug => $c->req->json->{slug} } )
->hash )
{
$c->db->update( 'posts', $c->req->json, { id => $old_post->{id} } );
$c->rendered(204);
}
else {
$c->db->insert( 'posts', $c->req->json );
$c->rendered(201);
}
};
app->start;

37
upload.pl Normal file
View File

@@ -0,0 +1,37 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::File;
use Carp qw(croak carp);
my $file = Mojo::File->new(shift)
|| croak q{Expected $1 to be a file path, or couldn't open file};
my $ua = Mojo::UserAgent->new;
my $hn = shift || croak q{Expected $2 to be header name};
my $pw = shift || croak q{Expected $3 to be password};
my $ti = shift || croak q{Expected $4 to be title};
my $sl = shift || croak q{Expected $5 to be slug};
my $uri =
$ENV{BLOG_DEVELOPMENT} ? 'http://localhost:3000' : 'https://rawley.xyz';
my $j = {
content => $file->slurp,
title => $ti,
slug => $sl
};
my $r =
$ua->post( ( $uri . '/blog' ) => { $hn => $pw } => json => $j )->result;
if ( $r->is_success ) {
print 'Upload successful';
}
else {
carp q{Failed to upload } . $r->code;
exit 1;
}