View | Details | Raw Unified | Return to bug 20393
Collapse All | Expand All

(-)a/misc/plack/README.plack (-42 lines)
Lines 1-42 Link Here
1
Bug 7844 - plack intranet tooling for developers
2
3
koha.psgi example and plackup.sh script to run any Koha site
4
intranet or opac interface under plack with optional multi-process
5
Starman server
6
7
  plackup.sh site-name [intranet]
8
9
site-name is used to find config /etc/koha/sites/site-name/koha-conf.xml
10
11
All configuration is specified in koha.psgi, which you are welcomed to edit
12
and tune according to your development needs (enable memcache, enable/disable
13
debugging modules for plack and so on).
14
15
For deployment of opac or intranet you would probably want to take a look
16
in plackup.sh and enable starman as web server (which is pre-forking server
17
written in perl) and put some web server in front of it to serve static web
18
files (e.g. ngnix, apache)
19
20
When you are happy with it, rename koha.psgi and plackup.sh it to site name
21
and save it for safe-keeping.
22
23
This commit message is included in patch as README.plack because it includes
24
useful information for people using plack for first time.
25
26
Test scenario:
27
1. install plack and dependencies, as documented at
28
   http://wiki.koha-community.org/wiki/Plack
29
30
2. start ./plackup.sh sitename i[ntranet]
31
32
3. open intranet page http://localhost:5001/ and verify that it redirects
33
   to http://localhost:5001/cgi-bin/koha/mainpage.pl
34
35
4. start ./plackup.sh sitename
36
37
5. open OPAC http://localhost:5000/ and verify that it redirects to
38
   http://localhost:5000/cgi-bin/koha/opac-main.pl
39
40
6. next step is to take a look into koha.psgi and enable additional
41
   debug modules, save file and reload page (plackup will reload
42
   code automatically)
