1Mojo::Server::Daemon(3)User Contributed Perl DocumentatioMnojo::Server::Daemon(3)
2
3
4

NAME

6       Mojo::Server::Daemon - Non-blocking I/O HTTP and WebSocket server
7

SYNOPSIS

9         use Mojo::Server::Daemon;
10
11         my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:8080']);
12         $daemon->unsubscribe('request')->on(request => sub ($daemon, $tx) {
13
14           # Request
15           my $method = $tx->req->method;
16           my $path   = $tx->req->url->path;
17
18           # Response
19           $tx->res->code(200);
20           $tx->res->headers->content_type('text/plain');
21           $tx->res->body("$method request for $path!");
22
23           # Resume transaction
24           $tx->resume;
25         });
26         $daemon->run;
27

DESCRIPTION

29       Mojo::Server::Daemon is a full featured, highly portable non-blocking
30       I/O HTTP and WebSocket server, with IPv6, TLS, SNI, Comet (long
31       polling), keep-alive and multiple event loop support.
32
33       For better scalability (epoll, kqueue) and to provide non-blocking name
34       resolution, SOCKS5 as well as TLS support, the optional modules EV
35       (4.32+), Net::DNS::Native (0.15+), IO::Socket::Socks (0.64+) and
36       IO::Socket::SSL (2.009+) will be used automatically if possible.
37       Individual features can also be disabled with the "MOJO_NO_NNR",
38       "MOJO_NO_SOCKS" and "MOJO_NO_TLS" environment variables.
39
40       See "DEPLOYMENT" in Mojolicious::Guides::Cookbook for more.
41

SIGNALS

43       The Mojo::Server::Daemon process can be controlled at runtime with the
44       following signals.
45
46   INT, TERM
47       Shut down server immediately.
48

EVENTS

50       Mojo::Server::Daemon inherits all events from Mojo::Server.
51

ATTRIBUTES

