hit-counter/myapp.pl

68 lines
1.5 KiB
Perl
Raw Normal View History

2024-03-15 19:54:21 -07:00
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
my $counter_file = 'counter.numb';
my $counter = 0;
my $number_length = 6;
app->hook(before_server_start => sub ($server, $app) {
if (! (-e $counter_file)) {
2024-03-17 18:33:07 -07:00
write_counter();
2024-03-15 19:54:21 -07:00
}
2024-03-17 18:33:07 -07:00
read_counter();
2024-03-15 19:54:21 -07:00
-e '/bin/montage' or die 'ImageMagick not installed? `/bin/montage`';
});
get '/' => sub ($c) {
2024-03-17 18:33:07 -07:00
read_counter(); # hypnotoad or w/e may run on threads or something. awkward.
2024-03-15 19:54:21 -07:00
make_image(to_number_length($counter++));
$c->res->headers->header('Content-Security-Policy' => 'img-src * artemis.venus.place');
$c->res->headers->header('Server' => 'nginx/1.22.1'); # lie :)
$c->reply->file('tmp/counter.png');
2024-03-17 18:33:07 -07:00
write_counter();
};
sub write_counter () {
2024-03-15 19:54:21 -07:00
open my $fh, '>', $counter_file;
syswrite $fh, $counter, length $counter, 0;
close $fh;
2024-03-17 18:33:07 -07:00
}
2024-03-15 19:54:21 -07:00
2024-03-17 18:33:07 -07:00
sub read_counter () {
open my $fh, '<', $counter_file;
2024-03-17 18:33:07 -07:00
sysread $fh, $counter, 20; # 20 bytes to read which is more than enough for a counter
close $fh;
}
2024-03-15 19:54:21 -07:00
sub to_number_length ($counter) {
while (length $counter lt 6) {
$counter = "0$counter";
}
return $counter;
}
## returns path for image
sub make_image ($counter) {
my @args;
for my $i (split(//, $counter)) {
push @args, "asset/$i.png";
}
push @args, qw( -tile 6x1 -geometry +0+0 -background none -scale 50 );
my $o = 'tmp/counter.png';
push @args, $o;
if (! (-w 'tmp/' and -d 'tmp/')) {
mkdir 'tmp';
}
# user can refresh the page faster than this can run
# I guess in theory someone can DOS me just by refreshing the page enough times
system('/bin/montage', @args);
return $o;
}
app->start;