1 | package Mojo::Server;
|
---|
2 | use Mojo::Base 'Mojo::EventEmitter';
|
---|
3 |
|
---|
4 | use Carp 'croak';
|
---|
5 | use Mojo::File 'path';
|
---|
6 | use Mojo::Loader 'load_class';
|
---|
7 | use Mojo::Util 'md5_sum';
|
---|
8 | use POSIX ();
|
---|
9 | use Scalar::Util 'blessed';
|
---|
10 |
|
---|
11 | has app => sub { shift->build_app('Mojo::HelloWorld') };
|
---|
12 | has reverse_proxy => sub { $ENV{MOJO_REVERSE_PROXY} };
|
---|
13 |
|
---|
14 | sub build_app {
|
---|
15 | my ($self, $app) = (shift, shift);
|
---|
16 | local $ENV{MOJO_EXE};
|
---|
17 | return $self->app($app->new(@_))->app unless my $e = load_class $app;
|
---|
18 | die ref $e ? $e : qq{Can't find application class "$app" in \@INC. (@INC)\n};
|
---|
19 | }
|
---|
20 |
|
---|
21 | sub build_tx {
|
---|
22 | my $self = shift;
|
---|
23 | my $tx = $self->app->build_tx;
|
---|
24 | $tx->req->reverse_proxy(1) if $self->reverse_proxy;
|
---|
25 | return $tx;
|
---|
26 | }
|
---|
27 |
|
---|
28 | sub daemonize {
|
---|
29 |
|
---|
30 | # Fork and kill parent
|
---|
31 | die "Can't fork: $!" unless defined(my $pid = fork);
|
---|
32 | exit 0 if $pid;
|
---|
33 | POSIX::setsid or die "Can't start a new session: $!";
|
---|
34 |
|
---|
35 | # Close filehandles
|
---|
36 | open STDIN, '</dev/null';
|
---|
37 | open STDOUT, '>/dev/null';
|
---|
38 | open STDERR, '>&STDOUT';
|
---|
39 | }
|
---|
40 |
|
---|
41 | sub load_app {
|
---|
42 | my ($self, $path) = @_;
|
---|
43 |
|
---|
44 | # Clean environment (reset FindBin defensively)
|
---|
45 | {
|
---|
46 | local $0 = $path = path($path)->to_abs->to_string;
|
---|
47 | require FindBin;
|
---|
48 | FindBin->again;
|
---|
49 | local $ENV{MOJO_APP_LOADER} = 1;
|
---|
50 | local $ENV{MOJO_EXE};
|
---|
51 |
|
---|
52 | # Try to load application from script into sandbox
|
---|
53 | delete $INC{$path};
|
---|
54 | my $app = eval
|
---|
55 | "package Mojo::Server::Sandbox::@{[md5_sum $path]}; require \$path";
|
---|
56 | die qq{Can't load application from file "$path": $@} if $@;
|
---|
57 | die qq{File "$path" did not return an application object.\n}
|
---|
58 | unless blessed $app && $app->can('handler');
|
---|
59 | $self->app($app);
|
---|
60 | };
|
---|
61 | FindBin->again;
|
---|
62 |
|
---|
63 | return $self->app;
|
---|
64 | }
|
---|
65 |
|
---|
66 | sub new {
|
---|
67 | my $self = shift->SUPER::new(@_);
|
---|
68 | $self->on(request => sub { shift->app->handler(shift) });
|
---|
69 | return $self;
|
---|
70 | }
|
---|
71 |
|
---|
72 | sub run { croak 'Method "run" not implemented by subclass' }
|
---|
73 |
|
---|
74 | 1;
|
---|
75 |
|
---|
76 | =encoding utf8
|
---|
77 |
|
---|
78 | =head1 NAME
|
---|
79 |
|
---|
80 | Mojo::Server - HTTP/WebSocket server base class
|
---|
81 |
|
---|
82 | =head1 SYNOPSIS
|
---|
83 |
|
---|
84 | package Mojo::Server::MyServer;
|
---|
85 | use Mojo::Base 'Mojo::Server';
|
---|
86 |
|
---|
87 | sub run {
|
---|
88 | my $self = shift;
|
---|
89 |
|
---|
90 | # Get a transaction
|
---|
91 | my $tx = $self->build_tx;
|
---|
92 |
|
---|
93 | # Emit "request" event
|
---|
94 | $self->emit(request => $tx);
|
---|
95 | }
|
---|
96 |
|
---|
97 | =head1 DESCRIPTION
|
---|
98 |
|
---|
99 | L<Mojo::Server> is an abstract base class for HTTP/WebSocket servers and server
|
---|
100 | interfaces, like L<Mojo::Server::CGI>, L<Mojo::Server::Daemon>,
|
---|
101 | L<Mojo::Server::Hypnotoad>, L<Mojo::Server::Morbo>, L<Mojo::Server::Prefork>
|
---|
102 | and L<Mojo::Server::PSGI>.
|
---|
103 |
|
---|
104 | =head1 EVENTS
|
---|
105 |
|
---|
106 | L<Mojo::Server> inherits all events from L<Mojo::EventEmitter> and can emit the
|
---|
107 | following new ones.
|
---|
108 |
|
---|
109 | =head2 request
|
---|
110 |
|
---|
111 | $server->on(request => sub {
|
---|
112 | my ($server, $tx) = @_;
|
---|
113 | ...
|
---|
114 | });
|
---|
115 |
|
---|
116 | Emitted when a request is ready and needs to be handled.
|
---|
117 |
|
---|
118 | $server->on(request => sub {
|
---|
119 | my ($server, $tx) = @_;
|
---|
120 | $tx->res->code(200);
|
---|
121 | $tx->res->headers->content_type('text/plain');
|
---|
122 | $tx->res->body('Hello World!');
|
---|
123 | $tx->resume;
|
---|
124 | });
|
---|
125 |
|
---|
126 | =head1 ATTRIBUTES
|
---|
127 |
|
---|
128 | L<Mojo::Server> implements the following attributes.
|
---|
129 |
|
---|
130 | =head2 app
|
---|
131 |
|
---|
132 | my $app = $server->app;
|
---|
133 | $server = $server->app(MojoSubclass->new);
|
---|
134 |
|
---|
135 | Application this server handles, defaults to a L<Mojo::HelloWorld> object.
|
---|
136 |
|
---|
137 | =head2 reverse_proxy
|
---|
138 |
|
---|
139 | my $bool = $server->reverse_proxy;
|
---|
140 | $server = $server->reverse_proxy($bool);
|
---|
141 |
|
---|
142 | This server operates behind a reverse proxy, defaults to the value of the
|
---|
143 | C<MOJO_REVERSE_PROXY> environment variable.
|
---|
144 |
|
---|
145 | =head1 METHODS
|
---|
146 |
|
---|
147 | L<Mojo::Server> inherits all methods from L<Mojo::EventEmitter> and implements
|
---|
148 | the following new ones.
|
---|
149 |
|
---|
150 | =head2 build_app
|
---|
151 |
|
---|
152 | my $app = $server->build_app('MyApp');
|
---|
153 | my $app = $server->build_app('MyApp', log => Mojo::Log->new);
|
---|
154 | my $app = $server->build_app('MyApp', {log => Mojo::Log->new});
|
---|
155 |
|
---|
156 | Build application from class and assign it to L</"app">.
|
---|
157 |
|
---|
158 | =head2 build_tx
|
---|
159 |
|
---|
160 | my $tx = $server->build_tx;
|
---|
161 |
|
---|
162 | Let application build a transaction.
|
---|
163 |
|
---|
164 | =head2 daemonize
|
---|
165 |
|
---|
166 | $server->daemonize;
|
---|
167 |
|
---|
168 | Daemonize server process.
|
---|
169 |
|
---|
170 | =head2 load_app
|
---|
171 |
|
---|
172 | my $app = $server->load_app('/home/sri/myapp.pl');
|
---|
173 |
|
---|
174 | Load application from script and assign it to L</"app">.
|
---|
175 |
|
---|
176 | say Mojo::Server->new->load_app('./myapp.pl')->home;
|
---|
177 |
|
---|
178 | =head2 new
|
---|
179 |
|
---|
180 | my $server = Mojo::Server->new;
|
---|
181 | my $server = Mojo::Server->new(reverse_proxy => 1);
|
---|
182 | my $server = Mojo::Server->new({reverse_proxy => 1});
|
---|
183 |
|
---|
184 | Construct a new L<Mojo::Server> object and subscribe to L</"request"> event
|
---|
185 | with default request handling.
|
---|
186 |
|
---|
187 | =head2 run
|
---|
188 |
|
---|
189 | $server->run;
|
---|
190 |
|
---|
191 | Run server. Meant to be overloaded in a subclass.
|
---|
192 |
|
---|
193 | =head1 SEE ALSO
|
---|
194 |
|
---|
195 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
196 |
|
---|
197 | =cut
|
---|