move configurable variables in code
[MojoFacets.git] / lib / MojoFacets.pm
index dd80788..cfddc99 100644 (file)
@@ -8,6 +8,37 @@ our $VERSION = '0.0001';
 use base 'Mojolicious';
 
 use Data::Dump qw(dump);
+use Storable;
+use Time::HiRes qw(time);
+
+
+sub save_tx {
+       my ($self,$tx) = @_;
+#      warn "## before_dispatch req ",dump($tx->req->url, $tx->req->params);
+       my $parts = $tx->req->url->path->parts;
+       warn "# parts ",dump( $parts );
+       if ( $parts->[0] eq 'data' ) {
+               if ( my $params = $tx->req->params ) {
+
+                       warn "# params ",dump($params);
+
+                       my $time = time();
+                       if ( my $time_travel = $params->param('time') ) {
+                               warn "# time-travel to $time_travel from ", $tx->remote_address;
+                               $time = $time_travel;
+                       }
+
+                       my $path = '/tmp/actions/';
+                       mkdir $path unless -e $path;
+                       $path .= sprintf '%.4f.%s', $time, join('.', @$parts);
+
+                       store $params, $path;
+                       warn "$path ", -s $path, " bytes\n";
+               }
+       }
+}
+
+use MojoFacets::Plugin::NYTProf;
 
 # This method will run once at server start
 sub startup {
@@ -17,7 +48,7 @@ sub startup {
     my $r = $self->routes;
 
     # Default route
-    $r->route('/:controller/:action/:id')->to('data#index', id => 1);
+    $r->route('/:controller/:action/:id')->to('data#index', id => 0);
 
 #      $self->plugin( 'request_timer' );
 
@@ -25,12 +56,13 @@ sub startup {
                        before_dispatch => sub {
                                my ($self, $c) = @_;
                                my $tx = $c->tx;
-                               # Do whatever you want with the transaction here
-                               if ( $tx->req->url->query ) {
-                                       warn "# before_dispatch url ",dump($tx->req->url);
-                               }
+                               save_tx( $self, $tx );
                        }
        );
+
+       MojoFacets::Plugin::NYTProf->register( $self );
 }
 
+
+
 1;