1

Initial commit

This commit is contained in:
eleksir
2019-08-28 23:27:17 +03:00
commit 5b66e09ed8
4 changed files with 111 additions and 0 deletions

12
lib/fortune.pm Normal file
View File

@@ -0,0 +1,12 @@
package fortune;
use strict;
use vars qw/$VERSION/;
$VERSION = "1.0";
sub quote {
return ('200', 'text/plain', `fortune`);
}
1;

12
lib/ping.pm Normal file
View File

@@ -0,0 +1,12 @@
package ping;
use strict;
use vars qw/$VERSION/;
$VERSION = "1.0";
sub pong {
return ('200', 'text/plain', "pong\n");
}
1;

24
lib/utils.pm Normal file
View File

@@ -0,0 +1,24 @@
package utils;
use strict;
use vars qw/$VERSION/;
$VERSION = "1.0";
sub ip ($) {
my $env = shift;
my $msg = '';
if (defined($env->{X_REAL_IP})) {
$msg = $env->{X_REAL_IP};
} elsif (defined($env->{X_FORWARDED_FOR})) {
$msg = (split(/\,/, $env->{X_FORWARDED_FOR}, 2))[0];
} else {
$msg = $env->{REMOTE_ADDR};
}
$msg .= "\n";
return ('200', 'text/plain', $msg);
}
1;

63
myapi.psgi Normal file
View File

@@ -0,0 +1,63 @@
use Data::Dumper;
use strict;
use warnings "all";
use JSON::PP;
use threads;
use threads::shared;
# my plugins
use lib "$ENV{'HOME'}/.local/var/www/psgi/lib";
use ping;
use utils;
use fortune;
sub mythread();
my $main :shared = 0;
#unless ($main) { threads->create('mythread'); }
my $app = sub {
my $env = shift;
my $msg = "Your Opinion is very important for us, please stand by.\n";
my $status = '404';
my $content = 'text/plain';
if ($env->{PATH_INFO} eq '/d') {
$status = '200';
$msg = Dumper($env);
} elsif ($env->{PATH_INFO} eq '/ping') {
($status, $content, $msg) = ping::pong();
} elsif ($env->{PATH_INFO} eq '/ip') {
($status, $content, $msg) = utils::ip($env);
} elsif (($env->{PATH_INFO} eq '/fortune') or ($env->{PATH_INFO} eq '/quote')) {
($status, $content, $msg) = fortune::quote();
}
return [
$status,
[ 'Content-Type' => $content, 'Content-Length' => length($msg) ],
[ $msg ],
];
};
sub mythread () {
$main = 1;
threads->detach();
while (1) {
sleep 1;
open (F, ">>", '/tmp/myapi.txt');
print F time() . "\n";
close F;
}
}
# vim: set ft=perl noet ai :
__END__
https://toster.ru/q/75226
http://www.cbr.ru/scripts/XML_daily.asp