| 1 | package Parallel::Scoreboard::PSGI::App::JSON; | ||
| 2 | |||
| 3 | use Class::Accessor::Lite; | ||
| 4 | use JSON qw(encode_json decode_json); | ||
| 5 | use Parallel::Scoreboard; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | Class::Accessor::Lite->mk_accessors(qw(scoreboard)); | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $klass = shift; | ||
| 14 | my %args = @_; | ||
| 15 | die "mandatory parameter:scoreboard is missing" | ||
| 16 | unless $args{scoreboard}; | ||
| 17 | # build object | ||
| 18 | my $self = bless { | ||
| 19 | %args, | ||
| 20 | }, $klass; | ||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub to_app { | ||
| 25 | my $self = shift; | ||
| 26 | return sub { | ||
| 27 | my $status = $self->scoreboard->read_all; | ||
| 28 | for my $s (values %$status) { | ||
| 29 | # try to decode status (or if fails, leave it as a string) | ||
| 30 | eval { | ||
| 31 | $s = decode_json($s); | ||
| 32 | }; | ||
| 33 | } | ||
| 34 | return [ | ||
| 35 | 200, | ||
| 36 | [ 'Content-Type' => 'application/json; charset=utf-8' ], | ||
| 37 | [ encode_json($status) ], | ||
| 38 | ]; | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | 1; | ||
| 43 | __END__ |