Implement visitor counter
This commit is contained in:
parent
1ea5e007fa
commit
e756ca6d6e
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -35,3 +35,5 @@ inc/
|
|||
/pm_to_blib
|
||||
/*.zip
|
||||
|
||||
# Visitor counter
|
||||
/.counts
|
||||
|
|
|
@ -91,7 +91,7 @@
|
|||
+--------------------------+<br>
|
||||
</div>
|
||||
<div class="inner">
|
||||
<p>TODO: visitor count goes here</p>
|
||||
<p>You are visitor number <%= $count %>.</p>
|
||||
</div>
|
||||
<div class="outer">
|
||||
+--------------------------+<br>
|
||||
|
|
24
www-swagg.pl
24
www-swagg.pl
|
@ -6,13 +6,35 @@
|
|||
use Mojolicious::Lite;
|
||||
use Regexp::Common qw{net};
|
||||
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
|
||||
get '/' => sub {
|
||||
my ($c) = @_;
|
||||
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';
|
||||
|
||||
# Deprecation of IE page
|
||||
|
|
Loading…
Reference in a new issue