App-PerinciUtils
view release on metacpan or search on metacpan
script/peri-htserve view on Meta::CPAN
my %args = @_;
my $val = $args{value};
my $args = $args{args};
# put it here for function later
$args->{-modules} //= [];
$args->{-packages} //= [];
if ($val =~ /(.+::)\*$/) {
log_debug('Listing all modules in %s ...', $val);
my $res = list_modules($1, {list_modules=>1});
push @{ $args->{-modules} }, sort keys %$res;
} elsif ($val =~ s/^\+//) {
push @{ $args->{-packages} }, $val;
} else {
push @{ $args->{-modules} }, $val;
}
},
},
riap_access_log_path => {
schema => ['str' => {}],
summary => 'Path for Riap request access log file',
description => <<'_',
Default is ~/peri-htserve-riap_access.log
_
},
riap_access_log_size => {
schema => ['int' => {}],
summary => 'Maximum size for Riap request access log file',
description => <<'_',
Default is to use File::Write::Rotate's default (10485760, a.k.a. 10MB).
If size exceeds this, file will be rotated.
_
},
riap_access_log_histories => {
schema => ['int' => {}],
summary => 'Number of old Riap request access log files to keep',
description => <<'_',
Default is to use File::Write::Rotate's default (10).
_
},
server => {
schema => ['str*' => {
in => [qw/Starman Gepok/],
default => 'Gepok',
}],
summary => 'Choose PSGI server',
description => <<'_',
Currently only Starman or Gepok is supported. Default is Gepok.
_
},
starman_host => {
schema => ['str' => {}],
summary => 'Will be passed to Starman',
},
starman_port => {
schema => ['int' => {}],
summary => 'Will be passed to Starman',
},
gepok_http_ports => {
schema => ['str' => {}],
summary => 'Will be passed to Gepok',
},
gepok_https_ports => {
schema => ['str' => {}],
summary => 'Will be passed to Gepok',
},
gepok_unix_sockets => {
schema => ['str' => {}],
summary => 'Will be passed to Gepok',
},
gepok_ssl_key_file => {
schema => ['str' => {}],
summary => 'Will be passed to Gepok',
},
gepok_ssl_cert_file => {
schema => ['str' => {}],
summary => 'Will be passed to Gepok',
},
gepok_start_servers => {
schema => ['int' => {}],
summary => 'Will be passed to Gepok',
},
daemonize => {
schema => ['bool' => {
default => 0,
}],
summary => 'If true, will daemonize into background',
cmdline_aliases => {D=>{}},
},
library => {
schema => ['array' => {
of => 'str*',
}],
summary => 'Add directory to library search path, a la Perl\'s -I',
description => <<'_',
Note that some modules are already loaded before this option takes effect. To
make sure some directories are processed, you can use `PERL5OPT` or explicitly
use `perl` and use its `-I` option.
_
cmdline_aliases => {I=>{}},
cmdline_on_getopt => sub {
my %args = @_;
require lib;
lib->import($args{value});
},
},
use => {
schema => ['array' => of => 'str*'],
summary => 'Use a Perl module, a la Perl\'s -M',
cmdline_aliases => {M=>{}},
cmdline_on_getopt => sub {
my %args = @_;
my $val = $args{value};
script/peri-htserve view on Meta::CPAN
$class = "Perinci::Access::Schemeless::DBI";
$extra_opts{fallback_on_completion} = 1;
require DBI;
my $dbh = DBI->connect(
"dbi:SQLite:dbname=$args{metadb}", "", "", {RaiseError=>1});
$extra_opts{dbh} = $dbh;
} else {
$class = "Perinci::Access::Schemeless";
}
load $class;
$pa = $class->new(
load => 0,
allow_paths => [map {(my $url = $_) =~ s!::!/!g; "/$url"} @pkgs],
%extra_opts,
);
}
my $app = builder {
enable(
"PeriAHS::LogAccess",
dest => $fwr,
);
#enable "PeriAHS::CheckAccess";
if (defined($args{user}) && defined($args{password})) {
enable(
"Auth::Basic",
authenticator => sub {
my ($user, $pass, $env) = @_;
if ($user eq $args{user} && $pass eq $args{password}) {
#$env->{"REMOTE_USER"} = $user; # isn't this already done by webserver?
return 1;
}
return 0;
}
);
}
enable(
"PeriAHS::ParseRequest",
parse_path_info => $args{parse_path_info},
parse_form => $args{parse_form},
parse_reform => $args{parse_reform},
riap_client => $pa,
);
enable (
"PeriAHS::Respond",
enable_logging => $args{enable_logging},
);
};
my @argv;
push @argv, "-s", $server;
my @root_urls; # for hint
if ($server eq 'Starman') {
for (qw/host port/) {
push @argv, "--$_", $args{"starman_$_"} if $args{"starman_$_"};
}
my $host = $args{starman_host} // 'localhost';
my $port = $args{starman_port} // 8080;
push @root_urls, "http://$host:$port/";
} else {
if (!$args{gepok_http_ports} &&
!$args{gepok_https_ports} &&
!$args{gepok_unix_sockets}) {
$args{gepok_http_ports} = "*:5000";
}
for (qw/http_port https_ports unix_sockets
ssl_key_file ssl_cert_file start_servers/) {
push @argv, "--$_", $args{"gepok_$_"} if defined $args{"gepok_$_"};
}
my ($host, $port) = @_;
if ($args{gepok_http_ports}) {
if ($args{gepok_http_ports} =~ /(.+?):(\d+)/) {
$host = $1; $host = 'localhost' if $host eq '*';
$port = $2;
} elsif ($args{gepok_http_ports} =~ /(\d+)/) {
$host = 'localhost';
$port = $1;
}
push @root_urls, "http://$host:$port/";
}
if ($args{gepok_https_ports}) {
if ($args{gepok_https_ports} =~ /(.+?):(\d+)/) {
$host = $1; $host = 'localhost' if $host eq '*';
$port = $2;
} elsif ($args{gepok_https_ports} =~ /(\d+)/) {
$host = 'localhost';
$port = $1;
}
push @root_urls, "https://$host:$port/";
}
if ($args{gepok_unix_sockets}) {
if ($args{gepok_unix_sockets} =~ /(.+?)(?:,|\z)/) {
push @root_urls, "http:$1//";
}
}
}
# display hint for user
if (@root_urls) {
my @ep_urls; # api endpoints
for my $root_url (@root_urls) {
for my $pkg (@pkgs) {
my $pkgp = $pkg; $pkgp =~ s!::!/!g;
push @ep_urls, $root_url . "api/$pkgp/";
}
}
say "Try accessing one of the following URLs with curl/riap/etc:";
print map { "- $_\n" } @ep_urls;
say "";
}
push @argv, "-D" if $args{daemonize};
my $runner = Plack::Runner->new;
$runner->parse_options(@argv);
$runner->run($app);
# never reached though
script/peri-htserve view on Meta::CPAN
Passed to Plack::Middleware::PeriAHS::ParseRequest.
=item B<--parse-reform>
Passed to Plack::Middleware::PeriAHS::ParseRequest.
=item B<--password>=I<s>
Protect with HTTP authentication, specify password.
=item B<--require-json>=I<s>, B<-m>
Require a Perl module, a la Perl's -m (JSON-encoded).
See C<--require>.
=item B<--require>=I<s@>
Require a Perl module, a la Perl's -m.
Can be specified multiple times.
=item B<--riap-access-log-histories>=I<s>
Number of old Riap request access log files to keep.
Default is to use File::Write::Rotate's default (10).
=item B<--riap-access-log-path>=I<s>
Path for Riap request access log file.
Default is ~/peri-htserve-riap_access.log
=item B<--riap-access-log-size>=I<s>
Maximum size for Riap request access log file.
Default is to use File::Write::Rotate's default (10485760, a.k.a. 10MB).
If size exceeds this, file will be rotated.
=item B<--server>=I<s>
Choose PSGI server.
Default value:
"Gepok"
Valid values:
["Starman","Gepok"]
Currently only Starman or Gepok is supported. Default is Gepok.
=item B<--starman-host>=I<s>
Will be passed to Starman.
=item B<--starman-port>=I<s>
Will be passed to Starman.
=item B<--use-json>=I<s>, B<-M>
Use a Perl module, a la Perl's -M (JSON-encoded).
See C<--use>.
=item B<--use>=I<s@>
Use a Perl module, a la Perl's -M.
Can be specified multiple times.
=item B<--user>=I<s>
Protect with HTTP authentication, specify username.
=back
=head2 Configuration options
=over
=item B<--config-path>=I<filename>, B<-c>
Set path to configuration file.
=item B<--config-profile>=I<s>, B<-P>
Set configuration profile to use.
=item B<--no-config>, B<-C>
Do not use any configuration file.
=back
=head2 Environment options
=over
=item B<--no-env>
Do not read environment for default options.
=back
=head2 Output options
=over
=item B<--format>=I<s>
Choose output format, e.g. json, text.
Default value:
undef
script/peri-htserve view on Meta::CPAN
It is recommended, however, that you install modules using L<cpanm-shcompgen>
which can activate shell completion for scripts immediately.
=head2 tcsh
To activate tcsh completion for this script, put:
complete peri-htserve 'p/*/`peri-htserve`/'
in your tcsh startup (e.g. F<~/.tcshrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.
It is also recommended to install L<shcompgen> (see above).
=head2 other shells
For fish and zsh, install L<shcompgen> as described above.
=head1 CONFIGURATION FILE
This script can read configuration files. Configuration files are in the format of L<IOD>, which is basically INI with some extra features.
By default, these names are searched for configuration filenames (can be changed using C<--config-path>): F<~/.config/peri-htserve.conf>, F<~/peri-htserve.conf>, or F</etc/peri-htserve.conf>.
All found files will be read and merged.
To disable searching for configuration files, pass C<--no-config>.
You can put multiple profiles in a single file by using section names like C<[profile=SOMENAME]> or C<[SOMESECTION profile=SOMENAME]>. Those sections will only be read if you specify the matching C<--config-profile SOMENAME>.
You can also put configuration for multiple programs inside a single file, and use filter C<program=NAME> in section names, e.g. C<[program=NAME ...]> or C<[SOMESECTION program=NAME]>. The section will then only be used when the reading program match...
Finally, you can filter a section by environment variable using the filter C<env=CONDITION> in section names. For example if you only want a section to be read if a certain environment variable is true: C<[env=SOMEVAR ...]> or C<[SOMESECTION env=SOME...
List of available configuration parameters:
daemonize (see --daemonize)
enable_logging (see --disable-logging)
format (see --format)
gepok_http_ports (see --gepok-http-ports)
gepok_https_ports (see --gepok-https-ports)
gepok_ssl_cert_file (see --gepok-ssl-cert-file)
gepok_ssl_key_file (see --gepok-ssl-key-file)
gepok_start_servers (see --gepok-start-servers)
gepok_unix_sockets (see --gepok-unix-sockets)
library (see --library)
metadb (see --metadb)
module_or_package (see --module-or-package)
naked_res (see --naked-res)
parse_form (see --parse-form)
parse_path_info (see --parse-path-info)
parse_reform (see --parse-reform)
password (see --password)
require (see --require)
riap_access_log_histories (see --riap-access-log-histories)
riap_access_log_path (see --riap-access-log-path)
riap_access_log_size (see --riap-access-log-size)
server (see --server)
starman_host (see --starman-host)
starman_port (see --starman-port)
use (see --use)
user (see --user)
=head1 ENVIRONMENT
=head2 PERI_HTSERVE_OPT => str
Specify additional command-line options.
=head1 FILES
F<~/.config/peri-htserve.conf>
F<~/peri-htserve.conf>
F</etc/peri-htserve.conf>
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/App-PerinciUtils>.
=head1 SOURCE
Source repository is at L<https://github.com/perlancar/perl-App-PerinciUtils>.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-PerinciUtils>
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<Riap::HTTP>
L<Perinci::Access>, L<Perinci::Access::HTTP::Client>
PSGI servers used: L<Gepok>, L<Starman>
L<Plack::Runner>
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2019, 2017, 2016, 2015 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
( run in 0.930 second using v1.01-cache-2.11-cpan-e93a5daba3e )