| 1 | #!/usr/bin/env perl | ||
| 2 | # | ||
| 3 | # This file is part of Pod-Browser | ||
| 4 | # | ||
| 5 | # This software is Copyright (c) 2012 by Moritz Onken. | ||
| 6 | # | ||
| 7 | # This is free software, licensed under: | ||
| 8 | # | ||
| 9 | # The (three-clause) BSD License | ||
| 10 | # | ||
| 11 | # PODNAME: pod_browser_server.pl | ||
| 12 | BEGIN { | ||
| 13 | $ENV{CATALYST_ENGINE} ||= 'HTTP'; | ||
| 14 | $ENV{CATALYST_SCRIPT_GEN} = 39; | ||
| 15 | require Catalyst::Engine::HTTP; | ||
| 16 | } | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use Getopt::Long; | ||
| 21 | use Pod::Usage; | ||
| 22 | use FindBin; | ||
| 23 | use lib "$FindBin::Bin/../lib"; | ||
| 24 | |||
| 25 | my $debug = 0; | ||
| 26 | my $fork = 1; | ||
| 27 | my $help = 0; | ||
| 28 | my $host = undef; | ||
| 29 | my $port = $ENV{POD_BROWSER_PORT} || $ENV{CATALYST_PORT} || 3030; | ||
| 30 | my $keepalive = 0; | ||
| 31 | my $restart = $ENV{POD_BROWSER_RELOAD} || $ENV{CATALYST_RELOAD} || 0; | ||
| 32 | my $background = 0; | ||
| 33 | my $pidfile = undef; | ||
| 34 | |||
| 35 | my $check_interval; | ||
| 36 | my $file_regex; | ||
| 37 | my $watch_directory; | ||
| 38 | my $follow_symlinks; | ||
| 39 | |||
| 40 | my @argv = @ARGV; | ||
| 41 | |||
| 42 | GetOptions( | ||
| 43 | 'debug|d' => \$debug, | ||
| 44 | 'fork|f' => \$fork, | ||
| 45 | 'help|?' => \$help, | ||
| 46 | 'host=s' => \$host, | ||
| 47 | 'port|p=s' => \$port, | ||
| 48 | 'keepalive|k' => \$keepalive, | ||
| 49 | 'restart|r' => \$restart, | ||
| 50 | 'restartdelay|rd=s' => \$check_interval, | ||
| 51 | 'restartregex|rr=s' => \$file_regex, | ||
| 52 | 'restartdirectory=s@' => \$watch_directory, | ||
| 53 | 'followsymlinks' => \$follow_symlinks, | ||
| 54 | 'background' => \$background, | ||
| 55 | 'pidfile=s' => \$pidfile, | ||
| 56 | ); | ||
| 57 | |||
| 58 | pod2usage(1) if $help; | ||
| 59 | |||
| 60 | if ( $debug ) { | ||
| 61 | $ENV{CATALYST_DEBUG} = 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | # If we load this here, then in the case of a restarter, it does not | ||
| 65 | # need to be reloaded for each restart. | ||
| 66 | require Catalyst; | ||
| 67 | |||
| 68 | # If this isn't done, then the Catalyst::Devel tests for the restarter | ||
| 69 | # fail. | ||
| 70 | $| = 1 if $ENV{HARNESS_ACTIVE}; | ||
| 71 | |||
| 72 | my $runner = sub { | ||
| 73 | # This is require instead of use so that the above environment | ||
| 74 | # variables can be set at runtime. | ||
| 75 | require Pod::Browser; | ||
| 76 | |||
| 77 | Pod::Browser->run( | ||
| 78 | $port, $host, | ||
| 79 | { | ||
| 80 | argv => \@argv, | ||
| 81 | 'fork' => $fork, | ||
| 82 | keepalive => $keepalive, | ||
| 83 | background => $background, | ||
| 84 | pidfile => $pidfile, | ||
| 85 | } | ||
| 86 | ); | ||
| 87 | }; | ||
| 88 | |||
| 89 | if ( $restart ) { | ||
| 90 | die "Cannot run in the background and also watch for changed files.\n" | ||
| 91 | if $background; | ||
| 92 | |||
| 93 | require Catalyst::Restarter; | ||
| 94 | |||
| 95 | my $subclass = Catalyst::Restarter->pick_subclass; | ||
| 96 | |||
| 97 | my %args; | ||
| 98 | $args{follow_symlinks} = 1 | ||
| 99 | if $follow_symlinks; | ||
| 100 | $args{directories} = $watch_directory | ||
| 101 | if defined $watch_directory; | ||
| 102 | $args{sleep_interval} = $check_interval | ||
| 103 | if defined $check_interval; | ||
| 104 | $args{filter} = qr/$file_regex/ | ||
| 105 | if defined $file_regex; | ||
| 106 | |||
| 107 | my $restarter = $subclass->new( | ||
| 108 | %args, | ||
| 109 | start_sub => $runner, | ||
| 110 | argv => \@argv, | ||
| 111 | ); | ||
| 112 | |||
| 113 | $restarter->run_and_watch; | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | $runner->(); | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 | |||
| 121 | |||
| 122 | __END__ |