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