| 1 | package Mojo::UserAgent::Server; | ||||||||||||||||||||||||||||
| 2 | 23 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 23 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 23 | ▶ | use Mojo::Server::Daemon; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 23 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 0 | ▶ | has ioloop => sub { Mojo::IOLoop->singleton }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 183 | ▶ | sub app { | ||||||||||||||||||||||||||
|
Subroutine
app: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 183 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Singleton application | ||||||||||||||||||||||||||||
| 14 | 183 | state $singleton; | |||||||||||||||||||||||||||
| 15 | 183 | ▶ | return $singleton = $app ? $app : $singleton unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Default to singleton application | ||||||||||||||||||||||||||||
| 18 | 160 | ▶ | return $self->{app} || $singleton unless $app; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{'app'} || $singleton
MC/DC0%0/2
$self->{'app'} || $singleton
$self->{'app'}
$singleton
Truth table66%2/3
A || B
A= $self->{'app'}B= $singleton
| |||||||||||||||||||||||||||||
| 19 | 28 | $self->{app} = $app; | |||||||||||||||||||||||||||
| 20 | 28 | return $self; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub nb_url { shift->_url(1, @_) } | ||||||||||||||||||||||||||
|
Subroutine
nb_url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub restart { delete @{$_[0]}{qw(nb_port nb_server port server)} } | ||||||||||||||||||||||||||
|
Subroutine
restart: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 157 | ▶ | sub url { shift->_url(0, @_) } | ||||||||||||||||||||||||||
|
Subroutine
url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 157 | ▶ | sub _url { | ||||||||||||||||||||||||||
|
Subroutine
_url: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||
| 30 | 157 | my ($self, $nb, $proto) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 157 | ▶ | if (!$self->{server} || $proto) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->{"server"} or $proto
MC/DC50%1/2
not $self->{"server"} or $proto
not $self->{"server"}
$proto
Truth table66%2/3
not(A) or B
A= not $self->{"server"}B= $proto
| |||||||||||||||||||||||||||||
| 33 | 17 | ▶ | $proto = $self->{proto} = $proto || 'http'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$proto || 'http'
MC/DC0%0/1
$proto || 'http'
$proto
Truth table50%1/2
A
A= $proto
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Blocking | ||||||||||||||||||||||||||||
| 36 | 17 | my $server = $self->{server} = Mojo::Server::Daemon->new(ioloop => $self->ioloop, silent => 1); | |||||||||||||||||||||||||||
| 37 | 17 | weaken $server->app($self->app)->{app}; | |||||||||||||||||||||||||||
| 38 | 17 | ▶ | my $port = $self->{port} ? ":$self->{port}" : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 17 | $self->{port} = $server->listen(["$proto://127.0.0.1$port"])->start->ports->[0]; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Non-blocking | ||||||||||||||||||||||||||||
| 42 | 17 | $server = $self->{nb_server} = Mojo::Server::Daemon->new(silent => 1); | |||||||||||||||||||||||||||
| 43 | 17 | weaken $server->app($self->app)->{app}; | |||||||||||||||||||||||||||
| 44 | 17 | ▶ | $port = $self->{nb_port} ? ":$self->{nb_port}" : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 17 | $self->{nb_port} = $server->listen(["$proto://127.0.0.1$port"])->start->ports->[0]; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 157 | ▶ | my $port = $nb ? $self->{nb_port} : $self->{port}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 157 | return Mojo::URL->new("$self->{proto}://127.0.0.1:$port/"); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 23 | 1; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =encoding utf8 | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 NAME | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Mojo::UserAgent::Server - Application server | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | use Mojo::UserAgent::Server; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | my $server = Mojo::UserAgent::Server->new; | ||||||||||||||||||||||||||||
| 65 | say $server->url; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | L<Mojo::UserAgent::Server> is an embedded web server based on L<Mojo::Server::Daemon> that processes requests for | ||||||||||||||||||||||||||||
| 70 | L<Mojo::UserAgent>. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | L<Mojo::UserAgent::Server> implements the following attributes. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 ioloop | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | my $loop = $server->ioloop; | ||||||||||||||||||||||||||||
| 79 | $server = $server->ioloop(Mojo::IOLoop->new); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Event loop object to use for I/O operations, defaults to the global L<Mojo::IOLoop> singleton. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 METHODS | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | L<Mojo::UserAgent::Server> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head2 app | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | my $app = Mojo::UserAgent::Server->app; | ||||||||||||||||||||||||||||
| 90 | Mojo::UserAgent::Server->app(Mojolicious->new); | ||||||||||||||||||||||||||||
| 91 | my $app = $server->app; | ||||||||||||||||||||||||||||
| 92 | $server = $server->app(Mojolicious->new); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Application this server handles, instance specific applications override the global default. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Change application behavior | ||||||||||||||||||||||||||||
| 97 | $server->app->defaults(testing => 'oh yea!'); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 nb_url | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | my $url = $server->nb_url; | ||||||||||||||||||||||||||||
| 102 | my $url = $server->nb_url('http'); | ||||||||||||||||||||||||||||
| 103 | my $url = $server->nb_url('https'); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Get absolute L<Mojo::URL> object for server processing non-blocking requests with L</"app"> and switch protocol if | ||||||||||||||||||||||||||||
| 106 | necessary. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 restart | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | $server->restart; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Restart server with new port. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head2 url | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | my $url = $server->url; | ||||||||||||||||||||||||||||
| 117 | my $url = $server->url('http'); | ||||||||||||||||||||||||||||
| 118 | my $url = $server->url('https'); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Get absolute L<Mojo::URL> object for server processing blocking requests with L</"app"> and switch protocol if | ||||||||||||||||||||||||||||
| 121 | necessary. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||