Implement visitor counter

This commit is contained in:
swagg boi 2021-03-06 17:20:56 -05:00
parent 1ea5e007fa
commit e756ca6d6e
3 changed files with 26 additions and 2 deletions

2
.gitignore vendored
View file

@ -35,3 +35,5 @@ inc/
/pm_to_blib /pm_to_blib
/*.zip /*.zip
# Visitor counter
/.counts

View file

@ -91,7 +91,7 @@
+--------------------------+<br> +--------------------------+<br>
</div> </div>
<div class="inner"> <div class="inner">
<p>TODO: visitor count goes here</p> <p>You are visitor number <%= $count %>.</p>
</div> </div>
<div class="outer"> <div class="outer">
+--------------------------+<br> +--------------------------+<br>

View file

@ -6,13 +6,35 @@
use Mojolicious::Lite; use Mojolicious::Lite;
use Regexp::Common qw{net}; use Regexp::Common qw{net};
use Digest::SHA qw{sha1_hex}; use Digest::SHA qw{sha1_hex};
use Fcntl qw{:flock :seek};
use Number::Format qw{format_number};
# The main landing page; pass in the output of the fortune command # The main landing page; pass in the output of the fortune command
get '/' => sub { get '/' => sub {
my ($c) = @_; my ($c) = @_;
my $fortune = `fortune` || "huh?? no fortune for u\n"; my $fortune = `fortune` || "huh?? no fortune for u\n";
my ($count, %serverSide);
$c->render(fortune => $fortune); # Visitor counter ala the ol' count.cgi script
# TODO: clean up and variable-ize filehandles
open(IN,"+<.counts"); # Open it
flock(IN,LOCK_EX); # Lock the file (exclusive lock)
seek(IN,0,SEEK_SET); # Rewind it to the beginning
$count = <IN>; # Read only the first line
$count = ++$count; # Increment counter
truncate(IN,0); # This erases the file to length=0
seek(IN,0,SEEK_SET); # Rewind it to the beginning
print IN "$count\n"; # Write out the new count
close(IN); # Close the file.
$count = format_number($count); # Add commas
# Gather all our server-side stuff
%serverSide = (
count => $count,
fortune => $fortune
);
$c->render(%serverSide);
} => 'index'; } => 'index';
# Deprecation of IE page # Deprecation of IE page