gmnifaq

Unnamed repository; edit this file 'description' to name the repository.
git clone git://code.clttr.info/gmnifaq.git
Log | Files | Refs | README | LICENSE

commit adbf69557321fece135394ffd331da789fc7379a
parent ed9009e79d88ebe5eeb1ce78cedd0d1bb4fe5dd8
Author: René Wagner <rwagner@rw-net.de>
Date:   Fri, 20 Nov 2020 22:26:54 +0100

move footer() and write_response() to lib

Diffstat:
MTODO.md | 1+
Mfaqs.pl | 50++++++--------------------------------------------
Mindex.pl | 62+++++++++++---------------------------------------------------
Alib/gmnifaq.pm | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtags.pl | 48+++++-------------------------------------------
5 files changed, 81 insertions(+), 138 deletions(-)

diff --git a/TODO.md b/TODO.md @@ -1,4 +1,5 @@ # initial todo +- avoid serving of data files?! - implement display of question - single question - implement search diff --git a/faqs.pl b/faqs.pl @@ -5,31 +5,12 @@ use strict; use DBI; -#use URI::Encode qw(uri_encode uri_decode); -# define return codes -our %RC = ( - 'INPUT', 10, - 'SENSITIVE_INPUT', 11, - 'SUCCESS', 20, - 'TEMPORARY_REDIRECT', 30, - 'PERMANENT_REDIRECT', 31, - 'TEMPORARY_FAILURE', 40, - 'SERVER_UNAVAILABLE', 41, - 'CGI_ERROR', 42, - 'PROXY_ERROR', 43, - 'SLOW_DOWN', 44, - 'PERMANENT_FAILURE', 50, - 'NOT_FOUND', 51, - 'GONE', 52, - 'PROXY_REQUEST_REFUSE', 53, - 'BAD_REQUEST', 59, - 'CLIENT_CERT_REQUIRED', 60, - 'CERT_NOT_AUTHORISED', 61, - 'CERT_NOT_VALID', 62 -); - -our $sitename; -our $siteintro; +use lib 'lib/'; +use gmnifaq qw(write_response footer); + +our $sitename = 'gmnifaq'; +our $siteintro = 'Welcome to gmnifaq!'; +if ( -f './gmnifaq.conf' ) { do './gmnifaq.conf'; } my $dsn = "DBI:SQLite:dbname=data/data.sqlite"; @@ -99,22 +80,3 @@ sub header { return ( '# FAQs on '. $sitename, ''); } - -sub footer -{ - return ('', '=> index.pl Home', '=> https://git.sr.ht/~rwa/gmnifaq powered by gmnifaq'); -} - -sub write_response -{ - my ($returncode, $meta, @content) = @_; - - if (!defined($RC{$returncode})) { die "Unknown response code!"; } - - printf("%d %s\r\n", $RC{$returncode}, ($meta eq '') ? $returncode : $meta); - foreach (@content) { - print("$_\r\n"); - } - - exit; -} diff --git a/index.pl b/index.pl @@ -5,31 +5,12 @@ use strict; use DBI; - -# define return codes -our %RC = ( - 'INPUT', 10, - 'SENSITIVE_INPUT', 11, - 'SUCCESS', 20, - 'TEMPORARY_REDIRECT', 30, - 'PERMANENT_REDIRECT', 31, - 'TEMPORARY_FAILURE', 40, - 'SERVER_UNAVAILABLE', 41, - 'CGI_ERROR', 42, - 'PROXY_ERROR', 43, - 'SLOW_DOWN', 44, - 'PERMANENT_FAILURE', 50, - 'NOT_FOUND', 51, - 'GONE', 52, - 'PROXY_REQUEST_REFUSE', 53, - 'BAD_REQUEST', 59, - 'CLIENT_CERT_REQUIRED', 60, - 'CERT_NOT_AUTHORISED', 61, - 'CERT_NOT_VALID', 62 -); +use lib 'lib/'; +use gmnifaq; our $sitename = 'gmnifaq'; our $siteintro = 'Welcome to gmnifaq!'; +if ( -f './gmnifaq.conf' ) { do './gmnifaq.conf'; } my $dsn = "DBI:SQLite:dbname=data/data.sqlite"; @@ -38,28 +19,26 @@ use utf8; binmode STDOUT, ':utf8'; binmode STDERR, ':utf8'; -if (!defined($ENV{'SERVER_PROTOCOL'}) || $ENV{'SERVER_PROTOCOL'} ne 'GEMINI') -{ +if (!defined($ENV{'SERVER_PROTOCOL'}) || $ENV{'SERVER_PROTOCOL'} ne 'GEMINI') { write_response('CGI_ERROR', 'CGI execution error', undef); } - -if ( -f './gmnifaq.conf' ) { do './gmnifaq.conf'; } if ( !-f 'data/data.sqlite' ) { write_response('PERMANENT_FAILURE', 'Permanent failure', undef) }; my @body = (); push @body, header(); -push @body, '## Meta'; -push @body, ''; -push @body, 'Search'; -push @body, '=> tags.pl Tags'; -push @body, '=> faqs.pl View all'; -push @body, footer(); +push @body, body(); +push @body, gmnifaq::footer(); write_response('SUCCESS', 'text/gemini', @body); exit; +sub body +{ + return ('## Meta', '', 'Search', '=> tags.pl Tags', '=> faqs.pl View all'); +} + sub header { my $dbh = DBI->connect($dsn, '', '', { RaiseError => 1 }) or die $DBI::errstr; @@ -70,22 +49,3 @@ sub header return ('# Welcome to '. $sitename, '', $siteintro, '', sprintf('We are currently serving %d FAQs categorized with %d tags!', $faqcount, $tagcount), ''); } - -sub footer -{ - return ('', '=> https://git.sr.ht/~rwa/gmnifaq powered by gmnifaq'); -} - -sub write_response -{ - my ($returncode, $meta, @content) = @_; - - if (!defined($RC{$returncode})) { die "Unknown response code!"; } - - printf("%d %s\r\n", $RC{$returncode}, ($meta eq '') ? $returncode : $meta); - foreach (@content) { - print("$_\r\n"); - } - - exit; -} diff --git a/lib/gmnifaq.pm b/lib/gmnifaq.pm @@ -0,0 +1,58 @@ +#!/usr/bin/perl +# Copyright René Wagner 2020 +# licenced under BSD 3-Clause licence +# https://git.sr.ht/~rwa/gmni-perl-cgi-demo + +package gmnifaq; +use strict; +use Exporter; +our @ISA = qw(Exporter); +our @EXPORT = qw(footer write_response); # automatically exported subs + +# enable UTF-8 mode for everything +use utf8; +binmode STDOUT, ':utf8'; +binmode STDERR, ':utf8'; + +# define return codes +our %RC = ( + 'INPUT', 10, + 'SENSITIVE_INPUT', 11, + 'SUCCESS', 20, + 'TEMPORARY_REDIRECT', 30, + 'PERMANENT_REDIRECT', 31, + 'TEMPORARY_FAILURE', 40, + 'SERVER_UNAVAILABLE', 41, + 'CGI_ERROR', 42, + 'PROXY_ERROR', 43, + 'SLOW_DOWN', 44, + 'PERMANENT_FAILURE', 50, + 'NOT_FOUND', 51, + 'GONE', 52, + 'PROXY_REQUEST_REFUSE', 53, + 'BAD_REQUEST', 59, + 'CLIENT_CERT_REQUIRED', 60, + 'CERT_NOT_AUTHORISED', 61, + 'CERT_NOT_VALID', 62 +); + +sub footer +{ + return ('', '=> index.pl Home', '=> https://git.sr.ht/~rwa/gmnifaq powered by gmnifaq'); +} + +sub write_response +{ + my ($returncode, $meta, @content) = @_; + + if (!defined($RC{$returncode})) { die "Unknown response code!"; } + + printf("%d %s\r\n", $RC{$returncode}, ($meta eq '') ? $returncode : $meta); + foreach (@content) { + print("$_\r\n"); + } + + exit; +} + +1; diff --git a/tags.pl b/tags.pl @@ -5,31 +5,12 @@ use strict; use DBI; -#use URI::Encode qw(uri_encode uri_decode); -# define return codes -our %RC = ( - 'INPUT', 10, - 'SENSITIVE_INPUT', 11, - 'SUCCESS', 20, - 'TEMPORARY_REDIRECT', 30, - 'PERMANENT_REDIRECT', 31, - 'TEMPORARY_FAILURE', 40, - 'SERVER_UNAVAILABLE', 41, - 'CGI_ERROR', 42, - 'PROXY_ERROR', 43, - 'SLOW_DOWN', 44, - 'PERMANENT_FAILURE', 50, - 'NOT_FOUND', 51, - 'GONE', 52, - 'PROXY_REQUEST_REFUSE', 53, - 'BAD_REQUEST', 59, - 'CLIENT_CERT_REQUIRED', 60, - 'CERT_NOT_AUTHORISED', 61, - 'CERT_NOT_VALID', 62 -); +use lib 'lib/'; +use gmnifaq; -our $sitename; -our $siteintro; +our $sitename = 'gmnifaq'; +our $siteintro = 'Welcome to gmnifaq!'; +if ( -f './gmnifaq.conf' ) { do './gmnifaq.conf'; } my $dsn = "DBI:SQLite:dbname=data/data.sqlite"; @@ -81,22 +62,3 @@ sub header { return ('# Welcome to '. $sitename, '', 'Select a tag to browse the questions associated with this tag.', '', '## Tags', ''); } - -sub footer -{ - return ('', '=> index.pl Home', '=> https://git.sr.ht/~rwa/gmnifaq powered by gmnifaq'); -} - -sub write_response -{ - my ($returncode, $meta, @content) = @_; - - if (!defined($RC{$returncode})) { die "Unknown response code!"; } - - printf("%d %s\r\n", $RC{$returncode}, ($meta eq '') ? $returncode : $meta); - foreach (@content) { - print("$_\r\n"); - } - - exit; -}