Permalink
Browse files

hello world

  • Loading branch information...
1 parent b3b84ff commit f0707bbd36886c31fcc06e1b8199cd5c52deac82 @wchristian committed Aug 6, 2012
Showing with 109 additions and 1 deletion.
  1. +3 −0 bin/scenefinity
  2. +8 −0 eg/scenefinity.pl
  3. +34 −1 lib/Web/Scenefinity.pm
  4. +63 −0 lib/Web/SimpleX/Helper/ActionWithRender.pm
  5. +1 −0 share/infinity
View
@@ -0,0 +1,3 @@
+use strictures;
+use Web::Scenefinity;
+Web::Scenefinity->run_if_script;
View
@@ -0,0 +1,8 @@
+#!/usr/bin/perl
+
+use strictures;
+use lib '../lib';
+use CGI::Carp qw(fatalsToBrowser);
+use Carp::Always;
+use Web::Scenefinity;
+Web::Scenefinity->run_if_script;
@@ -4,8 +4,41 @@ package Web::Scenefinity;
# VERSION
-# ABSTRACT:
+# ABSTRACT: web app to endlessly stream random demos
# COPYRIGHT
+use Web::Simple;
+
+use Web::SimpleX::Helper::ActionWithRender qw' mm action ';
+use File::ShareDir 'module_file';
+use Text::Xslate;
+
+sub share_dir {
+ eval { module_dir( __PACKAGE__ ) } || "../share/";
+}
+
+sub dispatch_request {
+ ( "/" => mm( action "infinity" ) );
+}
+
+sub default_view { "Xslate" }
+
+sub infinity {
+ ['infinity'];
+}
+
+sub render_Xslate {
+ my ( $self, $result ) = @_;
+ my $content = Text::Xslate->new( path => $self->share_dir )->render( @{$result} );
+ [ 200, [ "Content-Type" => "text/html; charset=utf-8" ], [$content], ];
+
+}
+
+sub view_error_Xslate {
+ my ( $self, $error ) = @_;
+ my $msg = "An error happened during rendering of the page.<br><pre>$error</pre>";
+ [ 500, [ "Content-Type" => "text/html; charset=utf-8" ], [$msg], ];
+}
+
1;
@@ -0,0 +1,63 @@
+use strictures;
+
+package Web::SimpleX::Helper::ActionWithRender;
+
+use Sub::Exporter::Simple qw'action mm';
+use Try::Tiny;
+use Scalar::Util 'blessed';
+
+sub mm {
+ my @args = @_;
+ unshift @args, 'GET' if ref $args[0];
+ for my $match ( @args ) {
+ next if ref $match;
+ next if $match =~ /HEAD/;
+ $match =~ s/GET/GET|HEAD/;
+ }
+ return sub { @args };
+}
+
+sub action {
+ my ( $action, $view ) = @_;
+
+ return sub {
+ my ( $self, @args ) = @_;
+
+ $view ||= do {
+ if ( my $meth = $self->can( 'default_view' ) ) {
+ $self->$meth;
+ }
+ else {
+ 'template';
+ }
+ };
+
+ my $view_args = try {
+ while ( ref $action and ref $action eq 'CODE' ) {
+ $action = $action->( $self, $view );
+ }
+ $self->$action( @args );
+ }
+ catch {
+ my $action_error = "action_error_$view";
+ $self->$action_error( $_ );
+ };
+
+ my $plack_response = try {
+ while ( ref $view_args and ref $view_args eq 'CODE' ) {
+ $view_args = $view_args->( $self );
+ }
+ return $view_args->finalize if blessed( $view_args ) and $view_args->isa( "Plack::Response" );
+ my $render = "render_$view";
+ return $self->$render( $view_args );
+ }
+ catch {
+ my $error_view = "view_error_$view";
+ $self->$error_view( $_ );
+ };
+
+ return $plack_response;
+ };
+}
+
+1;
View
@@ -0,0 +1 @@
+hello world

0 comments on commit f0707bb

Please sign in to comment.