summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2008-10-26 11:30:08 +0100
committerYorhel <git@yorhel.nl>2008-10-26 11:30:08 +0100
commit0a22ed97765c4f8937abae863c36e094b2df217f (patch)
treec26652425b5d18736757b02e8d1590ceca544e5b /examples
parentda8c2ec3805c3086e68ae1f28591929b2dac2bcc (diff)
Proper 'packaging' + readme + license file
I've chosen a MIT license for this project, I'm pretty sure nobody would disagree with that.
Diffstat (limited to 'examples')
-rw-r--r--examples/VNDB/DB/Misc.pm24
-rw-r--r--examples/VNDB/Handler/Example.pm87
-rw-r--r--examples/VNDB/Handler/Forms.pm60
-rwxr-xr-xexamples/vndb.pl47
4 files changed, 218 insertions, 0 deletions
diff --git a/examples/VNDB/DB/Misc.pm b/examples/VNDB/DB/Misc.pm
new file mode 100644
index 0000000..b83a66d
--- /dev/null
+++ b/examples/VNDB/DB/Misc.pm
@@ -0,0 +1,24 @@
+
+package VNDB::DB::Misc;
+
+use strict;
+use warnings;
+use Exporter 'import';
+
+our @EXPORT = ('dbVNInfo');
+
+
+# small example showing how execute an SQL query and return the results
+sub dbVNInfo {
+ my($s, $id) = @_;
+ return $s->dbRow(q|
+ SELECT vr.id, vr.title, vr.original
+ FROM vn v
+ JOIN vn_rev vr ON vr.id = v.latest
+ WHERE v.id = ?
+ LIMIT 1|,
+ $id);
+}
+
+
+1;
diff --git a/examples/VNDB/Handler/Example.pm b/examples/VNDB/Handler/Example.pm
new file mode 100644
index 0000000..0fdc788
--- /dev/null
+++ b/examples/VNDB/Handler/Example.pm
@@ -0,0 +1,87 @@
+
+package VNDB::Handler::Example;
+
+use strict;
+use warnings;
+use YAWF ':html', ':xml';
+
+
+YAWF::register(
+ qr/envdump/, \&envdump,
+ qr/error/, \&error,
+ qr/html/, \&htmlexample,
+ qr{v([1-9]\d*)/xml}, \&vnxml,
+);
+
+
+sub envdump {
+ my $self = shift;
+ $self->resHeader('Content-Type', 'text/plain');
+ my $fd = $self->resFd;
+
+ # normally you'd use req* methods to fetch
+ # environment-specific information...
+ print $fd "ENV-Dump:\n";
+ printf $fd " %s: %s\n", $_, $ENV{$_} for (sort keys %ENV);
+
+ # ...like this
+ print $fd "\n";
+ print $fd "Header dump:\n";
+ printf $fd " %s: %s\n", $_, $self->reqHeader($_) for ($self->reqHeader());
+
+ # ..and this
+ print $fd "\n";
+ print $fd "Param dump:\n";
+ printf $fd " %s: %s\n", $_, $self->reqParam($_) for ($self->reqParam());
+}
+
+
+sub error {
+ # this handler demonstrates a function that fails, YAWF
+ # should display a 500 error page and write a detailed
+ # report to the log file
+ warn "A random warning message before the error actually occurs";
+ die "Some descriptive error message here";
+}
+
+
+sub htmlexample {
+ html;
+ head;
+ title 'HTML Output Example';
+ end;
+ body;
+ h1 'HTML Output Example';
+ p 'This is a way to output HTML...';
+ end;
+ end;
+}
+
+
+# this function will get a number as argument, this number was parsed
+# from the pattern match above (/v+/xml, we get the + here as argument)
+sub vnxml {
+ my($self, $id) = @_;
+
+ # Let's actually serve XML as text/xml
+ $self->resHeader('Content-Type' => 'text/xml');
+
+ # fetch some information about that VN,
+ # defined in VNDB::DB::Misc
+ my $v = $self->dbVNInfo($id);
+
+ # no results found, return a 404
+ return 404 if !$v->{id};
+
+ # XML output
+ xml;
+ tag 'vn', id => $id;
+ tag 'title', $v->{title};
+ tag 'original', $v->{original};
+ end;
+}
+
+
+1;
+
+
diff --git a/examples/VNDB/Handler/Forms.pm b/examples/VNDB/Handler/Forms.pm
new file mode 100644
index 0000000..11a5735
--- /dev/null
+++ b/examples/VNDB/Handler/Forms.pm
@@ -0,0 +1,60 @@
+
+package VNDB::Handler::Forms;
+
+use strict;
+use warnings;
+use YAWF ':html';
+use Data::Dumper 'Dumper';
+
+
+$Data::Dumper::Sortkeys++;
+$Data::Dumper::Terse++;
+
+
+# an alternative way of writing handlers (TIMTOWTDI)
+YAWF::register(
+qr/formtest/, sub {
+ my $self = shift;
+
+ my @rules;
+ my $rules = q{ (
+ { name => 'string1', required => 1, maxlength => 20, minlength => 10, enum => [qw|50 hundred only-valid-value|] },
+ { name => 'string2', required => 0, regex => [ qr/default/, 'fail message' ], default => 'this is a default (and matches)' },
+ { name => 'string3', required => 1, func => [ sub {
+ if(length $_[0] > 5) { $_[0] = uc($_[0]); return $_[0] } else { return undef }
+ }, 'another fail message' ] },
+ { name => 'string4', required => 1, template => 'int' },
+ )};
+ eval '@rules = '.$rules;
+
+ my $frm = $self->formValidate(@rules);
+
+ html;
+ head;
+ title 'Form Validation Test';
+ end;
+ body;
+ h1 'Form Validation Test';
+ h2 'A Random Form...';
+ form action => '/formtest', method => 'post';
+ fieldset;
+ for (1..4) {
+ label for => 'string'.$_, '#'.$_;
+ input type => 'text', id => 'string'.$_, name => 'string'.$_,
+ value => $frm->{"string$_"}, size => 50;
+ br;
+ }
+ input type => 'submit', value => 'Submit!';
+ end;
+ end;
+ h2 '@rules = ';
+ pre $rules;
+ h2 'Dumper($self->formValidate(@rules));';
+ pre Dumper $frm;
+ end;
+ end;
+},
+);
+
+
+1;
diff --git a/examples/vndb.pl b/examples/vndb.pl
new file mode 100755
index 0000000..fb4aebd
--- /dev/null
+++ b/examples/vndb.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+# this script should be able to be used as both a CGI
+# and a FastCGI script.
+
+
+package VNDB;
+
+use strict;
+use warnings;
+
+# determine root directory
+use Cwd 'abs_path';
+our $ROOT;
+BEGIN {
+ ($ROOT = abs_path $0) =~ s{/examples/vndb\.pl$}{};
+}
+
+# make sure YAWF is findable
+use lib $ROOT.'/lib';
+# and make sure our own example modules are, too
+use lib $ROOT.'/examples';
+
+
+use YAWF;
+
+
+YAWF::init(
+
+ # required
+ namespace => 'VNDB',
+ db_login => [ 'dbi:Pg:dbname=vndb', 'vndb', 'passwd' ],
+
+ # optional
+ debug => 1,
+ logfile => $ROOT.'/data/logs/err.log',
+
+ error_404_handler => \&page_404,
+);
+
+
+sub page_404 {
+ my $self = shift;
+ my $fd = $self->resFd;
+ print $fd "This is our custom 404 error page!\n";
+}
+