diff options
author | Daniel Friesel <derf@finalrewind.org> | 2022-04-14 11:16:43 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2022-04-14 11:18:18 +0200 |
commit | e2753e2a9604e9687e1ba8dda26174cc013d75c8 (patch) | |
tree | a377b331c0c4692cbec9dd0b79e869be3cb9112c | |
parent | dd1143470f634a05fcbcbc2f0724b219026add77 (diff) |
stationboard: directly render json
The render_to_string path is only needed for legacy callback requests.
With this change, renderer->compression(0) is no longer required for
non-callback json requests to work.
-rw-r--r-- | lib/DBInfoscreen.pm | 3 | ||||
-rw-r--r-- | lib/DBInfoscreen/Controller/Stationboard.pm | 54 |
2 files changed, 25 insertions, 32 deletions
diff --git a/lib/DBInfoscreen.pm b/lib/DBInfoscreen.pm index 519e837..7730f90 100644 --- a/lib/DBInfoscreen.pm +++ b/lib/DBInfoscreen.pm @@ -43,7 +43,8 @@ sub startup { chomp $self->config->{version}; # Generally, the reverse proxy handles compression. - # Also, Mojolicious compression breaks JSON endpoints for some clients. + # Also, Mojolicious compression breaks legacy callback-based JSON endpoints + # for some clients. $self->renderer->compress(0); $self->hook( diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm index 7aff03b..6931e42 100644 --- a/lib/DBInfoscreen/Controller/Stationboard.pm +++ b/lib/DBInfoscreen/Controller/Stationboard.pm @@ -77,13 +77,11 @@ sub handle_no_results_json { $self->res->headers->access_control_allow_origin(q{*}); my $json; if ($errstr) { - $json = $self->render_to_string( - json => { - api_version => $api_version, - version => $self->config->{version}, - error => $errstr, - } - ); + $json = { + api_version => $api_version, + version => $self->config->{version}, + error => $errstr, + }; } else { my @candidates = map { { code => $_->[0], name => $_->[1] } } @@ -91,26 +89,23 @@ sub handle_no_results_json { if ( @candidates > 1 or ( @candidates == 1 and $candidates[0]{code} ne $station ) ) { - $json = $self->render_to_string( - json => { - api_version => $api_version, - version => $self->config->{version}, - error => 'ambiguous station code/name', - candidates => \@candidates, - } - ); + $json = { + api_version => $api_version, + version => $self->config->{version}, + error => 'ambiguous station code/name', + candidates => \@candidates, + }; } else { - $json = $self->render_to_string( - json => { - api_version => $api_version, - version => $self->config->{version}, - error => ( $errstr // "Got no results for '$station'" ) - } - ); + $json = { + api_version => $api_version, + version => $self->config->{version}, + error => ( $errstr // "Got no results for '$station'" ) + }; } } if ($callback) { + $json = $self->render_to_string( json => $json ); $self->render( data => "$callback($json);", format => 'json' @@ -118,8 +113,7 @@ sub handle_no_results_json { } else { $self->render( - data => $json, - format => 'json' + json => $json, ); } return; @@ -1386,12 +1380,11 @@ sub handle_result { if ( $template eq 'json' ) { $self->res->headers->access_control_allow_origin(q{*}); - my $json = $self->render_to_string( - json => { - departures => \@departures, - } - ); + my $json = { + departures => \@departures, + }; if ($callback) { + $json = $self->render_to_string( json => $json ); $self->render( data => "$callback($json);", format => 'json' @@ -1399,8 +1392,7 @@ sub handle_result { } else { $self->render( - data => $json, - format => 'json' + json => $json, ); } } |