From e2753e2a9604e9687e1ba8dda26174cc013d75c8 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 14 Apr 2022 11:16:43 +0200 Subject: 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. --- lib/DBInfoscreen.pm | 3 +- 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, ); } } -- cgit v1.2.3