gmnifaq

faq engine for gemini with full text search
git clone https://git.clttr.info/gmnifaq.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit 4102d2a37da9349f1842a15a1c161e69a4cb2d16
parent adbf69557321fece135394ffd331da789fc7379a
Author: René Wagner <rwagner@rw-net.de>
Date:   Sat, 21 Nov 2020 09:24:22 +0100

move config() to lib

Diffstat:
Mfaqs.pl | 18+++++-------------
Mgmnifaq.conf.example | 4++--
Mindex.pl | 14++++----------
Mlib/gmnifaq.pm | 15++++++++++++++-
Mtags.pl | 14+++-----------
5 files changed, 28 insertions(+), 37 deletions(-)

diff --git a/faqs.pl b/faqs.pl @@ -1,18 +1,14 @@ #!/usr/bin/perl # Copyright René Wagner 2020 # licenced under BSD 3-Clause licence -# https://git.sr.ht/~rwa/gmni-perl-cgi-demo +# https://git.sr.ht/~rwa/gmnifaq use strict; use DBI; use lib 'lib/'; -use gmnifaq qw(write_response footer); +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"; +config(); # enable UTF-8 mode for everything use utf8; @@ -23,10 +19,6 @@ 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, faqs(); @@ -53,7 +45,7 @@ sub sql sub faqs { - my $dbh = DBI->connect($dsn, '', '', { RaiseError => 1 }) or die $DBI::errstr; + my $dbh = DBI->connect($CONF{'dsn'}, '', '', { RaiseError => 1 }) or die $DBI::errstr; my @return; my $stmt = $dbh->prepare(sql()); @@ -78,5 +70,5 @@ sub faqs sub header { - return ( '# FAQs on '. $sitename, ''); + return ( '# FAQs on '. $CONF{'name'}, ''); } diff --git a/gmnifaq.conf.example b/gmnifaq.conf.example @@ -1,2 +1,2 @@ -$sitename = 'gemini faq'; -$siteintro = 'Welcome to gmnifaq, the simple cgi engine for your gemini capsule'; +$CONF{'name'} = 'gemini faq'; +$CONF{'intro'} = 'Welcome to gmnifaq, the simple cgi engine for your gemini capsule'; diff --git a/index.pl b/index.pl @@ -8,11 +8,7 @@ use DBI; 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"; +config(); # enable UTF-8 mode for everything use utf8; @@ -23,12 +19,10 @@ if (!defined($ENV{'SERVER_PROTOCOL'}) || $ENV{'SERVER_PROTOCOL'} ne 'GEMINI') { write_response('CGI_ERROR', 'CGI execution error', undef); } -if ( !-f 'data/data.sqlite' ) { write_response('PERMANENT_FAILURE', 'Permanent failure', undef) }; - my @body = (); push @body, header(); push @body, body(); -push @body, gmnifaq::footer(); +push @body, footer(); write_response('SUCCESS', 'text/gemini', @body); @@ -41,11 +35,11 @@ sub body sub header { - my $dbh = DBI->connect($dsn, '', '', { RaiseError => 1 }) or die $DBI::errstr; + my $dbh = DBI->connect($CONF{'dsn'}, '', '', { RaiseError => 1 }) or die $DBI::errstr; my $tagcount = $dbh->selectrow_array("SELECT count(id) from tags"); my $faqcount = $dbh->selectrow_array("SELECT count(id) from questions"); $dbh->disconnect(); - return ('# Welcome to '. $sitename, '', $siteintro, '', sprintf('We are currently serving %d FAQs categorized with %d tags!', $faqcount, $tagcount), ''); + return ('# Welcome to '. $CONF{'name'}, '', $CONF{'intro'}, '', sprintf('We are currently serving %d FAQs categorized with %d tags!', $faqcount, $tagcount), ''); } diff --git a/lib/gmnifaq.pm b/lib/gmnifaq.pm @@ -7,13 +7,15 @@ package gmnifaq; use strict; use Exporter; our @ISA = qw(Exporter); -our @EXPORT = qw(footer write_response); # automatically exported subs +our @EXPORT = qw(config footer write_response %CONF %RC); # automatically exported subs # enable UTF-8 mode for everything use utf8; binmode STDOUT, ':utf8'; binmode STDERR, ':utf8'; +our %CONF = (); + # define return codes our %RC = ( 'INPUT', 10, @@ -36,6 +38,17 @@ our %RC = ( 'CERT_NOT_VALID', 62 ); +sub config +{ + $CONF{'name'} = 'gmnifaq'; + $CONF{'intro'} = 'Welcome to gmnifaq!'; + if ( -f './gmnifaq.conf' ) { do './gmnifaq.conf'; } + + if ( !-f 'data/data.sqlite' ) { write_response('PERMANENT_FAILURE', 'Permanent failure', undef) }; + + $CONF{'dsn'} = "DBI:SQLite:dbname=data/data.sqlite"; +} + sub footer { return ('', '=> index.pl Home', '=> https://git.sr.ht/~rwa/gmnifaq powered by gmnifaq'); diff --git a/tags.pl b/tags.pl @@ -8,11 +8,7 @@ use DBI; 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"; +config(); # enable UTF-8 mode for everything use utf8; @@ -23,10 +19,6 @@ 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, tags(); @@ -38,7 +30,7 @@ exit; sub tags { - my $dbh = DBI->connect($dsn, '', '', { RaiseError => 1 }) or die $DBI::errstr; + my $dbh = DBI->connect($CONF{'dsn'}, '', '', { RaiseError => 1 }) or die $DBI::errstr; my @tags; my $stmt = $dbh->prepare('SELECT id, name, count(t_id) FROM tags LEFT JOIN tags_questions ON tags_questions.t_id = tags.id GROUP BY t_id'); @@ -60,5 +52,5 @@ sub tags sub header { - return ('# Welcome to '. $sitename, '', 'Select a tag to browse the questions associated with this tag.', '', '## Tags', ''); + return ('# Welcome to '. $CONF{'name'}, '', 'Select a tag to browse the questions associated with this tag.', '', '## Tags', ''); }