(-)a/misc/plack/koha.psgi (-104 lines)
Lines 1-104 Link Here
1
#!/usr/bin/perl
2
use Plack::Builder;
3
use Plack::App::CGIBin;
4
use lib qw( ./lib );
5
use Plack::Middleware::Debug;
6
use Plack::App::Directory;
7
8
use CGI qw(-utf8 ); # we will lose -utf8 under plack
9
{
10
    no warnings 'redefine';
11
    my $old_new = \&CGI::new;
12
    *CGI::new = sub {
13
        my $q = $old_new->( @_ );
14
        $CGI::PARAM_UTF8 = 1;
15
        Koha::Caches->flush_L1_caches();
16
        Koha::Cache::Memory::Lite->flush();
17
        return $q;
18
    };
19
}
20
21
BEGIN {
22
23
# override configuration from startup script below:
24
# (requires --reload option)
25
26
$ENV{PLACK_DEBUG} = 1; # toggle debugging
27
28
# memcache change requires restart
29
$ENV{MEMCACHED_SERVERS} = "localhost:11211";
30
#$ENV{MEMCACHED_DEBUG} = 0;
31
32
$ENV{PROFILE_PER_PAGE} = 1; # reset persistent and profile counters after each page, like CGI
33
#$ENV{INTRANET} = 1; # usually passed from script
34
35
#$ENV{DBI_AUTOPROXY}='dbi:Gofer:transport=null;cache=DBI::Util::CacheMemory'
36
37
} # BEGIN
38
39
use C4::Context;
40
use C4::Languages;
41
use C4::Members;
42
use C4::Boolean;
43
use C4::Letters;
44
use C4::Koha;
45
use C4::XSLT;
46
use Koha::DateUtils;
47
use Koha::Caches;
48
use Koha::Cache::Memory::Lite;
49
use Koha::Patron::Categories;
50
51
=for preload
52
use C4::Tags; # FIXME
53
=cut
54
55
use Devel::Size 0.77; # 0.71 doesn't work for Koha
56
my $watch_capture_regex = '(C4|Koha)';
57
58
sub watch_for_size {
59
	my @watch =
60
	map { s/^.*$watch_capture_regex/$1/; s/\//::/g; s/\.pm$//; $_ } # fix paths
61
	grep { /$watch_capture_regex/ }
62
	keys %INC
63
	;
64
	warn "# watch_for_size ",join(' ',@watch);
65
	return @watch;
66
};
67
68
my $CGI_ROOT = $ENV{INTRANET} ? $ENV{INTRANETDIR} : $ENV{OPACDIR};
69
warn "# using Koha ", $ENV{INTRANET} ? 'intranet' : 'OPAC', " CGI from $CGI_ROOT\n";
70
my $app=Plack::App::CGIBin->new(root => $CGI_ROOT)->to_app;
71
my $home = sub {
72
	return [ 302, [ Location => '/cgi-bin/koha/' . ( $ENV{INTRANET} ? 'mainpage.pl' : 'opac-main.pl' ) ] ];
73
};
74
75
builder {
76
77
	# please don't use plugins which are under enable_if $ENV{PLACK_DEBUG} in production!
78
	# they are known to leek memory
79
	enable_if { $ENV{PLACK_DEBUG} } 'Debug',  panels => [
80
		qw(Environment Response Timer Memory),
81
		# optional plugins (uncomment to enable) are sorted according to performance implact
82
#		[ 'Devel::Size', for => \&watch_for_size ], # https://github.com/dpavlin/p5-plack-devel-debug-devel-size
83
#		[ 'DBIProfile', profile => 2 ],
84
#		[ 'DBITrace', level => 1 ], # a LOT of fine-graded SQL trace
85
#		[ 'Profiler::NYTProf', exclude => [qw(.*\.css .*\.png .*\.ico .*\.js .*\.gif)] ],
86
	];
87
88
	# don't enable this plugin in production, since stack traces reveal too much information
89
	# about system to potential attackers!
90
	enable_if { $ENV{PLACK_DEBUG} } 'StackTrace';
91
92
	# this enables plackup or starman to serve static files and provide working plack
93
	# setup without need for front-end web server to serve static files
94
	enable_if { $ENV{INTRANETDIR} } "Plack::Middleware::Static",
95
		path => qr{^/(intranet|opac)-tmpl/},
96
		root => "$ENV{INTRANETDIR}/koha-tmpl/";
97
98
    # + is required so Plack doesn't try to prefix Plack::Middleware::
99
    enable "+Koha::Middleware::SetEnv";
100
101
	mount "/cgi-bin/koha" => $app;
102
	mount "/" => $home;
103
104
};
(-)a/misc/plack/plackup.sh (-55 lines)
Lines 1-54 Link Here
1
#!/bin/sh -e
2
3
# This is plack startup script for Koha
4
5
# ./plackup.sh [site] [intranet]
6
7
site=$1
8
test ! -z "$site" && shift || ( echo "usage: $0 [site] [i[tranet]]" ; exit 1 )
9
10
# extract useful paths from koha-conf.xml
11
export KOHA_CONF=/etc/koha/sites/$site/koha-conf.xml
12
export LOGDIR="$( sudo -u $site-koha xmlstarlet sel -t -v 'yazgfs/config/logdir' $KOHA_CONF )"
13
export INTRANETDIR="$( sudo -u $site-koha xmlstarlet sel -t -v 'yazgfs/config/intranetdir' $KOHA_CONF )"
14
export OPACDIR="$( sudo -u $site-koha xmlstarlet sel -t -v 'yazgfs/config/opacdir' $KOHA_CONF | sed 's,/cgi-bin/opac,,' )"
15
16
dir=`dirname $0`
17
18
# enable memcache - it's safe even on installation which don't have it
19
# since Koha has check on C4::Context
20
#export MEMCACHED_SERVERS=localhost:11211
21
# pass site name as namespace to perl code
22
export MEMCACHED_NAMESPACE=$site
23
#export MEMCACHED_DEBUG=1
24
25
if [ ! -e "$INTRANETDIR/C4" ] ; then
26
	echo "intranetdir in $KOHA_CONF doesn't point to Koha git checkout"
27
	exit 1
28
fi
29
30
if [ -z "$1" ] ; then # type anything after site name for intranet!
31
	INTRANET=0
32
	PORT=5000
33
else
34
	INTRANET=1
35
	PORT=5001
36
	shift # pass rest of arguments to plackup
37
fi
38
export INTRANET # pass to plack
39
40
# uncomment to enable logging
41
#opt="$opt --access-log $LOGDIR/opac-access.log --error-log $LOGDIR/opac-error.log"
42
43
# --max-requests 50 decreased from 1000 to keep memory usage sane
44
# --workers 4       number of cores on machine
45
#test "$INTRANET" != 1 && \ # don't use Starman for intranet
46
opt="$opt --server Starman -M FindBin --max-requests 50 --workers 4"
47
48
# -E deployment     turn off access log on STDOUT
49
opt="$opt -E deployment"
50
51
# comment out reload in production!
52
opt="$opt --reload -R $INTRANETDIR/C4 -R $INTRANETDIR/Koha"
53
54
sudo -E -u $site-koha plackup --port $PORT -I $INTRANETDIR -I $INTRANETDIR/installer $opt $* $dir/koha.psgi
55
- 

Return to bug 20393