diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-08-29 17:10:32 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-08-29 17:10:32 +0100 |
commit | 07d3568a02b5efb4286bccd9f869c8e1605928b5 (patch) | |
tree | 85770a4d1d8745a06b3bb8849c5444bb200f5693 | |
parent | 759b5ef970546c0a70fb4f8d051b1f6dd54e4d2d (diff) | |
parent | 77d4d7096a3f0f83e5c19260a320fd2b11455a48 (diff) |
Merge branch 'some-dev-improvements'
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | app.psgi | 9 | ||||
-rwxr-xr-x | bin/make_css | 6 | ||||
-rw-r--r-- | cpanfile | 6 | ||||
-rw-r--r-- | cpanfile.snapshot | 131 | ||||
-rw-r--r-- | perllib/Catalyst/Plugin/Compress/Gzip.pm | 82 | ||||
-rw-r--r-- | perllib/Catalyst/TraitFor/Model/DBIC/Schema/QueryLog/AdoptPlack.pm | 128 | ||||
-rw-r--r-- | perllib/FixMyStreet/App.pm | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Model/DB.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/FixMyStreet.pm | 2 | ||||
-rwxr-xr-x | script/server | 3 |
12 files changed, 287 insertions, 89 deletions
diff --git a/.travis.yml b/.travis.yml index de9603218..13bbee380 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,7 +37,7 @@ addons: install: - .travis/install - - 'if [ "$TRAVIS_PERL_VERSION" = "5.24" ]; then cpanm --quiet --notest Devel::Cover::Report::Codecov; fi' + - 'if [ "$TRAVIS_PERL_VERSION" = "5.24" ]; then cpanm --quiet --notest https://github.com/mysociety/codecov-perl/tarball/cover-uncoverables; fi' before_script: - commonlib/bin/gettext-makemo FixMyStreet - 'if [ "$TRAVIS_PERL_VERSION" = "5.24" ]; then export HARNESS_PERL_SWITCHES="-MDevel::Cover=+ignore,local/lib/perl5,commonlib,perllib/Catalyst,perllib/DBIx,perllib/Email,perllib/Template,^t"; fi' diff --git a/CHANGELOG.md b/CHANGELOG.md index fdbb6c638..ba7225e4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - Duplicate list not loading when phone number present. #1803 - Don't list multiple fixed states all as Fixed in dropdown. #1824 - Development improvements: + - Debug toolbar added. #1823 - `switch-site` script to automate switching config.yml files. #1741 - `make_css --watch` can run custom script after each compilation. @@ -2,7 +2,14 @@ use strict; use warnings; use FixMyStreet::App; +use Plack::Builder; +use Catalyst::Utils; my $app = FixMyStreet::App->apply_default_middlewares(FixMyStreet::App->psgi_app); -$app; +builder { + enable 'Debug', panels => [ qw(Parameters Response DBIC::QueryLog CatalystLog Timer Memory) ] + if Catalyst::Utils::env_value( 'FixMyStreet::App', 'DEBUG' ); + + $app; +}; diff --git a/bin/make_css b/bin/make_css index 7c7a5290d..6a70aba01 100755 --- a/bin/make_css +++ b/bin/make_css @@ -14,7 +14,6 @@ BEGIN { use CSS::Sass; use File::ChangeNotify; use File::Find::Rule; -use File::Slurp; use Getopt::Long; use MIME::Base64; use MIME::Types; @@ -125,9 +124,10 @@ sub compile { # Write a file, only if it has changed. sub write_if_different { my ($fn, $data) = @_; - my $current = File::Slurp::read_file($fn, { binmode => ':utf8', err_mode => 'quiet' }); + $fn = path($fn); + my $current = $fn->slurp_utf8; if (!$current || $current ne $data) { - File::Slurp::write_file($fn, { binmode => ':utf8' }, $data); + $fn->spew_utf8($data); return 1; } return 0; @@ -115,6 +115,12 @@ requires 'File::ChangeNotify'; requires 'Path::Tiny', '0.104'; requires 'File::Find::Rule'; +# Modules used for development +requires 'Plack::Middleware::Debug'; +requires 'Plack::Middleware::Debug::DBIC::QueryLog'; +recommends 'Linux::Inotify2' if $^O eq 'linux'; +recommends 'Mac::FSEvents' if $^O eq 'darwin'; + # Modules used by the test suite requires 'Test::PostgreSQL'; requires 'CGI::Simple'; diff --git a/cpanfile.snapshot b/cpanfile.snapshot index a5f8a9bbb..7e516ecff 100644 --- a/cpanfile.snapshot +++ b/cpanfile.snapshot @@ -1221,6 +1221,20 @@ DISTRIBUTIONS DBIx::Class 0 ExtUtils::MakeMaker 6.72 Test::More 0 + DBIx-Class-QueryLog-1.005001 + pathname: F/FR/FREW/DBIx-Class-QueryLog-1.005001.tar.gz + provides: + DBIx::Class::QueryLog 1.005001 + DBIx::Class::QueryLog::Analyzer 1.005001 + DBIx::Class::QueryLog::NotifyOnMax 1.005001 + DBIx::Class::QueryLog::Query 1.005001 + DBIx::Class::QueryLog::Transaction 1.005001 + requirements: + DBIx::Class 0.07000 + ExtUtils::MakeMaker 0 + Moo 2 + Time::HiRes 0 + Types::Standard 0 DBIx-Class-Schema-Loader-0.07035 pathname: G/GE/GENEHACK/DBIx-Class-Schema-Loader-0.07035.tar.gz provides: @@ -4775,6 +4789,70 @@ DISTRIBUTIONS URI 1.59 parent 0 perl 5.008001 + Plack-Middleware-DBIC-QueryLog-0.05 + pathname: J/JJ/JJNAPIORK/Plack-Middleware-DBIC-QueryLog-0.05.tar.gz + provides: + Plack::Middleware::DBIC::QueryLog 0.05 + requirements: + DBIx::Class::QueryLog v1.3.0 + Data::Dump 0 + ExtUtils::MakeMaker 6.42 + HTTP::Request::Common 0 + Moo 0.009004 + Plack 0.9957 + Plack::Middleware::Debug 0 + Scalar::Util 0 + Test::Fatal 0 + Test::More 0 + perl 5.008008 + Plack-Middleware-Debug-0.16 + pathname: M/MI/MIYAGAWA/Plack-Middleware-Debug-0.16.tar.gz + provides: + Plack::Middleware::Debug 0.16 + Plack::Middleware::Debug::Base 0.16 + Plack::Middleware::Debug::CatalystLog 0.16 + Plack::Middleware::Debug::DBITrace 0.16 + Plack::Middleware::Debug::Environment 0.16 + Plack::Middleware::Debug::Memory 0.16 + Plack::Middleware::Debug::ModuleVersions 0.16 + Plack::Middleware::Debug::Panel undef + Plack::Middleware::Debug::Parameters undef + Plack::Middleware::Debug::PerlConfig 0.16 + Plack::Middleware::Debug::Response 0.16 + Plack::Middleware::Debug::Session undef + Plack::Middleware::Debug::Timer 0.16 + Plack::Middleware::Debug::TrackObjects undef + requirements: + Class::Method::Modifiers 1.05 + Data::Dump 0 + Encode 2.23 + File::ShareDir 1.00 + Module::Build::Tiny 0.026 + Plack 0 + Text::MicroTemplate 0.15 + parent 0 + perl 5.008001 + Plack-Middleware-Debug-DBIC-QueryLog-0.09 + pathname: J/JJ/JJNAPIORK/Plack-Middleware-Debug-DBIC-QueryLog-0.09.tar.gz + provides: + Plack::Middleware::Debug::DBIC::QueryLog 0.09 + requirements: + DBIx::Class::QueryLog 0 + DBIx::Class::QueryLog::Analyzer 0 + Data::Dump 0 + ExtUtils::MakeMaker 7.1002 + HTTP::Request::Common 0 + Moo 0.009005 + Plack 0 + Plack::Builder 0 + Plack::Middleware::DBIC::QueryLog 0.05 + Plack::Middleware::Debug 0 + Plack::Test 0 + SQL::Abstract 1.70 + Scalar::Util 0 + Test::Fatal 0 + Test::More 0.96 + perl 5.008008 Plack-Middleware-ReverseProxy-0.15 pathname: M/MI/MIYAGAWA/Plack-Middleware-ReverseProxy-0.15.tar.gz provides: @@ -5846,6 +5924,17 @@ DISTRIBUTIONS requirements: Module::Build 0.36 Test::More 0 + Text-MicroTemplate-0.24 + pathname: K/KA/KAZUHO/Text-MicroTemplate-0.24.tar.gz + provides: + Text::MicroTemplate 0.24 + Text::MicroTemplate::EncodedString 0.24 + Text::MicroTemplate::File undef + requirements: + ExtUtils::MakeMaker 6.59 + File::Temp 0 + Test::More 0 + perl 5.00800 Text-SimpleTable-2.03 pathname: M/MR/MRAMBERG/Text-SimpleTable-2.03.tar.gz provides: @@ -5994,6 +6083,48 @@ DISTRIBUTIONS constant 0 strict 0 warnings 0 + Type-Tiny-1.002001 + pathname: T/TO/TOBYINK/Type-Tiny-1.002001.tar.gz + provides: + Devel::TypeTiny::Perl56Compat 1.002001 + Devel::TypeTiny::Perl58Compat 1.002001 + Error::TypeTiny 1.002001 + Error::TypeTiny::Assertion 1.002001 + Error::TypeTiny::Compilation 1.002001 + Error::TypeTiny::WrongNumberOfParameters 1.002001 + Eval::TypeTiny 1.002001 + Reply::Plugin::TypeTiny 1.002001 + Test::TypeTiny 1.002001 + Type::Coercion 1.002001 + Type::Coercion::FromMoose 1.002001 + Type::Coercion::Union 1.002001 + Type::Library 1.002001 + Type::Params 1.002001 + Type::Parser 1.002001 + Type::Registry 1.002001 + Type::Tiny 1.002001 + Type::Tiny::Class 1.002001 + Type::Tiny::Duck 1.002001 + Type::Tiny::Enum 1.002001 + Type::Tiny::Intersection 1.002001 + Type::Tiny::Role 1.002001 + Type::Tiny::Union 1.002001 + Type::Utils 1.002001 + Types::Common::Numeric 1.002001 + Types::Common::String 1.002001 + Types::Standard 1.002001 + Types::Standard::ArrayRef 1.002001 + Types::Standard::CycleTuple 1.002001 + Types::Standard::Dict 1.002001 + Types::Standard::HashRef 1.002001 + Types::Standard::Map 1.002001 + Types::Standard::ScalarRef 1.002001 + Types::Standard::Tuple 1.002001 + Types::TypeTiny 1.002001 + requirements: + Exporter::Tiny 0.026 + ExtUtils::MakeMaker 6.17 + perl 5.006001 UNIVERSAL-can-1.20140328 pathname: C/CH/CHROMATIC/UNIVERSAL-can-1.20140328.tar.gz provides: diff --git a/perllib/Catalyst/Plugin/Compress/Gzip.pm b/perllib/Catalyst/Plugin/Compress/Gzip.pm deleted file mode 100644 index 06532c84c..000000000 --- a/perllib/Catalyst/Plugin/Compress/Gzip.pm +++ /dev/null @@ -1,82 +0,0 @@ -package Catalyst::Plugin::Compress::Gzip; -use strict; -use warnings; -use MRO::Compat; - -use Compress::Zlib (); - -sub finalize_headers { - my $c = shift; - - if ( $c->response->content_encoding ) { - return $c->next::method(@_); - } - - unless ( $c->response->body ) { - return $c->next::method(@_); - } - - unless ( $c->response->status == 200 ) { - return $c->next::method(@_); - } - - unless ( $c->response->content_type =~ /^text|xml$|javascript$/ ) { - return $c->next::method(@_); - } - - my $accept = $c->request->header('Accept-Encoding') || ''; - - unless ( index( $accept, "gzip" ) >= 0 ) { - return $c->next::method(@_); - } - - - my $body = $c->response->body; - eval { local $/; $body = <$body> } if ref $body; - die "Response body is an unsupported kind of reference" if ref $body; - - $c->response->body( Compress::Zlib::memGzip( $body ) ); - $c->response->content_length( length( $c->response->body ) ); - $c->response->content_encoding('gzip'); - $c->response->headers->push_header( 'Vary', 'Accept-Encoding' ); - - $c->next::method(@_); -} - -1; - -__END__ - -=head1 NAME - -Catalyst::Plugin::Compress::Gzip - Gzip response - -=head1 SYNOPSIS - - use Catalyst qw[Compress::Gzip]; - - -=head1 DESCRIPTION - -Gzip compress response if client supports it. Changed from CPAN version to -overload finalize_headers, rather than finalize. - -=head1 METHODS - -=head2 finalize_headers - -=head1 SEE ALSO - -L<Catalyst>. - -=head1 AUTHOR - -Christian Hansen, C<ch@ngmedia.com> -Matthew Somerville. - -=head1 LICENSE - -This library is free software . You can redistribute it and/or modify it under -the same terms as perl itself. - -=cut diff --git a/perllib/Catalyst/TraitFor/Model/DBIC/Schema/QueryLog/AdoptPlack.pm b/perllib/Catalyst/TraitFor/Model/DBIC/Schema/QueryLog/AdoptPlack.pm new file mode 100644 index 000000000..22509568e --- /dev/null +++ b/perllib/Catalyst/TraitFor/Model/DBIC/Schema/QueryLog/AdoptPlack.pm @@ -0,0 +1,128 @@ +# Local version to clone schema in enable_dbic_querylogging + +package Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack; +our $VERSION = "0.07"; + +use 5.008004; +use Moose::Role; +use Plack::Middleware::DBIC::QueryLog; +use Scalar::Util 'blessed'; + +with 'Catalyst::Component::InstancePerContext'; + +requires 'storage'; + +has show_missing_ql_warning => (is=>'rw', default=>1); + +sub get_querylog_from_env { + my ($self, $env) = @_; + return Plack::Middleware::DBIC::QueryLog->get_querylog_from_env($env); +} + +sub infer_env_from { + my ($self, $ctx) = @_; + if($ctx->engine->can('env')) { + return $ctx->engine->env; + } elsif($ctx->request->can('env')) { + return $ctx->request->env; + } else { return } +} + +sub enable_dbic_querylogging { + my ($self, $querylog) = @_; + my $clone = $self->clone; + $clone->storage->debugobj($querylog); + $clone->storage->debug(1); +} + +sub die_missing_querylog { + shift->show_missing_ql_warning(0); + die <<DEAD; +You asked me to querylog DBIC, but there is no querylog object in the Plack +\$env. You probably forgot to enable Plack::Middleware::Debug::DBIC::QueryLog +in your debugging panel. +DEAD +} + +sub die_not_plack { + die "Not a Plack Engine or compatible interface!" +} + +sub build_per_context_instance { + my ( $self, $ctx ) = @_; + return $self unless blessed($ctx); + + if(my $env = $self->infer_env_from($ctx)) { + if(my $querylog = $self->get_querylog_from_env($env)) { + $self->enable_dbic_querylogging($querylog); + } else { + $self->die_missing_querylog() if + $self->show_missing_ql_warning; + } + } else { + die_not_plack(); + } + + return $self; +} + +1; + +=head1 NAME + +Catalyst::TraitFor::Model::DBIC::Schema::QueryLog::AdoptPlack - Use a Plack Middleware QueryLog + +=head1 SYNOPSIS + + package MyApp::Web::Model::Schema; + use parent 'Catalyst::Model::DBIC::Schema'; + + __PACKAGE__->config({ + schema_class => 'MyApp::Schema', + traits => ['QueryLog::AdoptPlack'], + ## .. rest of configuration + }); + +=head1 DESCRIPTION + +This is a trait for L<Catalyst::Model::DBIC::Schema> which adopts a L<Plack> +created L<DBIx::Class::QueryLog> and logs SQL for a given request cycle. It is +intended to be compatible with L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog> +which you may already be using. + +It picks up the querylog from C<< $env->{'plack.middleware.dbic.querylog'} >> +or from C<< $env->{'plack.middleware.debug.dbic.querylog'} >> which is generally +provided by the L<Plack> middleware L<Plack::Middleware::Debug::DBIC::QueryLog> +In fact you will probably use these two modules together. Please see the documentation +in L<Plack::Middleware::Debug::DBIC::QueryLog> for an example. + +PLEASE NOTE: Starting with the 0.04 version of L<Plack::Middleware::Debug::DBIC::QueryLog> +we will canonicalize on C<< $env->{'plack.middleware.dbic.querylog'} >>. For now +both listed keys will work, but within a release or two the older key will warn and +prompt you to upgrade your version of L<Plack::Middleware::Debug::DBIC::QueryLog>. +Sorry for the trouble. + +=head1 SEE ALSO + +L<Plack::Middleware::Debug::DBIC::QueryLog>, +L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog>, +L<Catalyst::Model::DBIC::Schema>, +L<Plack::Middleware::Debug> + +=head1 ACKNOWLEGEMENTS + +This code inspired from L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog> +and the author owes a debt of gratitude for the original authors. + +=head1 AUTHOR + +John Napiorkowski, C<< <jjnapiork@cpan.org> >> + +=head1 COPYRIGHT & LICENSE + +Copyright 2012, John Napiorkowski + +This program is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index b7bbc8a0a..25d3d744e 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -25,7 +25,6 @@ use Catalyst ( 'Session::State::Cookie', # FIXME - we're using our own override atm 'Authentication', 'SmartURI', - 'Compress::Gzip', ); extends 'Catalyst'; diff --git a/perllib/FixMyStreet/App/Model/DB.pm b/perllib/FixMyStreet/App/Model/DB.pm index ffd867485..db8e72c27 100644 --- a/perllib/FixMyStreet/App/Model/DB.pm +++ b/perllib/FixMyStreet/App/Model/DB.pm @@ -5,6 +5,7 @@ use strict; use warnings; use FixMyStreet; +use Catalyst::Utils; use Moose; with 'Catalyst::Component::InstancePerContext'; @@ -13,6 +14,10 @@ __PACKAGE__->config( schema_class => 'FixMyStreet::DB::Schema', connect_info => sub { FixMyStreet::DB->schema->storage->dbh }, ); +__PACKAGE__->config( + traits => ['QueryLog::AdoptPlack'], +) + if Catalyst::Utils::env_value( 'FixMyStreet::App', 'DEBUG' ); sub build_per_context_instance { my ( $self, $c ) = @_; diff --git a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm index 1052bac0e..c50721334 100644 --- a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm +++ b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm @@ -21,6 +21,8 @@ sub path_to_email_templates { sub add_response_headers { my $self = shift; + # uncoverable branch true + return if $self->{c}->debug; my $csp_nonce = $self->{c}->stash->{csp_nonce} = unpack('h*', mySociety::Random::random_bytes(16, 1)); $self->{c}->res->header('Content-Security-Policy', "script-src 'self' www.google-analytics.com www.googleadservices.com 'unsafe-inline' 'nonce-$csp_nonce'") } diff --git a/script/server b/script/server index 2ed265a28..b1d72269e 100755 --- a/script/server +++ b/script/server @@ -3,4 +3,5 @@ set -e cd "$(dirname "$0")/.." -script/fixmystreet_app_server.pl --fork --restart --debug $@ +export FIXMYSTREET_APP_DEBUG=${FIXMYSTREET_APP_DEBUG=1} +bin/cron-wrapper local/bin/plackup -s Starman --listen :3000 --Reload perllib,conf/general.yml |