summaryrefslogtreecommitdiff
path: root/perl.cgi
blob: d7a1476445affa22e4bbda559a63da5dbff9af1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl
use strict;
use warnings;
use open qw< :encoding(UTF-8) >;

my $BASEDIR = "/htdocs/pastanoggin.com";
my $base_filename = "$BASEDIR/docs/base";
my $status_filename = "$BASEDIR/docs/status";

sub print_file {
	my $filename = shift;
	my $file = undef;
	open($file, "<", $filename)
		or die "$0: can't open $filename for reading: $!";
	print while (<$file>);
	die "unexpected error while reading from $filename: $!" if $!
}

sub print_header {
	my $code = shift;
	print <<~END;
	Status: $code
	Content-Type: text/html

	END
}

sub render_page {
	my ($docname, $doctitle) = @_;
	my $doc_filename = "$BASEDIR/docs/$docname";

	my $base_file = undef;
	open($base_file, "<", $base_filename)
		or die "$0: can't open $base_filename for reading: $!";

	# print header and contents
	print_header 200;
	while (<$base_file>) {
		if (/\[%title%\]/) {
			print "$doctitle";
		} elsif (/\[%content%\]/) {
			print_file $doc_filename;
		} elsif (/\[%status%\]/) {
			print_file "$status_filename";
		} else {
			print;
		}
	}
	die "unexpected error while reading from $base_filename: $!" if $!
}

sub render_error {
	my $errcode = shift;
	print_header $errcode;
	print_file "$BASEDIR/docs/${errcode}.html";
}

my $path = $ENV{'PATH_INFO'} || '/';

if ($path =~ /^\/?$/) {
	render_page 'root', 'pastanoggin!!';
} elsif ($path =~ /^\/contact\/?$/) {
	render_page 'contact', 'hello, is it me you\'re looking for?';
} elsif ($path =~ /^\/blog\/?$/) {
	render_page 'blog', 'yapyapyapyap :D';
} elsif ($path =~ /^\/links\/?$/) {
	render_page 'links', 'the interwebs';
} else {
	render_error '404';
}