Newer Older
115 lines | 2.529kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojo::UserAgent::Server;
2
use Mojo::Base -base;
3

            
4
use Mojo::IOLoop;
5
use Mojo::Server::Daemon;
6

            
7
has ioloop => sub { Mojo::IOLoop->singleton };
8

            
9
sub app {
10
  my ($self, $app) = @_;
11

            
12
  # Singleton application
13
  state $singleton;
14
  return $singleton = $app ? $app : $singleton unless ref $self;
15

            
16
  # Default to singleton application
17
  return $self->{app} || $singleton unless $app;
18
  $self->{app} = $app;
19
  return $self;
20
}
21

            
22
sub restart { shift->_restart(1) }
23

            
24
sub url {
25
  my $self = shift;
26
  $self->_restart(0, @_)
27
    if !$self->{server} || $self->{server}->ioloop ne $self->ioloop || @_;
28
  return Mojo::URL->new("$self->{proto}://localhost:$self->{port}/");
29
}
30

            
31
sub _restart {
32
  my ($self, $full, $proto) = @_;
33

            
34
  my $server = $self->{server} = Mojo::Server::Daemon->new(
35
    app    => $self->app,
36
    ioloop => $self->ioloop,
37
    silent => 1
38
  );
39
  delete $self->{port} if $full;
40
  die "Couldn't find a free TCP port for application.\n"
41
    unless my $port = $self->{port} ||= Mojo::IOLoop->generate_port;
42
  $self->{proto} = $proto ||= 'http';
43
  $server->listen(["$proto://127.0.0.1:$port"])->start;
44
}
45

            
46
1;
47

            
48
=encoding utf8
49

            
50
=head1 NAME
51

            
52
Mojo::UserAgent::Server - Application server
53

            
54
=head1 SYNOPSIS
55

            
56
  use Mojo::UserAgent::Server;
57

            
58
  my $server = Mojo::UserAgent::Server->new;
59
  say $server->url;
60

            
61
=head1 DESCRIPTION
62

            
63
L<Mojo::UserAgent::Server> is an embedded web server based on
64
L<Mojo::Server::Daemon> that processes requests for L<Mojo::UserAgent>.
65

            
66
=head1 ATTRIBUTES
67

            
68
L<Mojo::UserAgent::Server> implements the following attributes.
69

            
70
=head2 ioloop
71

            
72
  my $loop = $server->ioloop;
73
  $server  = $server->ioloop(Mojo::IOLoop->new);
74

            
75
Event loop object to use for I/O operations, defaults to the global
76
L<Mojo::IOLoop> singleton.
77

            
78
=head1 METHODS
79

            
80
L<Mojo::UserAgent::Server> inherits all methods from L<Mojo::Base> and
81
implements the following new ones.
82

            
83
=head2 app
84

            
85
  my $app = Mojo::UserAgent::Server->app;
86
            Mojo::UserAgent::Server->app(MyApp->new);
87
  my $app = $server->app;
88
  $server = $server->app(MyApp->new);
89

            
90
Application this server handles, instance specific applications override the
91
global default.
92

            
93
  # Change application behavior
94
  $server->defaults(testing => 'oh yea!');
95

            
96
=head2 restart
97

            
98
  $server->restart;
99

            
100
Restart server with new port.
101

            
102
=head2 url
103

            
104
  my $url = $ua->url;
105
  my $url = $ua->url('http');
106
  my $url = $ua->url('https');
107

            
108
Get absolute L<Mojo::URL> object for L</"app"> and switch protocol if
109
necessary.
110

            
111
=head1 SEE ALSO
112

            
113
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
114

            
115
=cut