53       Mojo::Server::Daemon inherits all attributes from Mojo::Server and
54       implements the following new ones.
55
56   acceptors
57         my $acceptors = $daemon->acceptors;
58         $daemon       = $daemon->acceptors(['6be0c140ef00a389c5d039536b56d139']);
59
60       Active acceptor ids.
61
62         # Check port
63         mu $port = $daemon->ioloop->acceptor($daemon->acceptors->[0])->port;
64
65   backlog
66         my $backlog = $daemon->backlog;
67         $daemon     = $daemon->backlog(128);
68
69       Listen backlog size, defaults to "SOMAXCONN".
70
71   inactivity_timeout
72         my $timeout = $daemon->inactivity_timeout;
73         $daemon     = $daemon->inactivity_timeout(5);
74
75       Maximum amount of time in seconds a connection with an active request
76       can be inactive before getting closed, defaults to the value of the
77       "MOJO_INACTIVITY_TIMEOUT" environment variable or 30. Setting the value
78       to 0 will allow connections to be inactive indefinitely.
79
80   ioloop
81         my $loop = $daemon->ioloop;
82         $daemon  = $daemon->ioloop(Mojo::IOLoop->new);
83
84       Event loop object to use for I/O operations, defaults to the global
85       Mojo::IOLoop singleton.
86
87   keep_alive_timeout
88         my $timeout = $daemon->keep_alive_timeout;
89         $daemon     = $daemon->keep_alive_timeout(10);
90
91       Maximum amount of time in seconds a connection without an active
92       request can be inactive before getting closed, defaults to the value of
93       the "MOJO_KEEP_ALIVE_TIMEOUT" environment variable or 5. Setting the
94       value to 0 will allow connections to be inactive indefinitely.
95
96   listen
97         my $listen = $daemon->listen;
98         $daemon    = $daemon->listen(['https://127.0.0.1:8080']);
99
100       Array reference with one or more locations to listen on, defaults to
101       the value of the "MOJO_LISTEN" environment variable or "http://*:3000"
102       (shortcut for "http://0.0.0.0:3000").
103
104         # Listen on all IPv4 interfaces
105         $daemon->listen(['http://*:3000']);
106
107         # Listen on all IPv4 and IPv6 interfaces
108         $daemon->listen(['http://[::]:3000']);
109
110         # Listen on IPv6 interface
111         $daemon->listen(['http://[::1]:4000']);
112
113         # Listen on IPv4 and IPv6 interfaces
114         $daemon->listen(['http://127.0.0.1:3000', 'http://[::1]:3000']);
115
116         # Listen on UNIX domain socket "/tmp/myapp.sock" (percent encoded slash)
117         $daemon->listen(['http+unix://%2Ftmp%2Fmyapp.sock']);
118
119         # File descriptor, as used by systemd
120         $daemon->listen(['http://127.0.0.1?fd=3']);
121
122         # Allow multiple servers to use the same port (SO_REUSEPORT)
123         $daemon->listen(['http://*:8080?reuse=1']);
124
125         # Listen on two ports with HTTP and HTTPS at the same time
126         $daemon->listen(['http://*:3000', 'https://*:4000']);
127
128         # Use a custom certificate and key
129         $daemon->listen(['https://*:3000?cert=/x/server.crt&key=/y/server.key']);
130
131         # Domain specific certificates and keys (SNI)
132         $daemon->listen(
133           ['https://*:3000?example.com_cert=/x/my.crt&example.com_key=/y/my.key']);
134
135         # Or even a custom certificate authority
136         $daemon->listen(
137           ['https://*:3000?cert=/x/server.crt&key=/y/server.key&ca=/z/ca.crt']);
138
139       These parameters are currently available:
140
141       ca
142           ca=/etc/tls/ca.crt
143
144         Path to TLS certificate authority file used to verify the peer
145         certificate.
146
147       cert
148           cert=/etc/tls/server.crt
149           mojolicious.org_cert=/etc/tls/mojo.crt
150
151         Path to the TLS cert file, defaults to a built-in test certificate.
152
153       ciphers
154           ciphers=AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH
155
156         TLS cipher specification string. For more information about the
157         format see
158         <https://www.openssl.org/docs/manmaster/man1/ciphers.html#CIPHER-STRINGS>.
159
160       fd
161           fd=3
162
163         File descriptor with an already prepared listen socket.
164
165       key
166           key=/etc/tls/server.key
167           mojolicious.org_key=/etc/tls/mojo.key
168
169         Path to the TLS key file, defaults to a built-in test key.
170
171       reuse
172           reuse=1
173
174         Allow multiple servers to use the same port with the "SO_REUSEPORT"
175         socket option.
176
177       single_accept
178           single_accept=1
179
180         Only accept one connection at a time.
181
182       verify
183           verify=0x00
184
185         TLS verification mode.
186
187       version
188           version=TLSv1_2
189
190         TLS protocol version.
191
192   max_clients
193         my $max = $daemon->max_clients;
194         $daemon = $daemon->max_clients(100);
195
196       Maximum number of accepted connections this server is allowed to handle
197       concurrently, before stopping to accept new incoming connections,
198       passed along to "max_connections" in Mojo::IOLoop.
199
200   max_requests
201         my $max = $daemon->max_requests;
202         $daemon = $daemon->max_requests(250);
203
204       Maximum number of keep-alive requests per connection, defaults to 100.
205
206   silent
207         my $bool = $daemon->silent;
208         $daemon  = $daemon->silent($bool);
209
210       Disable console messages.
211

METHODS

213       Mojo::Server::Daemon inherits all methods from Mojo::Server and
214       implements the following new ones.
215
216   ports
217         my $ports = $daemon->ports;
218
219       Get all ports this server is currently listening on.
220
221         # All ports
222         say for @{$daemon->ports};
223
224   run
225         $daemon->run;
226
227       Run server and wait for "SIGNALS".
228
229   start
230         $daemon = $daemon->start;
231
232       Start or resume accepting connections through "ioloop".
233
234         # Listen on random port
235         my $port = $daemon->listen(['http://127.0.0.1'])->start->ports->[0];
236
237         # Run multiple web servers concurrently
238         my $daemon1 = Mojo::Server::Daemon->new(listen => ['http://*:3000'])->start;
239         my $daemon2 = Mojo::Server::Daemon->new(listen => ['http://*:4000'])->start;
240         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
241
242   stop
243         $daemon = $daemon->stop;
244
245       Stop accepting connections through "ioloop".
246

DEBUGGING

248       You can set the "MOJO_SERVER_DEBUG" environment variable to get some
249       advanced diagnostics information printed to "STDERR".
250
251         MOJO_SERVER_DEBUG=1
252

SEE ALSO

254       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
255
256
257
258perl v5.36.0                      2022-07-22           Mojo::Server::Daemon(3)
Impressum