-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebservice_pg.pl
executable file
·68 lines (52 loc) · 1.71 KB
/
webservice_pg.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl -w
#
use DBI;
use Data::Dumper;
{
package MyWebServer;
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
my %dispatch = (
'/hello' => \&resp_hello,
'/reverse' => \&reverse
);
sub handle_request {
my $self = shift;
my $cgi = shift;
my $path = $cgi->path_info();
my $handler = $dispatch{$path};
if (ref($handler) eq "CODE") {
print "HTTP/1.0 200 OK\r\n";
$handler->($cgi);
} else {
print "HTTP/1.0 404 Not found\r\n";
print $cgi->header,
$cgi->start_html('Not found'),
$cgi->h1('Not found'),
$cgi->end_html;
}
}
sub resp_hello {
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
my $who = $cgi->param('name');
print $cgi->header,
$cgi->start_html("Hello"),
$cgi->h1("Hello $who!"),
$cgi->end_html;
}
sub reverse
{
my $cgi = shift;
my $a_inverser=$cgi->param('chaine');
my $dbh=DBI->connect('dbi:Pg:port=6667') or die;
my $query="select row_to_json(row(string_agg(tmp2.regexp_matches,''))) from (SELECT regexp_matches[1],row_number from (SELECT *,row_number() over () from regexp_matches(?,'.','g')) as tmp1 order by 2 desc) as tmp2";
my $sth=$dbh->prepare($query);
$sth->execute($a_inverser);
my $result=$sth->fetchall_arrayref();
print $cgi->header,$result->[0]->[0];
}
}
# start the server on port 8080
my $pid = MyWebServer->new(8080)->run();
#select row_to_json(row(string_agg(tmp2.regexp_matches,''))) from (SELECT regexp_matches[1],row_number from (SELECT *,row_number() over () from regexp_matches('tati','.','g')) as tmp1 order by 2 desc) as tmp2;