| 1 | # ABSTRACT: HiD 'server' subcmd - start up a Plack-based web server for your site | ||||||||||||
| 2 | |||||||||||||
| 3 | |||||||||||||
| 4 | package HiD::App::Command::server; | ||||||||||||
| 5 | 3 | our $AUTHORITY = 'cpan:GENEHACK'; | |||||||||||
| 6 | 3 | $HiD::App::Command::server::VERSION = '1.96'; | |||||||||||
| 7 | 5 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | extends 'HiD::App::Command'; | |||||||||||
| 9 | 3 | with 'HiD::Role::PublishesDrafts'; | |||||||||||
| 10 | 7 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 7 | ▶ | use 5.014; # strict, unicode_strings | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 7 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 5 | ▶ | use autodie; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 5 | ▶ | use warnings qw/ FATAL utf8 /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 5 | ▶ | use open qw/ :std :utf8 /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 5 | ▶ | use charnames qw/ :full /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 5 | ▶ | use feature qw/ unicode_strings /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | 5 | ▶ | use Class::Load qw/ try_load_class /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 5 | ▶ | use Plack::Builder; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 5 | ▶ | use Plack::Runner; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | |||||||||||||
| 24 | 5 | ▶ | use HiD::Server; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | 5 | ▶ | use HiD::Server::Handler; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 5 | ▶ | use HiD::Server::Loader; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | |||||||||||||
| 29 | 3 | has auto_refresh => ( | |||||||||||
| 30 | is => 'ro', | ||||||||||||
| 31 | isa => 'Bool', | ||||||||||||
| 32 | traits => [ 'Getopt' ], | ||||||||||||
| 33 | cmd_aliases => [ qw/ auto A / ], | ||||||||||||
| 34 | documentation => 'auto re-publish when source changes, Default=False', | ||||||||||||
| 35 | lazy => 1, | ||||||||||||
| 36 | default => 0, | ||||||||||||
| 37 | ); | ||||||||||||
| 38 | |||||||||||||
| 39 | |||||||||||||
| 40 | 3 | has clean => ( | |||||||||||
| 41 | is => 'ro' , | ||||||||||||
| 42 | isa => 'Bool' , | ||||||||||||
| 43 | cmd_aliases => 'C' , | ||||||||||||
| 44 | traits => [ 'Getopt' ] , | ||||||||||||
| 45 | ); | ||||||||||||
| 46 | |||||||||||||
| 47 | |||||||||||||
| 48 | 3 | has debug => ( | |||||||||||
| 49 | is => 'ro' , | ||||||||||||
| 50 | isa => 'Bool' , | ||||||||||||
| 51 | cmd_aliases => 'D' , | ||||||||||||
| 52 | traits => [ 'Getopt' ] , | ||||||||||||
| 53 | ); | ||||||||||||
| 54 | |||||||||||||
| 55 | |||||||||||||
| 56 | 3 | has port => ( | |||||||||||
| 57 | is => 'ro' , | ||||||||||||
| 58 | isa => 'Int' , | ||||||||||||
| 59 | traits => [ 'Getopt' ] , | ||||||||||||
| 60 | cmd_aliases => 'p' , | ||||||||||||
| 61 | documentation => 'port to run the server on. Default=5000' , | ||||||||||||
| 62 | lazy => 1 , | ||||||||||||
| 63 | builder => '_build_port' , | ||||||||||||
| 64 | ); | ||||||||||||
| 65 | |||||||||||||
| 66 | sub _build_port { | ||||||||||||
| 67 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_build_port: not called CC=3 SCAR=24.8
| |||||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | return $self->{port} if defined $self->{port}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | |||||||||||||
| 71 | 0 | my $config = $self->config; | |||||||||||
| 72 | 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"}
| |||||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | sub _run { | ||||||||||||
| 76 | 0 | ▶ | my( $self , $opts , $args ) = @_; | ||||||||||
|
Subroutine
_run: not called CC=7 SCAR=40.3
| |||||||||||||
| 77 | |||||||||||||
| 78 | 0 | my $config = $self->config; | |||||||||||
| 79 | 0 | ▶ | if ( $self->clean ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 0 | $config->{clean_destination} = 1; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | 0 | ▶ | if ( $self->publish_drafts ){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 0 | $config->{publish_drafts} = 1; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 0 | my $app = HiD::Server->new( root => $self->destination )->to_app; | |||||||||||
| 88 | |||||||||||||
| 89 | 0 | ▶ | if ( $self->debug ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | ▶ | if ( try_load_class( 'Plack::Middleware::DebugLogging' )) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | $app = builder { | ||||||||||||
| 92 | 0 | ▶ | enable_if { $ENV{PLACK_ENV} eq 'development' } 'DebugLogging'; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 93 | 0 | $app; | |||||||||||
| 94 | 0 | }; | |||||||||||
| 95 | } | ||||||||||||
| 96 | else { | ||||||||||||
| 97 | 0 | print STDERR "*** Plack::Middleware::DebugLogging required for debug logging.\n"; | |||||||||||
| 98 | 0 | print STDERR "*** Continuing with normal logging.\n"; | |||||||||||
| 99 | } | ||||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 0 | my $runner = Plack::Runner->new; | |||||||||||
| 103 | 0 | my %args = ( '-p' => $self->port ); | |||||||||||
| 104 | 0 | $runner->parse_options(%args); | |||||||||||
| 105 | |||||||||||||
| 106 | # auto refresh | ||||||||||||
| 107 | 0 | ▶ | if ( $self->auto_refresh ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | 0 | my @dirs = map { $self->hid->get_config($_) } qw/ include_dir layout_dir posts_dir /; | |||||||||||
| 109 | |||||||||||||
| 110 | 0 | for my $dir (qw/pages regular_files/) { | |||||||||||
| 111 | 0 | push @dirs, map { $_->input_filename } @{ $self->hid->$dir }; | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | # FIXME wish there was a better way to override stuff in Plack::Runner. >_< | ||||||||||||
| 115 | 0 | $runner->{server} = '+HiD::Server::Handler'; | |||||||||||
| 116 | 0 | $runner->{loader} = '+HiD::Server::Loader'; | |||||||||||
| 117 | 0 | $runner->loader->watch(@dirs); | |||||||||||
| 118 | |||||||||||||
| 119 | 0 | push @{$runner->{options}} , ( hid => $self ); | |||||||||||
| 120 | |||||||||||||
| 121 | # no need to explicitly ->publish() here, as that will happen inside | ||||||||||||
| 122 | # HiD::Server::Loader, in the child, after the fork. | ||||||||||||
| 123 | } | ||||||||||||
| 124 | 0 | else { $self->publish() } | |||||||||||
| 125 | |||||||||||||
| 126 | 0 | $runner->run($app); | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 130 | 3 | 1; | |||||||||||
| 131 | |||||||||||||
| 132 | __END__ | ||||||||||||
| 133 | |||||||||||||
| 134 | =pod | ||||||||||||
| 135 | |||||||||||||
| 136 | =encoding UTF-8 | ||||||||||||
| 137 | |||||||||||||
| 138 | =head1 NAME | ||||||||||||
| 139 | |||||||||||||
| 140 | HiD::App::Command::server - HiD 'server' subcmd - start up a Plack-based web server for your site | ||||||||||||
| 141 | |||||||||||||
| 142 | =head1 SYNOPSIS | ||||||||||||
| 143 | |||||||||||||
| 144 | $ ../bin/hid server | ||||||||||||
| 145 | HTTP::Server::PSGI: Accepting connections at http://0:5000/ | ||||||||||||
| 146 | |||||||||||||
| 147 | =head1 DESCRIPTION | ||||||||||||
| 148 | |||||||||||||
| 149 | Start a Plack-based web server that serves your C<destination> directory. | ||||||||||||
| 150 | |||||||||||||
| 151 | =head1 ATTRIBUTES | ||||||||||||
| 152 | |||||||||||||
| 153 | =head2 auto_refresh | ||||||||||||
| 154 | |||||||||||||
| 155 | Automatically refresh result when source file/dir changed, just likey jekyll | ||||||||||||
| 156 | |||||||||||||
| 157 | Note that setting this will also clean out the destination directory whenever | ||||||||||||
| 158 | a watched file changes. | ||||||||||||
| 159 | |||||||||||||
| 160 | =head2 clean | ||||||||||||
| 161 | |||||||||||||
| 162 | Remove any existing site directory prior to the publication run | ||||||||||||
| 163 | |||||||||||||
| 164 | =head2 debug | ||||||||||||
| 165 | |||||||||||||
| 166 | Emit debug-style logging for requests | ||||||||||||
| 167 | |||||||||||||
| 168 | =head2 port | ||||||||||||
| 169 | |||||||||||||
| 170 | Port number to bind. Defaults to 5000. | ||||||||||||
| 171 | |||||||||||||
| 172 | =head1 SEE ALSO | ||||||||||||
| 173 | |||||||||||||
| 174 | See L<HiD::App::Command> for additional command line options supported by all | ||||||||||||
| 175 | sub commands. | ||||||||||||
| 176 | |||||||||||||
| 177 | =head1 VERSION | ||||||||||||
| 178 | |||||||||||||
| 179 | version 1.96 | ||||||||||||
| 180 | |||||||||||||
| 181 | =head1 AUTHOR | ||||||||||||
| 182 | |||||||||||||
| 183 | John SJ Anderson <genehack@genehack.org> | ||||||||||||
| 184 | |||||||||||||
| 185 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 186 | |||||||||||||
| 187 | This software is copyright (c) 2015 by John SJ Anderson. | ||||||||||||
| 188 | |||||||||||||
| 189 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 190 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 191 | |||||||||||||
| 192 | =cut | ||||||||||||