1Mojo::Server::Daemon(3)User Contributed Perl DocumentatioMnojo::Server::Daemon(3)
2
3
4
6 Mojo::Server::Daemon - Non-blocking I/O HTTP and WebSocket server
7
9 use Mojo::Server::Daemon;
10
11 my $daemon = Mojo::Server::Daemon->new(listen => ['http://*:8080']);
12 $daemon->unsubscribe('request')->on(request => sub {
13 my ($daemon, $tx) = @_;
14
15 # Request
16 my $method = $tx->req->method;
17 my $path = $tx->req->url->path;
18
19 # Response
20 $tx->res->code(200);
21 $tx->res->headers->content_type('text/plain');
22 $tx->res->body("$method request for $path!");
23
24 # Resume transaction
25 $tx->resume;
26 });
27 $daemon->run;
28
30 Mojo::Server::Daemon is a full featured, highly portable non-blocking
31 I/O HTTP and WebSocket server, with IPv6, TLS, SNI, Comet (long
32 polling), keep-alive and multiple event loop support.
33
34 For better scalability (epoll, kqueue) and to provide non-blocking name
35 resolution, SOCKS5 as well as TLS support, the optional modules EV
36 (4.0+), Net::DNS::Native (0.15+), IO::Socket::Socks (0.64+) and
37 IO::Socket::SSL (2.009+) will be used automatically if possible.
38 Individual features can also be disabled with the "MOJO_NO_NNR",
39 "MOJO_NO_SOCKS" and "MOJO_NO_TLS" environment variables.
40
41 See "DEPLOYMENT" in Mojolicious::Guides::Cookbook for more.
42
44 The Mojo::Server::Daemon process can be controlled at runtime with the
45 following signals.
46
47 INT, TERM
48 Shut down server immediately.
49
51 Mojo::Server::Daemon inherits all events from Mojo::Server.
52
54 Mojo::Server::Daemon inherits all attributes from Mojo::Server and
55 implements the following new ones.
56
57 acceptors
58 my $acceptors = $daemon->acceptors;
59 $daemon = $daemon->acceptors(['6be0c140ef00a389c5d039536b56d139']);
60
61 Active acceptor ids.
62
63 # Check port
64 mu $port = $daemon->ioloop->acceptor($daemon->acceptors->[0])->port;
65
66 backlog
67 my $backlog = $daemon->backlog;
68 $daemon = $daemon->backlog(128);
69
70 Listen backlog size, defaults to "SOMAXCONN".
71
72 inactivity_timeout
73 my $timeout = $daemon->inactivity_timeout;
74 $daemon = $daemon->inactivity_timeout(5);
75
76 Maximum amount of time in seconds a connection can be inactive before
77 getting closed, defaults to the value of the "MOJO_INACTIVITY_TIMEOUT"
78 environment variable or 15. Setting the value to 0 will allow
79 connections to be inactive indefinitely.
80
81 ioloop
82 my $loop = $daemon->ioloop;
83 $daemon = $daemon->ioloop(Mojo::IOLoop->new);
84
85 Event loop object to use for I/O operations, defaults to the global
86 Mojo::IOLoop singleton.
87
88 listen
89 my $listen = $daemon->listen;
90 $daemon = $daemon->listen(['https://127.0.0.1:8080']);
91
92 Array reference with one or more locations to listen on, defaults to
93 the value of the "MOJO_LISTEN" environment variable or "http://*:3000"
94 (shortcut for "http://0.0.0.0:3000").
95
96 # Listen on all IPv4 interfaces
97 $daemon->listen(['http://*:3000']);
98
99 # Listen on all IPv4 and IPv6 interfaces
100 $daemon->listen(['http://[::]:3000']);
101
102 # Listen on IPv6 interface
103 $daemon->listen(['http://[::1]:4000']);
104
105 # Listen on IPv4 and IPv6 interfaces
106 $daemon->listen(['http://127.0.0.1:3000', 'http://[::1]:3000']);
107
108 # Listen on UNIX domain socket "/tmp/myapp.sock" (percent encoded slash)
109 $daemon->listen(['http+unix://%2Ftmp%2Fmyapp.sock']);
110
111 # File descriptor, as used by systemd
112 $daemon->listen(['http://127.0.0.1?fd=3']);
113
114 # Allow multiple servers to use the same port (SO_REUSEPORT)
115 $daemon->listen(['http://*:8080?reuse=1']);
116
117 # Listen on two ports with HTTP and HTTPS at the same time
118 $daemon->listen(['http://*:3000', 'https://*:4000']);
119
120 # Use a custom certificate and key
121 $daemon->listen(['https://*:3000?cert=/x/server.crt&key=/y/server.key']);
122
123 # Domain specific certificates and keys (SNI)
124 $daemon->listen(
125 ['https://*:3000?example.com_cert=/x/my.crt&example.com_key=/y/my.key']);
126
127 # Or even a custom certificate authority
128 $daemon->listen(
129 ['https://*:3000?cert=/x/server.crt&key=/y/server.key&ca=/z/ca.crt']);
130
131 These parameters are currently available:
132
133 ca
134 ca=/etc/tls/ca.crt
135
136 Path to TLS certificate authority file used to verify the peer
137 certificate.
138
139 cert
140 cert=/etc/tls/server.crt
141 mojolicious.org_cert=/etc/tls/mojo.crt
142
143 Path to the TLS cert file, defaults to a built-in test certificate.
144
145 ciphers
146 ciphers=AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH
147
148 TLS cipher specification string. For more information about the
149 format see
150 <https://www.openssl.org/docs/manmaster/man1/ciphers.html#CIPHER-STRINGS>.
151
152 fd
153 fd=3
154
155 File descriptor with an already prepared listen socket.
156
157 key
158 key=/etc/tls/server.key
159 mojolicious.org_key=/etc/tls/mojo.key
160
161 Path to the TLS key file, defaults to a built-in test key.
162
163 reuse
164 reuse=1
165
166 Allow multiple servers to use the same port with the "SO_REUSEPORT"
167 socket option.
168
169 single_accept
170 single_accept=1
171
172 Only accept one connection at a time.
173
174 verify
175 verify=0x00
176
177 TLS verification mode.
178
179 version
180 version=TLSv1_2
181
182 TLS protocol version.
183
184 max_clients
185 my $max = $daemon->max_clients;
186 $daemon = $daemon->max_clients(100);
187
188 Maximum number of accepted connections this server is allowed to handle
189 concurrently, before stopping to accept new incoming connections,
190 passed along to "max_connections" in Mojo::IOLoop.
191
192 max_requests
193 my $max = $daemon->max_requests;
194 $daemon = $daemon->max_requests(250);
195
196 Maximum number of keep-alive requests per connection, defaults to 100.
197
198 silent
199 my $bool = $daemon->silent;
200 $daemon = $daemon->silent($bool);
201
202 Disable console messages.
203
205 Mojo::Server::Daemon inherits all methods from Mojo::Server and
206 implements the following new ones.
207
208 ports
209 my $ports = $daemon->ports;
210
211 Get all ports this server is currently listening on.
212
213 # All ports
214 say for @{$daemon->ports};
215
216 run
217 $daemon->run;
218
219 Run server and wait for "SIGNALS".
220
221 start
222 $daemon = $daemon->start;
223
224 Start or resume accepting connections through "ioloop".
225
226 # Listen on random port
227 my $port = $daemon->listen(['http://127.0.0.1'])->start->ports->[0];
228
229 # Run multiple web servers concurrently
230 my $daemon1 = Mojo::Server::Daemon->new(listen => ['http://*:3000'])->start;
231 my $daemon2 = Mojo::Server::Daemon->new(listen => ['http://*:4000'])->start;
232 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
233
234 stop
235 $daemon = $daemon->stop;
236
237 Stop accepting connections through "ioloop".
238
240 You can set the "MOJO_SERVER_DEBUG" environment variable to get some
241 advanced diagnostics information printed to "STDERR".
242
243 MOJO_SERVER_DEBUG=1
244
246 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
247
248
249
250perl v5.30.0 2019-07-26 Mojo::Server::Daemon(3)