--- /dev/null
+package Newslash::Plugin::HttpCompression;
+use Mojo::Base 'Mojolicious::Plugin';
+use IO::Compress::Gzip qw(gzip);
+use List::Util qw(any);
+use Data::Dumper;
+
+sub register {
+ my ($self, $app, $conf) = @_;
+
+ # set default config value
+ my $cnf = $app->config->{HttpCompression} ||= {};
+ $cnf->{enable} = 0 if !defined $cnf->{enable};
+
+ return if !$cnf->{enable};
+
+ $app->hook(after_render => sub {
+ my ($c, $output, $format) = @_;
+
+ # Check if user agent accepts gzip compression
+ return unless ($c->req->headers->accept_encoding // '') =~ /gzip/i;
+
+ # Check if content-type can be compressed
+ if ( !any { $format eq $_ } (qw{html json})) {
+ return;
+ }
+
+ # Add header
+ $c->res->headers->append(Vary => 'Accept-Encoding');
+
+ # Compress content with gzip
+ $c->res->headers->content_encoding('gzip');
+ gzip $output, \my $compressed;
+ $$output = $compressed;
+ });
+}
+
+1;
+
+=encoding utf8
+
+=head1 NAME
+
+Newslash::Plugin::HttpCompression - gzip content-encoding support plugin
+
+=head1 SYNOPSIS
+
+ # Mojolicious
+ $app->plugin('Newslash::Plugin::HttpCompression');
+
+=head1 DESCRIPTION
+
+L<Newslash::Plugin::HttpCompression> porvides gzip content-encoding support
+to reduce network traffic.
+
+=head1 METHODS
+
+=head2 register
+
+ $plugin->register(Mojolicious->new);
+
+Register epoch in L<Mojolicious> application.
+
+=head1 SEE ALSO
+
+L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
+
+=cut