-
Notifications
You must be signed in to change notification settings - Fork 10
/
webapi-dbic-any.psgi
57 lines (41 loc) · 1.64 KB
/
webapi-dbic-any.psgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
=head1 NAME
webapi-dbic-any.psgi - instant WebAPI::DBIC browser for any DBIx::Class schema
=head1 SYNOPSIS
$ export WEBAPI_DBIC_SCHEMA=Foo::Bar # your own schema
$ export WEBAPI_DBIC_HTTP_AUTH_TYPE=none # recommended
$ export DBI_DSN=dbi:Driver:... # your own database
$ export DBI_USER=... # for initial connection, if needed
$ export DBI_PASS=... # for initial connection, if needed
$ plackup webapi-dbic-any.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;
my $hal_app = Plack::App::File->new(
root => Alien::Web::HalBrowser->dir
)->to_app;
my $schema_class = $ENV{WEBAPI_DBIC_SCHEMA}
or die "WEBAPI_DBIC_SCHEMA env var not set";
eval "require $schema_class" or die "Error loading $schema_class: $@";
my $schema = $schema_class->connect(); # uses DBI_DSN, DBI_USER, DBI_PASS env vars
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/" ], [ ] ] };
};