From 2dd3a24d39e35cfe398685c4abdfd9bb85dc2a0c Mon Sep 17 00:00:00 2001 From: Dennis Eriksen Date: Fri, 1 Nov 2019 13:31:38 +0100 Subject: Initial commit --- redirect | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 redirect (limited to 'redirect') diff --git a/redirect b/redirect new file mode 100755 index 0000000..c471831 --- /dev/null +++ b/redirect @@ -0,0 +1,43 @@ +#!/bin/perl -wT +# (c) 2019 Dennis Eriksen + +use strict; +use warnings; + +use CGI; +use DBI; + +my $q = CGI->new; # create CGI object + +# create database handler +my $dbh = DBI->connect("dbi:Pg:dbname=purl") or die $DBI::errstr; + +# set the short +my $short = $ENV{REQUEST_URI}; +$short =~ s/^\///; + +# SQL Query +my $query = qq(SELECT shorts.url FROM shorts WHERE shorts.short = ?;); + +# run the query, and do an if on it +if (my $url = $dbh->selectrow_array($query, undef, $short)) { + $query = qq(UPDATE shorts SET count = count + 1, last_visited = now() WHERE short = ?;); + my $sth = $dbh->prepare( $query ); + my $rv = $sth->execute( $short ) or die $sth->errstr; + print $q->redirect($url); +} +else { + print $q->header(-status=>'404 Not found',-charset=>'utf-8'); + print < +404 Not Found + +

404 Not Found

+
nginx
+ + +notfound +} + +# disconnect from database +$dbh->disconnect(); -- cgit v1.2.3