From 42824612a951d5683bdd9108868fb72b8fc6ac48 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Thu, 5 Apr 2012 00:30:09 +0200 Subject: [PATCH] Bug 7844 - plack intranet tooling for developers koha.psgi example and plackup.sh script to run any Koha site intranet or opac interface under plack with optional multi-process Starman server plackup.sh site-name [intranet] site-name is used to find config /etc/koha/sites/site-name/koha-conf.xml All configuration is specified in koha.psgi, which you are welcomed to edit and tune according to your development needs. When you are happy with it, rename it to site name and save it for safe-keeping. Test scenario: 1. install plack and dependencies, using ./cpanm-install.pl 2. start ./plackup.sh sitename 3. open intranet page http://localhost:5001/cgi-bin/koha/mainpage.pl and verify that it works 4. start ./plackup.sh sitename i[ntranet] 5. open OPAC http://localhost:5000/cgi-bin/koha/opac-main.pl and verify that it works 6. next step is to take a look into koha.psgi and enable additional debug modules, save file and reload page (plackup will reload code automatically) This message is included as README.plack because it's useful as quickstart guide when trying code from distribution or checkout --- misc/plack/README.plack | 31 ++++++++++++++++++ misc/plack/koha.psgi | 82 +++++++++++++++++++++++++++++++++++++++++++++++ misc/plack/plackup.sh | 53 ++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+), 0 deletions(-) create mode 100644 misc/plack/README.plack create mode 100644 misc/plack/koha.psgi create mode 100755 misc/plack/plackup.sh diff --git a/misc/plack/README.plack b/misc/plack/README.plack new file mode 100644 index 0000000..0561a7e --- /dev/null +++ b/misc/plack/README.plack @@ -0,0 +1,31 @@ +Bug 7844 - plack intranet tooling for developers + +koha.psgi example and plackup.sh script to run any Koha site +intranet or opac interface under plack with optional multi-process +Starman server + + plackup.sh site-name [intranet] + +site-name is used to find config /etc/koha/sites/site-name/koha-conf.xml + +All configuration is specified in koha.psgi, which you are welcomed to edit +and tune according to your development needs. + +When you are happy with it, rename it to site name and save it for safe-keeping. + +Test scenario: +1. install plack and dependencies, using ./cpanm-install.pl + +2. start ./plackup.sh sitename + +3. open intranet page http://localhost:5001/cgi-bin/koha/mainpage.pl + and verify that it works + +4. start ./plackup.sh sitename i[ntranet] + +5. open OPAC http://localhost:5000/cgi-bin/koha/opac-main.pl + and verify that it works + +6. next step is to take a look into koha.psgi and enable additional + debug modules, save file and reload page (plackup will reload + code automatically) diff --git a/misc/plack/koha.psgi b/misc/plack/koha.psgi new file mode 100644 index 0000000..3473f49 --- /dev/null +++ b/misc/plack/koha.psgi @@ -0,0 +1,82 @@ +#!/usr/bin/perl +use Plack::Builder; +use Plack::App::CGIBin; +use lib qw( ./lib ); +use Plack::Middleware::Debug; +use Plack::App::Directory; + +BEGIN { + +# override configuration from startup script below: +# (requires --reload option) + +$ENV{PLACK_DEBUG} = 1; # toggle debugging + +# memcache change requires restart +$ENV{MEMCACHED_SERVERS} = "localhost:11211"; +#$ENV{MEMCACHED_DEBUG} = 0; + +#$ENV{PLACK_MINIFY} = 1; + +$ENV{PROFILE_PER_PAGE} = 1; # reset persistant and profile counters after each page, like CGI +#$ENV{INTRANET} = 1; # usually passed from script + +} # BEGIN + +use C4::Context; +use C4::Languages; +use C4::Members; +use C4::Dates; +use C4::Boolean; +use C4::Letters; +use C4::Koha; +use C4::XSLT; +use C4::Branch; +use C4::Category; +=for preload +use C4::Tags; # FIXME +=cut + +use Devel::Size 0.77; # 0.71 doesn't work for Koha +my $watch_capture_regex = '(C4|Koha)'; + +sub watch_for_size { + my @watch = + map { s/^.*$watch_capture_regex/$1/; s/\//::/g; s/\.pm$//; $_ } # fix paths + grep { /$watch_capture_regex/ } + keys %INC + ; + warn "# watch_for_size ",join(' ',@watch); + return @watch; +}; + +my $CGI_ROOT = $ENV{INTRANET} ? $ENV{INTRANETDIR} : $ENV{OPACDIR}; +warn "# using Koha ", $ENV{INTRANET} ? 'intranet' : 'OPAC', " CGI from $CGI_ROOT\n"; +my $app=Plack::App::CGIBin->new(root => $CGI_ROOT); + +builder { + + enable_if { $ENV{PLACK_DEBUG} } 'Debug', panels => [ + qw(Koha Persistant), + qw(Environment Response Timer Memory), + # optional plugins (uncomment to enable) are sorted according to performance implact +# [ 'Devel::Size', for => \&watch_for_size ], # https://github.com/dpavlin/p5-plack-devel-debug-devel-size +# [ 'DBIProfile', profile => 2 ], +# [ 'DBITrace', level => 1 ], # a LOT of fine-graded SQL trace +# [ 'Profiler::NYTProf', exclude => [qw(.*\.css .*\.png .*\.ico .*\.js .*\.gif)] ], + ]; + + enable_if { $ENV{PLACK_DEBUG} } 'StackTrace'; + + enable_if { $ENV{INTRANETDIR} } "Plack::Middleware::Static", + path => qr{^/(intranet|opac)-tmpl/}, + root => "$ENV{INTRANETDIR}/koha-tmpl/"; + + enable_if { $ENV{PLACK_MINIFIER} } "Plack::Middleware::Static::Minifier", + path => qr{^/(intranet|opac)-tmpl/}, + root => "$ENV{INTRANETDIR}/koha-tmpl/"; + + mount "/cgi-bin/koha" => $app; + +}; + diff --git a/misc/plack/plackup.sh b/misc/plack/plackup.sh new file mode 100755 index 0000000..51573c5 --- /dev/null +++ b/misc/plack/plackup.sh @@ -0,0 +1,53 @@ +#!/bin/sh -xe + +# This is plack startup script for Koha + +# ./plackup.sh [site] [intranet] + +site=$1 +test ! -z "$site" && shift || site=srvgit # default + +export KOHA_CONF=/etc/koha/sites/$site/koha-conf.xml +export LOGDIR="$( sudo -u $site-koha xmlstarlet sel -t -v 'yazgfs/config/logdir' $KOHA_CONF )" +export INTRANETDIR="$( sudo -u $site-koha xmlstarlet sel -t -v 'yazgfs/config/intranetdir' $KOHA_CONF )" +export OPACDIR="$( sudo -u $site-koha xmlstarlet sel -t -v 'yazgfs/config/opacdir' $KOHA_CONF | sed 's,/cgi-bin/opac,,' )" + +dir=`dirname $0` + +# enable memcache - it's safe even on installation which don't have it +# since Koha has check on C4::Context +#export MEMCACHED_SERVERS=localhost:11211 +# pass site name as namespace to perl code +export MEMCACHED_NAMESPACE=$site +#export MEMCACHED_DEBUG=1 + +if [ ! -e "$INTRANETDIR/C4" ] ; then + echo "intranetdir in $KOHA_CONF doesn't point to Koha git checkout" + exit 1 +fi + +if [ -z "$1" ] ; then # type anything after site name for intranet! + INTRANET=0 + PORT=5000 +else + INTRANET=1 + PORT=5001 + shift # pass rest of arguments to plackup +fi +export INTRANET # pass to plack + +# uncomment to enable logging +#opt="$opt --access-log $LOGDIR/opac-access.log --error-log $LOGDIR/opac-error.log" + +# --max-requests 50 decreased from 1000 to keep memory usage sane +# --workers 4 number of cores on machine +#test "$INTRANET" != 1 && \ # don't use Starman for intranet +opt="$opt --server Starman -M FindBin --max-requests 50 --workers 4" + +# -E deployment turn off access log on STDOUT +opt="-E deployment" + +# comment out reload in production! +opt="$opt --reload -R $INTRANETDIR/C4 -R $INTRANETDIR/Koha" + +sudo -E -u $site-koha plackup --port $PORT -I $INTRANETDIR $opt $* $dir/koha.psgi -- 1.7.2.5