-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.pl
53 lines (40 loc) · 1.57 KB
/
web.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/perl
use warnings;
use strict;
use POE qw(Component::Server::TCP Filter::HTTPD);
use HTTP::Response;
use JSON;
# Spawn a web server on port 8088 of all interfaces.
POE::Component::Server::TCP->new(
Alias => "web_server",
Port => 8088,
ClientFilter => 'POE::Filter::HTTPD',
# The ClientInput function is called to deal with client input.
# Because this server uses POE::Filter::HTTPD to parse input,
# ClientInput will receive HTTP requests.
ClientInput => sub {
my ($kernel, $heap, $request) = @_[KERNEL, HEAP, ARG0];
# Filter::HTTPD sometimes generates HTTP::Response objects.
# They indicate (and contain the response for) errors that occur
# while parsing the client's HTTP request. It's easiest to send
# the responses as they are and finish up.
if ($request->isa("HTTP::Response")) {
$heap->{client}->put($request);
$kernel->yield("shutdown");
return;
}
my $response = HTTP::Response->new(200);
my $json = JSON->new->allow_nonref;
my $value = { name => "ivan", poe => "rocks!" };
my $content = $json->pretty->encode($value);
$response->push_header('Content-type', 'application/json');
$response->content($content);
# Once the content has been built, send it back to the client
# and schedule a shutdown.
$heap->{client}->put($response);
$kernel->yield("shutdown");
}
);
# Start POE. This will run the server until it exits.
$poe_kernel->run();
exit 0;