Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a REST api driven by WebAPI::DBIC.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
=head1 NAME | ||
api.psgi - REST API for lystyng.com. | ||
=head1 SYNOPSIS | ||
$ plackup api.psgi | ||
... open a web browser on port 5000 to browse your new API | ||
The API provided by this .psgi file will be read-only unless the | ||
C<WEBAPI_DBIC_WRITABLE> env var is true. | ||
For details on the C<WEBAPI_DBIC_HTTP_AUTH_TYPE> env var and security issues | ||
see C<http_auth_type> in L<WebAPI::DBIC::Resource::Role::DBICAuth>. | ||
=cut | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Plack::Builder; | ||
use Plack::App::File; | ||
use WebAPI::DBIC::WebApp; | ||
use Alien::Web::HalBrowser; | ||
|
||
use FindBin '$Bin'; | ||
use lib "$Bin/../lib"; | ||
use Lystyng::Schema; | ||
|
||
my $hal_app = Plack::App::File->new( | ||
root => Alien::Web::HalBrowser->dir | ||
)->to_app; | ||
|
||
$ENV{WEBAPI_DBIC_HTTP_AUTH_TYPE} //= 'none'; | ||
|
||
my $schema = Lystyng::Schema->get_schema; | ||
|
||
my $app = WebAPI::DBIC::WebApp->new({ | ||
routes => [ map( $schema->source($_), $schema->sources) ] | ||
})->to_psgi_app; | ||
|
||
my $app_prefix = "/webapi-dbic"; | ||
|
||
builder { | ||
enable "SimpleLogger"; # show on STDERR | ||
|
||
mount "$app_prefix/" => builder { | ||
mount "/browser" => $hal_app; | ||
mount "/" => $app; | ||
}; | ||
|
||
# root redirect for discovery - redirect to API | ||
mount "/" => sub { [ 302, [ Location => "$app_prefix/" ], [ ] ] }; | ||
}; |