#!perl

use 5.010;
use strict;
use warnings;

our $DATE = '2017-07-11'; # DATE
our $VERSION = '0.07'; # VERSION

use File::ShareDir qw(dist_file);
use File::Temp qw(tempfile);
use Getopt::Long qw(:config pass_through auto_help auto_version);
use YAML::Syck qw(DumpFile);

my %Opts;

GetOptions(
    'user=s' => \$Opts{user},
    'pass=s' => \$Opts{pass},
);

die "Please supply DBI dsn to export\n" unless @ARGV;
my $dsn = shift @ARGV;

my (undef, $confpath) = tempfile();
#say "D:confpath=$confpath";
DumpFile($confpath, {
    riap_access_log_dir => $ENV{HOME},
    riap_access_log_prefix => "dbi2http-riap_access.log",
    #riap_access_log_size => 10485760,
    #riap_access_log_histories => 10,
    db_dsn => $dsn,
    db_user => $Opts{user},
    db_password => $Opts{pass},
});

$ENV{DBI2HTTP_CONFIG_PATH} = $confpath;
my @cmd = ("plackup", dist_file("Plack-App-dbi2http", "www/dbi2http.psgi"),
    @ARGV);
#say "D:cmd=", join(" ", @cmd);
system @cmd;
my $exit = $?;

unlink $confpath;
exit $exit;

# ABSTRACT: Export DBI database as HTTP API (Riap::HTTP)
# PODNAME: dbi2http

__END__

=pod

=encoding UTF-8

=head1 NAME

dbi2http - Export DBI database as HTTP API (Riap::HTTP)

=head1 VERSION

This document describes version 0.07 of dbi2http (from Perl distribution Plack-App-dbi2http), released on 2017-07-11.

=head1 SYNOPSIS

Simplest usage (will run an HTTP server):

 % dbi2http dbi:SQLite:dbname=/path/to/your.db

To specify DBI user and password:

 % dbi2http dbi:mysql:database=foo --user dbuser --pass dbpass

Unknown options will be passed to plackup:

 % dbi2http dbi:mysql:test --user dbuser --pass dbpass --port 5001 -s Starman

See other options:

 % dbi2http --help

After the server is running, you can query it from another console, e.g. via
curl:

 % curl http://localhost:5000/list_tables
 countries
 continents

 % curl http://localhost:5000/list_columns?table=countries
 id
 ind_name
 eng_name
 tags

 % curl 'http://localhost:5000/list_columns?table=countries&detail=1&-riap-fmt=json-pretty'
 [
    200,
   "OK",
   [
      {
         "pos" : 1,
         "name" : "id",
         "type" : "text"
      },
      {
         "name" : "ind_name",
         "pos" : 2,
         "type" : "text"
      },
      {
         "type" : "text",
         "name" : "eng_name",
         "pos" : 3
      },
      {
         "pos" : 4,
         "name" : "tags",
         "type" : "text"
      }
   ]
 ]

 % curl 'http://localhost:5000/list_rows?table=countries'
 China   cn      Cina    panda
 Indonesia       id      Indonesia       bali,tropical
 Singapore       sg      Singapura       tropical
 United States of America        us      Amerika Serikat

 % curl 'http://localhost:5000/list_rows?table=countries&-riap-fmt=text-pretty'
 .-----------------------------------------------------------------.
 | eng_name                   id   ind_name          tags          |
 |                                                                 |
 | China                      cn   Cina              panda         |
 | Indonesia                  id   Indonesia         bali,tropical |
 | Singapore                  sg   Singapura         tropical      |
 | United States of America   us   Amerika Serikat                 |
 `-----------------------------------------------------------------'

Or use L<App::riap>, a client shell for Riap (with filesystem-like API browsing
and shell tab completion):

 % riap http://localhost:5000/
 riap /> ls
 list_columns
 list_rows
 list_tables

 riap /> list_tables
 countries
 continents

 riap /> list_columns --table countries --detail

 riap /> list_rows --table countries

=head1 DESCRIPTION

This is a one-liner shortcut to export your DBI database as HTTP API (using
Riap::HTTP protocol). It is a wrapper to a PSGI app (L<Plack::App::dbi2http>)
which does the heavy-lifting. So what this script does is basically create a
config file on-the-fly and run B<plackup> to serve the PSGI application.

=head1 USAGE

 % dbi2http [options] <DBI dsn>

=head1 OPTIONS

Unknown options will be passed to B<plackup>.

=head2 --user => str

Supply DBI username. Some driver do not need it (e.g. SQLite).

=head2 --pass => str

Supply DBI password. Some driver do not need it (e.g. SQLite).

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/Plack-App-dbi2http>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-Plack-App-dbi2http>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Plack-App-dbi2http>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 SEE ALSO

L<Plack::App::dbi2http>, L<Riap>, L<Riap::HTTP>

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017, 2015, 2014 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut