update post endpoint, add upload script
This commit is contained in:
14
site.pl
14
site.pl
@@ -61,9 +61,17 @@ post '/blog' => sub {
|
|||||||
my $auth = $c->req->headers->to_hash->{ $ENV{AUTH_HEADER} };
|
my $auth = $c->req->headers->to_hash->{ $ENV{AUTH_HEADER} };
|
||||||
return $c->rendered(400) unless $auth and ( $auth eq $ENV{AUTH_KEY} );
|
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} } )
|
||||||
$c->rendered(204);
|
->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;
|
app->start;
|
||||||
|
|||||||
37
upload.pl
Normal file
37
upload.pl
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user