Skip to content

Commit

Permalink
Merge c254a2a into c5fb889
Browse files Browse the repository at this point in the history
  • Loading branch information
vpeil committed May 13, 2019
2 parents c5fb889 + c254a2a commit 5e71f6e
Show file tree
Hide file tree
Showing 27 changed files with 1,377 additions and 26 deletions.
9 changes: 9 additions & 0 deletions bin/app.pl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Plack::App::Cascade;
use Dancer;
use LibreCat::App;
use Mojo::Server::PSGI;

# setup template paths
config->{engines}{template_toolkit}{INCLUDE_PATH} = librecat->template_paths;
Expand Down Expand Up @@ -64,6 +65,14 @@
Dancer->dance(Dancer::Request->new(env => $_[0]));
});

# mojo app
{
my $server = Mojo::Server::PSGI->new;
my $script = Path::Tiny->new(__FILE__)->sibling('mojo_app.pl')->stringify;
$server->load_app($script);
$app->add($server->to_psgi_app);
}

# setup sessions
my $config = config;
my $session_store_package = is_string($config->{session_store}->{package}) ?
Expand Down
9 changes: 9 additions & 0 deletions bin/mojo_app.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env perl

use Catmandu::Sane;
use Path::Tiny;
use lib path(__FILE__)->parent->parent->child('lib')->stringify;
use LibreCat qw(:load);
use Mojolicious::Commands;

Mojolicious::Commands->start_app('LibreCat::Application');
6 changes: 6 additions & 0 deletions config/catmandu.local.yml-example
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Public location of the application
uri_base: "http://localhost:5001"

# API configuration
# change at least the token secret!
api:
v1:
token_secret: CHANGEME

# Default interface options
default_lang: en
default_page_size: 20
Expand Down
4 changes: 4 additions & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ requires 'Catmandu::XML';
requires 'Search::Elasticsearch', '>=6.00';
requires 'Search::Elasticsearch::Client::6_0';

# Mojolicious
requires 'Mojolicious', '>=7.70';

#Dancer
requires 'Dancer';
requires 'Dancer::Plugin';
Expand Down Expand Up @@ -83,6 +86,7 @@ requires 'Clone';
requires 'Code::TidyAll', 0;
requires 'Config::Onion', '>= 1.007';
requires 'Crypt::Digest::MD5';
requires 'Crypt::JWT';
requires 'Crypt::SSLeay';
requires 'Data::Uniqid';
requires 'DateTime';
Expand Down
11 changes: 10 additions & 1 deletion lib/LibreCat.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use List::MoreUtils qw(any);
use String::CamelCase qw(camelize);
use POSIX qw(strftime);
use LibreCat::Hook;
use LibreCat::Token;
use Catmandu::Fix;
use Catmandu;
use Exporter::Shiny;
Expand Down Expand Up @@ -63,7 +64,7 @@ sub _exporter_expand_sub {
return $name => sub {state $memo = $class->instance};
}
if (any {$_ eq $name}
qw(log config fixer hook queue model root_path searcher timestamp))
qw(log config fixer hook queue model root_path searcher timestamp token))
{
return $name =>
sub {state $memo = $class->instance; $memo->$name(@_)};
Expand Down Expand Up @@ -115,6 +116,7 @@ has _model_accessors => (is => 'ro', init_arg => undef, default => sub {+{}});
has _hook_instances => (is => 'ro', init_arg => undef, default => sub {+{}});
has searcher => (is => 'lazy');
has queue => (is => 'lazy');
has token => (is => 'lazy');

sub BUILD {
my ($self) = @_;
Expand Down Expand Up @@ -371,6 +373,11 @@ sub _build_queue {
require_package('LibreCat::JobQueue')->new;
}

sub _build_token {
my ($self) = @_;
LibreCat::Token->new(secret => $self->config->{api}{v1}{token_secret});
}

sub timestamp {
my ($self, $time) = @_;
$time //= time;
Expand Down Expand Up @@ -510,6 +517,8 @@ variable, in which case the C<layers.yml> file will be ignored.
=head2 timestamp($time)
=head2 token
=head2 root_path
=head2 layer_paths
Expand Down
48 changes: 24 additions & 24 deletions lib/LibreCat/App/Api/Route/FileStore.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ sub do_error {
send_error({code =>, $code, error => $msg}, $http_code);
}

prefix '/librecat/api' => sub {
prefix '/api/v1/file' => sub {

=head2 GET /librecat/api/filestore
=head2 GET /api/v1/file
Return a text stream of all container identifier in the repository.
E.g.
$ curl -H "Content-Type: application/json" -X GET "http://localhost:5001/librecat/api/filestore"
$ curl -H "Content-Type: application/json" -X GET "http://localhost:5001/api/v1/file"
000000122
000000006
000000010
Expand All @@ -38,7 +38,7 @@ E.g.
=cut

get '/filestore' => sub {
get '/' => sub {
my $index = h->get_file_store()->index;

send_file(
Expand Down Expand Up @@ -76,13 +76,13 @@ E.g.
);
};

=head2 GET /librecat/api/filestore/:key
=head2 GET /api/v1/file/:key
Return the content of a container in JSON format
E.g.
$ curl -H "Content-Type: application/json" -X GET "http://localhost:5001/librecat/api/filestore/000000008"
$ curl -H "Content-Type: application/json" -X GET "http://localhost:5001/api/v1/file/000000008"
{
"files" : [
{
Expand Down Expand Up @@ -137,13 +137,13 @@ E.g.
}
};

=head2 GET /librecat/api/filestore/:key/:filename
=head2 GET /api/v1/file/:key/:filename
Return the binary content of a file in a container
E.g.
$ curl -H "Content-Type: application/json" -X GET "http://localhost:5001/librecat/api/filestore/000000008/rprogramming.pdf"
$ curl -H "Content-Type: application/json" -X GET "http://localhost:5001/api/v1/file/000000008/rprogramming.pdf"
<... binary data ...>
=cut
Expand Down Expand Up @@ -192,13 +192,13 @@ E.g.
);
};

=head2 DEL /librecat/api/filestore/:key
=head2 DEL /api/v1/file/:key
Delete a container from the repository
E.g.
$ curl -H "Content-Type: application/json" -X DELETE "http://localhost:5001/librecat/api/filestore/000000008"
$ curl -H "Content-Type: application/json" -X DELETE "http://localhost:5001/api/v1/file/000000008"
{ "ok": "1"}
=cut
Expand All @@ -219,13 +219,13 @@ E.g.
}
};

=head2 DEL /librecat/api/filestore/:key/:filename
=head2 DEL /api/v1/file/:key/:filename
Delete a file in a container from the repository
E.g.
$ curl -H "Content-Type: application/json" -X DELETE "http://localhost:5001/librecat/api/filestore/000000008/rprogramming.pdf"
$ curl -H "Content-Type: application/json" -X DELETE "http://localhost:5001/api/v1/file/000000008/rprogramming.pdf"
{ "ok": "1"}
=cut
Expand Down Expand Up @@ -256,13 +256,13 @@ E.g.
}
};

=head2 POST /librecat/api/filestore/:key
=head2 POST /api/v1/file/:key
Add a file to a container in the repository
E.g.
$ curl -H "Content-Type: application/json" -F file=@rpogramming.pdf -X POST "http://localhost:5001/librecat/api/filestore/000000008"
$ curl -H "Content-Type: application/json" -F file=@rpogramming.pdf -X POST "http://localhost:5001/api/v1/file/000000008"
{ "ok": "1"}
=cut
Expand Down Expand Up @@ -291,18 +291,18 @@ E.g.
return {ok => 1};
};

