| 11 | | use lib file( $FindBin::RealBin, 'lib' )->stringify; |
| 12 | | use Moxy; |
| 13 | | |
| 14 | | sub stdio_close { |
| 15 | | close(STDIN); |
| 16 | | close(STDOUT); |
| 17 | | close(STDERR); |
| 18 | | |
| 19 | | open(STDIN, "+>/dev/null"); ## no critic. |
| 20 | | open(STDOUT, "+>&STDIN"); ## no critic. |
| 21 | | open(STDERR, "+>&STDIN"); ## no critic. |
| 22 | | } |
| 23 | | |
| 24 | | my $conf_file = file( $FindBin::RealBin, 'config.yaml' )->stringify; |
| 25 | | |
| 26 | | Getopt::Long::GetOptions( |
| 27 | | '--man' => \my $man, |
| 28 | | '--daemon' => \my $daemon, |
| 29 | | '--config=s' => \$conf_file, |
| 30 | | ) or pod2usage(2); |
| 31 | | Getopt::Long::Configure("bundling"); |
| 32 | | pod2usage(-verbose => 2) if $man; |
| 33 | | |
| 34 | | my $config = YAML::LoadFile($conf_file); |
| 35 | | $config->{global}->{log} ||= { level => 'debug' }; |
| 36 | | |
| 37 | | sub start { |
| 38 | | my $moxy = Moxy->new($config); |
| 39 | | my $server_conf = { |
| 40 | | global => $config->{global}, |
| 41 | | plugins => [ |
| 42 | | $config->{global}->{server}, |
| 43 | | {module => 'DebugScreen', } |
| 44 | | ] |
| 45 | | }; |
| 46 | | HTTP::Engine->new( |
| 47 | | config => $server_conf, |
| 48 | | handle_request => sub { |
| 49 | | my $c = shift; |
| 50 | | $moxy->handle_request( $c ); |
| 51 | | }, |
| 52 | | )->run; |
| 53 | | } |
| 54 | | |
| 55 | | if ($daemon) { |
| 56 | | if (my $pid = fork) { |
| 57 | | exit 0; |
| 58 | | } elsif (defined $pid) { |
| 59 | | stdio_close(); |
| 60 | | start(); |
| 61 | | } else { |
| 62 | | die "fork failed: $@"; |
| 63 | | } |
| 64 | | } else { |
| 65 | | start(); |
| 66 | | } |
| 67 | | |
| 68 | | __END__ |
| 69 | | |
| 70 | | =head1 SYNOPSIS |
| 71 | | |
| 72 | | $ moxy.pl |
| 73 | | |
| 74 | | Options: |
| 75 | | --daemon => run as daemon |
| 76 | | --config=s => path to config file(default: config.yaml) |
| 77 | | --man => show this manual |
| 78 | | |
| 79 | | =head1 DESCRIPTION |
| 80 | | |
| 81 | | Proxy server for mobile web service development. |
| 82 | | |
| | 9 | Moxy::Cmd->run; |