upgrade Mojolicious to 2.x hooks
[MojoFacets.git] / lib / MojoFacets.pm
1 package MojoFacets;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.0001';
7
8 use base 'Mojolicious';
9
10 use Data::Dump qw(dump);
11 use Storable;
12 use Time::HiRes qw(time);
13
14
15 sub save_action {
16         my ($self) = @_;
17 #       warn "## before_dispatch req ",dump($tx->req->url, $tx->req->params);
18         my $path = $self->req->url->path;
19         if ( $path =~ m{/data/} ) {
20                 if ( my $params = $self->req->params ) {
21
22                         my $time = time();
23                         if ( my $time_travel = $params->param('time') ) {
24                                 warn "# time-travel to $time_travel\n";
25                                 $time = $time_travel;
26                         }
27
28                         my $actions_path = '/tmp/actions/';
29                         mkdir $actions_path unless -e $actions_path;
30                         $path =~ s{/}{.}g;
31                         $actions_path .= sprintf '%.4f%s', $time, $path;
32
33                         my $array = $params->params;
34                         if ( @$array ) {
35                                 store $array, $actions_path;
36                                 warn "SAVE $actions_path ", -s $actions_path, " bytes params = ", dump($array), $/;
37                         }
38                 }
39         }
40 }
41
42 # This method will run once at server start
43 sub startup {
44     my $self = shift;
45
46     # Routes
47     my $r = $self->routes;
48
49     # Default route
50     $r->route('/:controller/:action/:id')->to('data#index', id => 0);
51
52 #       $self->plugin( 'request_timer' );
53
54         $self->hook(
55                         after_dispatch => sub {
56                                 my ($self) = @_;
57                                 save_action( $self );
58                         }
59         );
60
61         eval 'use MojoFacets::Plugin::NYTProf';
62         if ( $@ ) {
63                 warn "profile disabled: ",substr($@,0,40) if $@;
64         } else {
65                 MojoFacets::Plugin::NYTProf->register( $self );
66         }
67 }
68
69
70
71 1;