summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2011-01-11 13:52:09 +0100
committerYorhel <git@yorhel.nl>2011-01-11 14:09:11 +0100
commit7098723f5ff2b0ae6862a388fb1404c739a02675 (patch)
tree622059132f85ca2f96a77dbdde8317284fb427a2 /examples
parentf8d8dfb049f5abbf0370cbce21faf4040b633e2a (diff)
Added examples/multifile.pl and MyWebsite/ErrorPages.pm
Diffstat (limited to 'examples')
-rw-r--r--examples/MyWebsite/ErrorPages.pm70
-rwxr-xr-xexamples/multifile.pl36
2 files changed, 106 insertions, 0 deletions
diff --git a/examples/MyWebsite/ErrorPages.pm b/examples/MyWebsite/ErrorPages.pm
new file mode 100644
index 0000000..ca7e500
--- /dev/null
+++ b/examples/MyWebsite/ErrorPages.pm
@@ -0,0 +1,70 @@
+
+# This example demonstrates how to create your custom error pages.
+
+
+# This file is a Perl module, and since this file is called
+# MyWebsite/ErrorPages.pm, we should use the namespace MyWebsite::Errorpages.
+package MyWebsite::ErrorPages;
+
+
+# import the html generation functions
+use TUWF ':html';
+
+
+# Set the error handlers
+TUWF::set(
+ error_500_handler => \&error_500,
+ error_404_handler => \&error_404,
+);
+
+
+# Register the URI '/500', which will explicitely die() and thus trigger a 500
+# page to be generated.
+TUWF::register(
+ qr/500/ => sub { die("This page contains an error!\n") },
+);
+
+
+# the 500 handler is called with an extra argument containing the error
+# message. You generally don't want to output this message to your visitors, as
+# it only makes sense for the developers of your website (you), and the same
+# error is already written to the log file - assuming you have the 'logfile'
+# option set.
+sub error_500 {
+ my($self, $error) = @_;
+
+ # it's a 500, so set the correct HTTP status code
+ $self->resStatus(500);
+
+ # generate a simple HTML page
+ html;
+ body;
+ h1 'Internal Server Error';
+ p 'It appears something went wrong on our side.';
+
+ # contradicting the above explanation about how you shouldn't output the
+ # error message to your visitors, we can of course still output it when
+ # running in debug mode.
+ p $error if $self->debug;
+ end;
+ end;
+}
+
+
+# and a simple 404 page
+sub error_404 {
+ my $self = shift;
+
+ $self->resStatus(404);
+ html;
+ body;
+ h1 'Our custom 404 page';
+ end;
+ end;
+}
+
+
+# this file is loaded like any other Perl module, so it will have to return a
+# true value.
+1;
+
diff --git a/examples/multifile.pl b/examples/multifile.pl
new file mode 100755
index 0000000..d04afee
--- /dev/null
+++ b/examples/multifile.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+# This example loads all code from MyWebsite/, which in contains either
+# functions that are exported to the main object or TUWF::register() or
+# TUWF::set() calls.
+
+use strict;
+use warnings;
+
+
+# See examples/singlefile.pl for an explanation on what this does
+use Cwd 'abs_path';
+our $ROOT;
+BEGIN { ($ROOT = abs_path $0) =~ s{/examples/multifile.pl$}{}; }
+use lib $ROOT.'/lib';
+use lib $ROOT.'/examples';
+
+
+# load TUWF
+use TUWF;
+
+# load all modules under MyWebsite/
+TUWF::load_recursive('MyWebsite');
+
+# Let's enable debug mode
+TUWF::set(debug => 1);
+
+# And let's enable logging
+# (Note that in practice you don't want to log to /tmp, I'll do it anyway to
+# make this example easier to setup, since /tmp is supposed to be always
+# writable from any process)
+TUWF::set(logfile => '/tmp/tuwf-multifile-example.log');
+
+# run TUWF
+TUWF::run();
+