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