use strict;
use warnings;
+use Cwd;
use Sys::Syslog;
use Data::Dumper;
use FCGI;
use Template;
-use feature 'state';
-
# Internal
use Secrets;
our $debug = 0;
-our @boards = (
- '/img/',
- '/doc/',
- );
-
-our %titles = (
- '/' => 'Root',
- '/img/' => 'Imgb.',
- '/doc/' => '2nd.',
- );
-
-our %descriptions = (
- '/' => 'Forum Index',
- '/img/' => 'Test Board',
- '/doc/' => 'Secondary Board',
- );
-
-our %sitemap_static = (
- '/' => 'index',
- '/404/' => '404',
- );
-our %sitemap;
-
-sub sitemapgen {
- %sitemap = %sitemap_static;
- foreach (@boards) {
- syslog("info", "+ $_");
- $sitemap{"$_"} = 'img';
- }
-}
-
-sub sitename {
- use Cwd;
- my $sitename = getcwd(); # pwd
- $sitename =~ s/.*\///; # /a/b/c/sitename/ => sitename
- return $sitename;
-}
-
-sub debug {
- my $sitename = $_[0];
+Triple::main() {
+ $sitename = (getcwd() =~ s/.*\///);
$debug = 1 if $sitename =~ /^test\./;
openlog($sitename, "ndelay,pid", Sys::Syslog::LOG_DAEMON);
-}
-
-sub handlers {
- my $include = $_[0];
- my $request = FCGI::Request();
- my $template;
- $template = Template->new({INCLUDE_PATH => $include}) or syslog("info", $template->error());
- return ($request, $template);
-}
-sub version {
- open my $fh, '<', $_[0] or die "Can't open file: $!";
- my $line = uc(substr(<$fh>, 0, 12));
- close $fh;
- return $line;
}
-
-sub init {
- my $sitename = sitename();
- debug($sitename);
- my ($request, $template) = handlers('tt');
- sitemapgen();
- return $sitename, $request, $template;
-}
-
-sub main() {
- my ($sitename, $request, $template) = Triple::init();
-
- my $version = Triple::version("version");
- syslog("info", "loading $version");
-
- my %static;
- {
- my $vars = {
- boards => \@Triple::boards,
- titles => \%Triple::titles,
- descriptions => \%Triple::descriptions,
- sitename => $sitename,
- version => $version,
- };
- foreach (my ($path, $page) = each %sitemap_static) {
- syslog("info", "staticify $path");
- $vars->{uri} = $path;
- my $description = $Triple::descriptions{$path} // 'No Description Retard';
- $vars->{description} = $description;
- my $pregen;
- $template->process($page, $vars, \$pregen) or syslog("info", $template->error());
- $static{$path} = $pregen;
- }
- }
-
-
- while($request->Accept() >= 0) {
- my $cgi = CGI->new;
-
- # my $method = $ENV{'REQUEST_METHOD'} || '?';
- my $ip = $ENV{'REMOTE_ADDR'} || '?';
- $ip = 'hidden' if not $Triple::debug;
-
- my $path = $ENV{'REQUEST_URI'} // '/';
- my $page = $Triple::sitemap{$path} // '404';
- my $stale = $static{$path} || '';
-
- print $cgi->header(
- -Content_Type => 'text/html',
- -charset => 'UTF-8',
- );
- if (not $stale) {
- syslog("info", "serving live");
- my $description = $Triple::descriptions{$path} // 'No Description Retard';
- my $vars = {
- uri => $path,
- description => $description,
- sitename => $sitename,
- version => $version,
- ip => $ip,
- };
- $template->process($page, $vars) or syslog("info", $template->error());
- } else {
- syslog("info", "serving stale");
- print $stale;
- }
- # my $description = $Triple::descriptions{$path} // 'No Description Retard';
- # syslog("info", "$ip $method $path template $page described as '$description'") if $Triple::debug;
- }
-}
-
-1;