diff options
author | franck cuny <franck@lumberjaph.net> | 2010-09-01 12:09:11 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-09-01 12:09:11 +0200 |
commit | f312b51dcc2ef23cbeb57d3060c7d8ca87ff9526 (patch) | |
tree | cbf6cc5b9bf0212d09ecd1e598d9caf5e6ad2697 | |
download | plack-middleware-transaction-master.tar.gz |
initial import master
-rw-r--r-- | lib/Plack/Middleware/Transaction.pm | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/Plack/Middleware/Transaction.pm b/lib/Plack/Middleware/Transaction.pm new file mode 100644 index 0000000..7432adb --- /dev/null +++ b/lib/Plack/Middleware/Transaction.pm @@ -0,0 +1,61 @@ +package Plack::Middleware::Transaction; + +use strict; +use warnings; + +use Data::UUID::LibUUID; +use Sys::Hostname; + +use parent qw/Plack::Middleware/; +use Plack::Util::Accessor qw/host/; + +sub prepare_app { + my $self = shift; + $self->host(hostname()); +} + +sub call { + my ( $self, $env ) = @_; + + my $uuid = new_uuid_string(); + + my $revision = `git rev-parse HEAD` || undef; + chomp $revision if ($revision); + + $self->response_cb( + $self->app->($env), + sub { + my $res = shift; + my $headers = $res->[1]; + + if ($self->host) { + $self->logger( $env, "[$uuid] is running on host" . $self->host ); + Plack::Util::header_set( $headers, 'X-Hostname', $self->host ); + } + + if ($revision) { + $self->logger( $env, "[$uuid] is on git revision " . $revision ); + Plack::Util::header_set( $headers, 'X-Revision', $revision ); + } + + $self->logger( $env, "[$uuid] Transaction completed" ); + Plack::Util::header_set( $headers, 'X-Transaction', $uuid ); + return $res; + } + ); +} + +sub logger { + my ( $self, $env, $message ) = @_; + + if ( $env->{'psgix.logger'} ) { + $env->{'psgix.logger'}->( + { + level => 'info', + message => $message, + } + ); + } +} + +1; |