$HiD::App::Command::server::VERSION
=
'1.8'
;
use
5.014;
use
open
qw/ :std :utf8 /
;
has
auto_refresh
=> (
is
=>
'ro'
,
isa
=>
'Bool'
,
traits
=> [
'Getopt'
],
cmd_aliases
=> [
qw/ auto A /
],
documentation
=>
'auto re-publish when source changes, Default=False'
,
lazy
=> 1,
default
=> 0,
);
has
clean
=> (
is
=>
'ro'
,
isa
=>
'Bool'
,
cmd_aliases
=>
'C'
,
traits
=> [
'Getopt'
] ,
);
has
debug
=> (
is
=>
'ro'
,
isa
=>
'Bool'
,
cmd_aliases
=>
'D'
,
traits
=> [
'Getopt'
] ,
);
has
port
=> (
is
=>
'ro'
,
isa
=>
'Int'
,
traits
=> [
'Getopt'
] ,
cmd_aliases
=>
'p'
,
documentation
=>
'port to run the server on. Default=5000'
,
lazy
=> 1 ,
builder
=>
'_build_port'
,
);
sub
_build_port {
my
$self
=
shift
;
return
$self
->{port}
if
defined
$self
->{port};
my
$config
=
$self
->config;
return
$self
->config->{server_port} // 5000;
}
sub
_run {
my
(
$self
,
$opts
,
$args
) =
@_
;
my
$config
=
$self
->config;
if
(
$self
->clean ) {
$config
->{clean_destination} = 1;
}
if
(
$self
->publish_drafts ){
$config
->{publish_drafts} = 1;
}
my
$app
= HiD::Server->new(
root
=>
$self
->destination )->to_app;
if
(
$self
->debug ) {
if
( try_load_class(
'Plack::Middleware::DebugLogging'
)) {
$app
= builder {
enable_if {
$ENV
{PLACK_ENV} eq
'development'
}
'DebugLogging'
;
$app
;
};
}
else
{
print
STDERR
"*** Plack::Middleware::DebugLogging required for debug logging.\n"
;
print
STDERR
"*** Continuing with normal logging.\n"
;
}
}
my
$runner
= Plack::Runner->new;
my
%args
= (
'-p'
=>
$self
->port );
$runner
->parse_options(
%args
);
if
(
$self
->auto_refresh ) {
my
@dirs
=
map
{
$self
->hid->get_config(
$_
) }
qw/ include_dir layout_dir posts_dir /
;
for
my
$dir
(
qw/pages regular_files/
) {
push
@dirs
,
map
{
$_
->input_filename } @{
$self
->hid->
$dir
};
}
$runner
->{server} =
'+HiD::Server::Handler'
;
$runner
->{loader} =
'+HiD::Server::Loader'
;
$runner
->loader->watch(
@dirs
);
push
@{
$runner
->{options}} , (
hid
=>
$self
);
}
else
{
$self
->publish() }
$runner
->run(
$app
);
}
__PACKAGE__->meta->make_immutable;
1;