Introduce config support, rename fortune.pm to easter_egg.pm
This commit is contained in:
5
data/myapi.json
Normal file
5
data/myapi.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"api" : {
|
||||||
|
"prefix" : "/api"
|
||||||
|
}
|
||||||
|
}
|
||||||
29
lib/easter_egg.pm
Normal file
29
lib/easter_egg.pm
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package easter_egg;
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings "all";
|
||||||
|
use vars qw/$VERSION/;
|
||||||
|
|
||||||
|
$VERSION = "1.0";
|
||||||
|
|
||||||
|
sub quote {
|
||||||
|
if (-f "/usr/games/fortune") {
|
||||||
|
return ('200', 'text/plain', `fortune`);
|
||||||
|
} else {
|
||||||
|
return ('200', 'text/plain', "No fortune here!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub chanserv {
|
||||||
|
return ('200', 'text/plain', "There is no conserves here.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
sub nickserv {
|
||||||
|
return ('200', 'text/plain', "Nick is out, please come back later.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
sub me {
|
||||||
|
return ('200', 'text/plain', "Who?\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
1;
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package fortune;
|
|
||||||
|
|
||||||
use strict;
|
|
||||||
use vars qw/$VERSION/;
|
|
||||||
|
|
||||||
$VERSION = "1.0";
|
|
||||||
|
|
||||||
sub quote {
|
|
||||||
return ('200', 'text/plain', `fortune`);
|
|
||||||
}
|
|
||||||
|
|
||||||
1;
|
|
||||||
19
myapi-confcheck.pl
Executable file
19
myapi-confcheck.pl
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/perl
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings "all";
|
||||||
|
|
||||||
|
use JSON::PP;
|
||||||
|
|
||||||
|
my $conf = "data/myapi.json";
|
||||||
|
my $jstr = "";
|
||||||
|
my $sep = $/;
|
||||||
|
$/ = '';
|
||||||
|
open (C, $conf) || die "No such file $conf\n";
|
||||||
|
$jstr = <C>;
|
||||||
|
close C;
|
||||||
|
|
||||||
|
my $c = decode_json($jstr);
|
||||||
|
|
||||||
|
my $j = JSON::PP->new->pretty->canonical->indent_length(4);
|
||||||
|
print $j->encode($c);
|
||||||
55
myapi.psgi
55
myapi.psgi
@@ -6,10 +6,13 @@ use threads;
|
|||||||
use threads::shared;
|
use threads::shared;
|
||||||
|
|
||||||
# my plugins
|
# my plugins
|
||||||
use lib "$ENV{'HOME'}/.local/var/www/psgi/lib";
|
use lib "./lib";
|
||||||
use ping;
|
use ping;
|
||||||
use utils;
|
use utils;
|
||||||
use fortune;
|
use easter_egg;
|
||||||
|
|
||||||
|
sub loadConf();
|
||||||
|
sub saveConf($);
|
||||||
|
|
||||||
sub mythread();
|
sub mythread();
|
||||||
|
|
||||||
@@ -17,6 +20,8 @@ my $main :shared = 0;
|
|||||||
|
|
||||||
#unless ($main) { threads->create('mythread'); }
|
#unless ($main) { threads->create('mythread'); }
|
||||||
|
|
||||||
|
my $CONF = loadConf();
|
||||||
|
if ($CONF->{api}->{prefix} eq '/') { $CONF->{prefix} = ''; }
|
||||||
|
|
||||||
my $app = sub {
|
my $app = sub {
|
||||||
my $env = shift;
|
my $env = shift;
|
||||||
@@ -25,15 +30,28 @@ my $app = sub {
|
|||||||
my $status = '404';
|
my $status = '404';
|
||||||
my $content = 'text/plain';
|
my $content = 'text/plain';
|
||||||
|
|
||||||
if ($env->{PATH_INFO} eq '/d') {
|
if ($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/d") {
|
||||||
$status = '200';
|
$status = '200';
|
||||||
$msg = Dumper($env);
|
$msg = Dumper($env);
|
||||||
} elsif ($env->{PATH_INFO} eq '/ping') {
|
} elsif ($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/ping") {
|
||||||
($status, $content, $msg) = ping::pong();
|
($status, $content, $msg) = ping::pong();
|
||||||
} elsif ($env->{PATH_INFO} eq '/ip') {
|
} elsif ($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/ip") {
|
||||||
($status, $content, $msg) = utils::ip($env);
|
($status, $content, $msg) = utils::ip($env);
|
||||||
} elsif (($env->{PATH_INFO} eq '/fortune') or ($env->{PATH_INFO} eq '/quote')) {
|
} elsif (($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/fortune") or
|
||||||
($status, $content, $msg) = fortune::quote();
|
($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/quote")) {
|
||||||
|
($status, $content, $msg) = easter_egg::quote();
|
||||||
|
} elsif (($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/chanserv") or
|
||||||
|
($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/chanServ") or
|
||||||
|
($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/ChanServ") or
|
||||||
|
($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/CHANSERV")) {
|
||||||
|
($status, $content, $msg) = easter_egg::chanserv();
|
||||||
|
} elsif (($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/nickserv") or
|
||||||
|
($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/nickServ") or
|
||||||
|
($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/NickServ") or
|
||||||
|
($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/NICKSERV")) {
|
||||||
|
($status, $content, $msg) = easter_egg::nickserv();
|
||||||
|
} elsif (($env->{PATH_INFO} eq "$CONF->{api}->{prefix}/me")) {
|
||||||
|
($status, $content, $msg) = easter_egg::me();
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@@ -43,6 +61,27 @@ my $app = sub {
|
|||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
sub loadConf() {
|
||||||
|
my $c = "data/myapi.json";
|
||||||
|
my $sep = $/;
|
||||||
|
$/ = '';
|
||||||
|
open (C, $c) or die "No conf at $c\n";
|
||||||
|
my $json = <C>;
|
||||||
|
close C;
|
||||||
|
return decode_json($json);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub saveConf($) {
|
||||||
|
my $c = shift;
|
||||||
|
my $file = "data/myapi.json";
|
||||||
|
my $j = JSON::PP->new->pretty->canonical->indent_length(4);
|
||||||
|
my $json = $j->encode($c);
|
||||||
|
# TODO: make it transactional
|
||||||
|
open (C, ">", $file) or die "Unable to open $file\n";
|
||||||
|
print C $json;
|
||||||
|
close C;
|
||||||
|
}
|
||||||
|
|
||||||
sub mythread () {
|
sub mythread () {
|
||||||
$main = 1;
|
$main = 1;
|
||||||
threads->detach();
|
threads->detach();
|
||||||
@@ -55,6 +94,8 @@ sub mythread () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# vim: set ft=perl noet ai :
|
# vim: set ft=perl noet ai :
|
||||||
__END__
|
__END__
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user