=head2 GET /librecat/api/access/:key/:filename/thumbnail
=head2 GET /api/v1/thumbnail/:key/:filename
Return the binary thumbail content of a file in a container
E.g.
$ curl -H "Content-Type: application/json" -X GET "http://localhost:5001/librecat/api/access/000000008/rprogramming.pdf/thumbnail"
$ curl -H "Content-Type: application/json" -X GET "http://localhost:5001/api/v1/thumbnail/000000008/rprogramming.pdf"
<... binary data ...>
=cut

get '/access/:key/:filename/thumbnail' => sub {
get '/thumbnail/:key/:filename' => sub {
my $key = param('key');

# For now stay backwards compatible and keep one thumbnail per container...
Expand All @@ -318,7 +318,7 @@ E.g.
my $file = $files->get($filename);

return Dancer::send_file(
'public/images/thumbnail_dummy.png',
'public/images_dummy.png',
system_path => 1,
filename => 'thumbnail_dummy.png'
) unless $file;
Expand Down Expand Up @@ -353,17 +353,17 @@ E.g.
);
};

=head2 POST /librecat/api/access/:key/:filename/thumbnail
=head2 POST /api/v1/thumbnail/:key/:filename
Create a thumbail for a file in a container
E.g.
$ curl -H "Content-Type: application/json" -X POST "http://localhost:5001/librecat/api/access/000000008/rprogramming.pdf/thumbnail"
$ curl -H "Content-Type: application/json" -X POST "http://localhost:5001/api/v1/thumbnail/000000008/rprogramming.pdf"
=cut

post '/access/:key/:filename/thumbnail' => sub {
post '/thumbnail/:key/:filename' => sub {
my $key = param('key');
my $filename = param('filename');

Expand All @@ -381,18 +381,18 @@ E.g.
$response;
};

=head2 DEL /librecat/api/access/:key/:filename/thumbnail
=head2 DEL /api/v1/thumbnail/:key/:filename
Delete a thumbnail in a container
E.g.
$ curl -H "Content-Type: application/json" -X DELETE "http://localhost:5001/librecat/api/access/000000008/rprogramming.pdf/thumbnail"
$ curl -H "Content-Type: application/json" -X DELETE "http://localhost:5001/api/v1/thumbnail/000000008/rprogramming.pdf"
{ "ok": "1"}
=cut

del '/access/:key/:filename/thumbnail' => sub {
del '/thumbnail/:key/:filename' => sub {
my $key = param('key');

my $thumbnailer_package
Expand Down
41 changes: 41 additions & 0 deletions lib/LibreCat/Application.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package LibreCat::Application;

use Catmandu::Sane;
use JSON::MaybeXS qw(decode_json);
use Mojo::Base 'Mojolicious';
use namespace::clean;

sub startup {
my ($self) = @_;

# add content types
$self->types->type(yml => 'text/x-yaml');

# controller namespace
my $r = $self->routes;
$r->namespaces(['LibreCat::Controller']);

# hardcoded for now
$self->plugin('LibreCat::Api');

# helpers
$self->helper(maybe_decode_json => sub {
my ($self, $json) = @_;
try {
decode_json($json);
} catch {
};
});
}

1;

__END__
=pod
=head1 NAME
LibreCat::Application -
=cut
3 changes: 2 additions & 1 deletion lib/LibreCat/Cmd/generate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ sub command_opt_spec {
sub command {
my ($self, $opts, $args) = @_;

my $commands = qr/^(package\.json|forms|departments|cleanup)$/;
my $commands
= qr/^(package\.json|forms|departments|cleanup|openapi.yml)$/;

unless (@$args) {
$self->usage_error("should be one of $commands");
Expand Down

0 comments on commit 5e71f6e

Please sign in to comment.