1Mojo::Server(3) User Contributed Perl Documentation Mojo::Server(3)
2
3
4
6 Mojo::Server - HTTP/WebSocket server base class
7
9 package Mojo::Server::MyServer;
10 use Mojo::Base 'Mojo::Server', -signatures;
11
12 sub run ($self) {
13
14 # Get a transaction
15 my $tx = $self->build_tx;
16
17 # Emit "request" event
18 $self->emit(request => $tx);
19 }
20
22 Mojo::Server is an abstract base class for HTTP/WebSocket servers and
23 server interfaces, like Mojo::Server::CGI, Mojo::Server::Daemon,
24 Mojo::Server::Hypnotoad, Mojo::Server::Morbo, Mojo::Server::Prefork and
25 Mojo::Server::PSGI.
26
28 Mojo::Server inherits all events from Mojo::EventEmitter and can emit
29 the following new ones.
30
31 request
32 $server->on(request => sub ($server, $tx) {...});
33
34 Emitted when a request is ready and needs to be handled.
35
36 $server->on(request => sub ($server, $tx) {
37 $tx->res->code(200);
38 $tx->res->headers->content_type('text/plain');
39 $tx->res->body('Hello World!');
40 $tx->resume;
41 });
42
44 Mojo::Server implements the following attributes.
45
46 app
47 my $app = $server->app;
48 $server = $server->app(MojoSubclass->new);
49
50 Application this server handles, defaults to a Mojo::HelloWorld object.
51
52 reverse_proxy
53 my $bool = $server->reverse_proxy;
54 $server = $server->reverse_proxy($bool);
55
56 This server operates behind a reverse proxy, defaults to the value of
57 the "MOJO_REVERSE_PROXY" environment variable or true if
58 "trusted_proxies" is not empty.
59
60 trusted_proxies
61 my $proxies = $server->trusted_proxies;
62 $server = $server->trusted_proxies(['10.0.0.0/8', '127.0.0.1', '172.16.0.0/12', '192.168.0.0/16', 'fc00::/7']);
63
64 This server expects requests from trusted reverse proxies, defaults to
65 the value of the "MOJO_TRUSTED_PROXIES" environment variable split on
66 commas with optional whitespace. These proxies should be addresses or
67 networks in CIDR form.
68
70 Mojo::Server inherits all methods from Mojo::EventEmitter and
71 implements the following new ones.
72
73 build_app
74 my $app = $server->build_app('MyApp');
75 my $app = $server->build_app('MyApp', log => Mojo::Log->new);
76 my $app = $server->build_app('MyApp', {log => Mojo::Log->new});
77
78 Build application from class and assign it to "app".
79
80 build_tx
81 my $tx = $server->build_tx;
82
83 Let application build a transaction.
84
85 daemonize
86 $server->daemonize;
87
88 Daemonize server process.
89
90 load_app
91 my $app = $server->load_app('/home/sri/myapp.pl');
92 my $app = $server->load_app('/home/sri/myapp.pl', log => Mojo::Log->new);
93 my $app = $server->load_app('/home/sri/myapp.pl', {log => Mojo::Log->new});
94
95 Load application from script and assign it to "app".
96
97 say Mojo::Server->new->load_app('./myapp.pl')->home;
98
99 new
100 my $server = Mojo::Server->new;
101 my $server = Mojo::Server->new(reverse_proxy => 1);
102 my $server = Mojo::Server->new({reverse_proxy => 1});
103
104 Construct a new Mojo::Server object and subscribe to "request" event
105 with default request handling.
106
107 run
108 $server->run;
109
110 Run server. Meant to be overloaded in a subclass.
111
113 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
114
115
116
117perl v5.38.0 2023-09-11 Mojo::Server(3)