| 1 | package CGI::Application::Demo::Dispatch; | ||
| 2 | |||
| 3 | # Author: | ||
| 4 | # Ron Savage <ron@savage.net.au> | ||
| 5 | |||
| 6 | 1 | our $VERSION = '1.05'; | |
| 7 | |||
| 8 | # ----------------------------------------------- | ||
| 9 | |||
| 10 | 1 | 1; | |
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | C<CGI::Application::Demo::Dispatch> - Demonstrate the delights of CGI::Application::Dispatch | ||
| 15 | |||
| 16 | =head1 Synopsis | ||
| 17 | |||
| 18 | A classic CGI script: | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | |||
| 23 | use CGI; | ||
| 24 | use CGI::Application::Dispatch; | ||
| 25 | |||
| 26 | # --------------------- | ||
| 27 | |||
| 28 | my($cgi) = CGI -> new(); | ||
| 29 | |||
| 30 | CGI::Application::Dispatch -> dispatch | ||
| 31 | ( | ||
| 32 | args_to_new => {QUERY => $cgi}, | ||
| 33 | prefix => 'CGI::Application::Demo::Dispatch', | ||
| 34 | table => | ||
| 35 | [ | ||
| 36 | '' => {app => 'Menu', rm => 'display'}, | ||
| 37 | ':app' => {rm => 'initialize'}, | ||
| 38 | ':app/:rm' => {}, | ||
| 39 | ], | ||
| 40 | ); | ||
| 41 | |||
| 42 | A Plack script: | ||
| 43 | |||
| 44 | #!/usr/bin/env perl | ||
| 45 | # | ||
| 46 | # Run with: | ||
| 47 | # starman -l 127.0.0.1:5021 --workers 1 httpd/cgi-bin/cgi/application/demo/dispatch/dispatch.psgi & | ||
| 48 | # or, for more debug output: | ||
| 49 | # plackup -l 127.0.0.1:5021 httpd/cgi-bin/cgi/application/demo/dispatch/dispatch.psgi & | ||
| 50 | |||
| 51 | use strict; | ||
| 52 | use warnings; | ||
| 53 | |||
| 54 | use CGI::Application::Dispatch::PSGI; | ||
| 55 | |||
| 56 | use Plack::Builder; | ||
| 57 | |||
| 58 | # --------------------- | ||
| 59 | |||
| 60 | my($app) = CGI::Application::Dispatch -> as_psgi | ||
| 61 | ( | ||
| 62 | prefix => 'CGI::Application::Demo::Dispatch', | ||
| 63 | table => | ||
| 64 | [ | ||
| 65 | '' => {app => 'Menu', rm => 'display'}, | ||
| 66 | ':app' => {rm => 'initialize'}, | ||
| 67 | ':app/:rm' => {}, | ||
| 68 | ], | ||
| 69 | ); | ||
| 70 | |||
| 71 | builder | ||
| 72 | { | ||
| 73 | enable "Plack::Middleware::Static", | ||
| 74 | path => qr!^/(assets|favicon|yui)/!, | ||
| 75 | root => '/dev/shm/html'; | ||
| 76 | $app; | ||
| 77 | }; | ||
| 78 | |||
| 79 | A modern FCGI script: | ||
| 80 | |||
| 81 | use strict; | ||
| 82 | use warnings; | ||
| 83 | |||
| 84 | use CGI::Application::Dispatch; | ||
| 85 | use CGI::Fast; | ||
| 86 | use FCGI::ProcManager; | ||
| 87 | |||
| 88 | # --------------------- | ||
| 89 | |||
| 90 | my($proc_manager) = FCGI::ProcManager -> new({n_processes => 2}); | ||
| 91 | |||
| 92 | $proc_manager -> pm_manage(); | ||
| 93 | |||
| 94 | my($cgi); | ||
| 95 | |||
| 96 | while ($cgi = CGI::Fast -> new() ) | ||
| 97 | { | ||
| 98 | $proc_manager -> pm_pre_dispatch(); | ||
| 99 | |||
| 100 | CGI::Application::Dispatch -> dispatch | ||
| 101 | ( | ||
| 102 | args_to_new => {QUERY => $cgi}, | ||
| 103 | prefix => 'CGI::Application::Demo::Dispatch', | ||
| 104 | table => | ||
| 105 | [ | ||
| 106 | '' => {app => 'Menu', rm => 'display'}, | ||
| 107 | ':app' => {rm => 'initialize'}, | ||
| 108 | ':app/:rm' => {}, | ||
| 109 | ], | ||
| 110 | ); | ||
| 111 | |||
| 112 | $proc_manager -> pm_post_dispatch(); | ||
| 113 | } | ||
| 114 | |||
| 115 | =head1 Description | ||
| 116 | |||
| 117 | C<CGI::Application::Demo::Dispatch> demonstrates the delights CGI::Application::Dispatch. | ||
| 118 | |||
| 119 | It ships with: | ||
| 120 | |||
| 121 | =over 4 | ||
| 122 | |||
| 123 | =item Two instance scripts: dispatch.cgi and dispatch | ||
| 124 | |||
| 125 | I<dispatch.cgi> is a trivial C<CGI> script, while I<dispatch> is a fancy script which uses C<FCGI::ProcManager>. | ||
| 126 | |||
| 127 | Both use C<CGI::Application::Dispatch>. | ||
| 128 | |||
| 129 | Trivial here refers to using a classic C<CGI>-style script, while fancy refers to using a modern C<FCGID>-style script. | ||
| 130 | |||
| 131 | The word fancy was chosen because it allows you to use fancier URLs. For samples, see I<Start Testing>, below. | ||
| 132 | |||
| 133 | The scripts are shipped as ./httpd/cgi-bin/dispatch.cgi and ./htdocs/local/dispatch. | ||
| 134 | |||
| 135 | These directory names were chosen because you'll be installing I<dispatch.cgi> in your web server's cgi-bin/ | ||
| 136 | directory, whereas you'll install I<dispatch> in a directory under your web server's doc root. | ||
| 137 | |||
| 138 | For home-grown modules, I use the namespace Local::*, and for local web server scripts I use the | ||
| 139 | directory local/ under Apache's doc root. | ||
| 140 | |||
| 141 | For C<FCGID>, see http://fastcgi.coremail.cn/. | ||
| 142 | |||
| 143 | C<FCGID> is a replacement for the older C<FastCGI>. For C<FastCGI>, see http://www.fastcgi.com/drupal/. | ||
| 144 | |||
| 145 | Also, edit I<dispatch.cgi> and I<dispatch> to fix the 'use lib' line. See the I<Note> in those files for details. | ||
| 146 | |||
| 147 | =item A set of C<HTML::Template> templates: *.tmpl | ||
| 148 | |||
| 149 | See ./htdocs/assets/templates/cgi/application/demo/dispatch/*. | ||
| 150 | |||
| 151 | =item A patch to httpd.conf, if you run Apache and FCGID. | ||
| 152 | |||
| 153 | See ./httpd/conf/httpd.conf. | ||
| 154 | |||
| 155 | Yes, I realise that if you run FCGID you already have this patch installed, but there's nothing | ||
| 156 | wrong with having such information documented in various places. | ||
| 157 | |||
| 158 | =item This Perl module: C<CGI::Application::Demo::Dispatch> | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | =head1 Distributions | ||
| 163 | |||
| 164 | This module is available as a Unix-style distro (*.tgz). | ||
| 165 | |||
| 166 | See http://savage.net.au/Perl-modules/html/installing-a-module.html for | ||
| 167 | help on unpacking and installing distros. | ||
| 168 | |||
| 169 | =head1 Installation | ||
| 170 | |||
| 171 | All these assume your doc root is /dev/shm/html (/dev/shm/ is Debian's RAM disk). | ||
| 172 | This really should be read from a config file. See Base.pm line 18. | ||
| 173 | |||
| 174 | You will need to patch C<CGI::Application::Demo::Dispatch::Base>, since it where C<HTML::Template>'s | ||
| 175 | tmpl_path is stored, if using another path. | ||
| 176 | |||
| 177 | =head2 Install the module | ||
| 178 | |||
| 179 | Note: I<Build.PL> and I<Makefile.PL> refer to C<FCGI::ProcManager>. If you are not going to use | ||
| 180 | the fancy script, you don't need C<FCGI::ProcManager>. | ||
| 181 | |||
| 182 | Install C<CGI::Application::Demo::Dispatch> as you would for any C<Perl> module: | ||
| 183 | |||
| 184 | Run I<cpan>: shell>sudo cpan CGI::Application::Demo::Dispatch | ||
| 185 | |||
| 186 | or unpack the distro, and then either: | ||
| 187 | |||
| 188 | perl Build.PL | ||
| 189 | ./Build | ||
| 190 | ./Build test | ||
| 191 | sudo ./Build install | ||
| 192 | |||
| 193 | or: | ||
| 194 | |||
| 195 | perl Makefile.PL | ||
| 196 | make (or dmake) | ||
| 197 | make test | ||
| 198 | make install | ||
| 199 | |||
| 200 | =head2 Install the C<HTML::Template> files. | ||
| 201 | |||
| 202 | Copy the distro's ./htdocs/assets/ directory to your doc root. | ||
| 203 | |||
| 204 | =head2 Install the trivial instance script | ||
| 205 | |||
| 206 | Copy the distro's ./httpd/cgi-bin/dispatch.cgi to your cgi-bin/ directory, | ||
| 207 | and make I<dispatch.cgi> executable. | ||
| 208 | |||
| 209 | =head2 Install the fancy instance script | ||
| 210 | |||
| 211 | Copy the distro's ./htdocs/local/ directory to your doc root, and make I<dispatch> executable. | ||
| 212 | |||
| 213 | =head2 Configure C<Apache> to use /local/dispatch | ||
| 214 | |||
| 215 | If in fancy mode, add these to C<Apache>'s httpd.conf: | ||
| 216 | |||
| 217 | LoadModule fcgid_module modules/mod_fcgid.so | ||
| 218 | |||
| 219 | and: | ||
| 220 | |||
| 221 | <Location /local> | ||
| 222 | SetHandler fcgid-script | ||
| 223 | Options ExecCGI | ||
| 224 | Order deny,allow | ||
| 225 | Deny from all | ||
| 226 | Allow from 127.0.0.1 | ||
| 227 | </Location> | ||
| 228 | |||
| 229 | Note: My use of '/local' is not mandatory; you could use any URL fragment there. | ||
| 230 | |||
| 231 | And don't forget to restart C<Apache> after editing it's httpd.conf. | ||
| 232 | |||
| 233 | =head2 Start testing | ||
| 234 | |||
| 235 | Point your broswer at http://127.0.0.1/cgi-bin/dispatch.cgi (trivial script), or | ||
| 236 | http://127.0.0.1/local/dispatch (fancy script). | ||
| 237 | |||
| 238 | =head1 Author | ||
| 239 | |||
| 240 | C<CGI::Application::Demo::Dispatch> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2009. | ||
| 241 | |||
| 242 | Home page: http://savage.net.au/index.html | ||
| 243 | |||
| 244 | =head1 Copyright | ||
| 245 | |||
| 246 | Australian copyright (c) 2009, Ron Savage. | ||
| 247 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 248 | you can redistribute them and/or modify them under the terms of | ||
| 249 | The Artistic License, a copy of which is available at: | ||
| 250 | http://www.opensource.org/licenses/index.html | ||
| 251 | |||
| 252 | =cut |