| 1 | # ABSTRACT: HiD 'server' subcmd - start up a Plack-based web server for your site | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package HiD::App::Command::server; | ||||||||||||||||||||||||||||
| 5 | 4 | $HiD::App::Command::server::VERSION = '1.5'; | |||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | extends 'HiD::App::Command'; | |||||||||||||||||||||||||||
| 8 | 4 | with 'HiD::Role::PublishesDrafts'; | |||||||||||||||||||||||||||
| 9 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use warnings qw/ FATAL utf8 /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use open qw/ :std :utf8 /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use charnames qw/ :full /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use feature qw/ unicode_strings /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use Plack::Runner; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 4 | has auto_refresh => ( | |||||||||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||||||||||
| 26 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 27 | traits => [ 'Getopt' ], | ||||||||||||||||||||||||||||
| 28 | cmd_aliases => [ qw/ auto A / ], | ||||||||||||||||||||||||||||
| 29 | documentation => 'auto re-publish when source changes, Default=False', | ||||||||||||||||||||||||||||
| 30 | lazy => 1, | ||||||||||||||||||||||||||||
| 31 | default => 0, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 4 | has port => ( | |||||||||||||||||||||||||||
| 36 | is => 'ro' , | ||||||||||||||||||||||||||||
| 37 | isa => 'Int' , | ||||||||||||||||||||||||||||
| 38 | traits => [ 'Getopt' ] , | ||||||||||||||||||||||||||||
| 39 | cmd_aliases => 'p' , | ||||||||||||||||||||||||||||
| 40 | documentation => 'port to run the server on. Default=5000' , | ||||||||||||||||||||||||||||
| 41 | lazy => 1 , | ||||||||||||||||||||||||||||
| 42 | builder => '_build_port' , | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub _build_port { | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_port: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | return $self->{port} if defined $self->{port}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | my $config = $self->config; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | return $self->config->{server_port} // 5000; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->config->{"server_port"} // 5000
MC/DC0%0/1
$self->config->{"server_port"} // 5000
$self->config->{"server_port"}
Truth table0%0/2
A
A= $self->config->{"server_port"}
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub _run { | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my( $self , $opts , $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_run: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $config = $self->config; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ( $self->clean ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $config->{clean_destination} = 1; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ( $self->publish_drafts ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | $config->{publish_drafts} = 1; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | $self->publish; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $app = HiD::Server->new( root => $self->destination )->to_app; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my %args = ( -p => $self->port ); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # auto refresh | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( $self->auto_refresh ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | my @dirs = map { $self->hid->get_config($_) } qw/ include_dir layout_dir posts_dir /; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | for my $dir (qw/pages regular_files/) { | |||||||||||||||||||||||||||
| 77 | 0 | push @dirs, map { $_->input_filename } @{ $self->hid->$dir }; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | $args{'-R'} = join ',', @dirs; | |||||||||||||||||||||||||||
| 81 | 0 | $args{'-r'} = 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $original_app = $app; | |||||||||||||||||||||||||||
| 84 | $app = \sub { | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | say 'Rebuild ... '; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 86 | 0 | $self->publish; | |||||||||||||||||||||||||||
| 87 | 0 | $original_app; | |||||||||||||||||||||||||||
| 88 | 0 | }; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $runner = Plack::Runner->new; | |||||||||||||||||||||||||||
| 92 | 0 | $runner->parse_options(%args); | |||||||||||||||||||||||||||
| 93 | 0 | $runner->run($app); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | package # hide... | ||||||||||||||||||||||||||||
| 99 | HiD::Server; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 4 | ▶ | use parent 'Plack::App::File'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub locate_file { | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my ($self, $env) = @_; | ||||||||||||||||||||||||||
|
Subroutine
locate_file: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my $path = $env->{PATH_INFO} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$env->{'PATH_INFO'} || ''
MC/DC0%0/1
$env->{'PATH_INFO'} || ''
$env->{'PATH_INFO'}
Truth table0%0/2
A
A= $env->{'PATH_INFO'}
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $path =~ s|^/|| unless $path eq '/'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ( -e -d $path and $path !~ m|/$| ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e -d $path and not $path =~ m[/$]u
MC/DC0%0/2
-e -d $path and not $path =~ m[/$]u
-e -d $path
not $path =~ m[/$]u
Truth table0%0/3
A and not(B)
A= -e -d $pathB= not $path =~ m[/$]u
| |||||||||||||||||||||||||||||
| 111 | 0 | $path .= '/'; | |||||||||||||||||||||||||||
| 112 | 0 | $env->{PATH_INFO} .= '/'; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | $env->{PATH_INFO} .= 'index.html' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$path and $path =~ m[/$]u
MC/DC0%0/2
$path and $path =~ m[/$]u
$path
$path =~ m[/$]u
Truth table0%0/3
A and B
A= $pathB= $path =~ m[/$]u
| |||||||||||||||||||||||||||||
| 116 | if ( $path && $path =~ m|/$| ); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | return $self->SUPER::locate_file( $env ); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 4 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =pod | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 NAME | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | HiD::App::Command::server - HiD 'server' subcmd - start up a Plack-based web server for your site | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | $ ../bin/hid server | ||||||||||||||||||||||||||||
| 136 | HTTP::Server::PSGI: Accepting connections at http://0:5000/ | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Start a Plack-based web server that serves your C<destination> directory. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head2 auto_refresh | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Automatically refresh result when source file/dir changed, just likey jekyll | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 port | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Port number to bind. Defaults to 5000. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | See L<HiD::App::Command> for additional command line options supported by all | ||||||||||||||||||||||||||||
| 155 | sub commands. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 VERSION | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | version 1.5 | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | John SJ Anderson <genehack@genehack.org> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | This software is copyright (c) 2014 by John SJ Anderson. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 170 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||