Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mrenvoize
Last active June 22, 2017 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrenvoize/8dc2e925f0c6cc1811dbd37501309887 to your computer and use it in GitHub Desktop.
Save mrenvoize/8dc2e925f0c6cc1811dbd37501309887 to your computer and use it in GitHub Desktop.
package Rebus;
use Mojo::Base 'Mojolicious';
use Mojo::Pg;
use Rebus::Schema;
use strict;
use warnings;
# Every app should have a version
our $VERSION = "17.08.02";
# Connect to database.
# We should re-impliment this as a helper to ensure
# a consistent connection throughout
has schema => sub {
my $self = shift;
my $config = $self->config;
my $dbic_connect_attrs = {quote_names => 1};
if ($config->{'dbi'}->{'driver'} eq 'Pg') {
$dbic_connect_attrs->{'pg_enable_utf8'} = 1;
$dbic_connect_attrs->{'on_connect_do'}
= ["CREATE SCHEMA IF NOT EXISTS list AUTHORIZATION $config->{'dbi'}->{'username'}", "SET search_path TO list"];
}
state $connect = Rebus::Schema->connect(
"dbi:$config->{'dbi'}->{'driver'}:"
. "database=$config->{'dbi'}->{'database'};"
. "host=$config->{'dbi'}->{'host'};"
. "port=$config->{'dbi'}->{'port'}",
"$config->{'dbi'}->{'username'}", "$config->{'dbi'}->{'password'}", $dbic_connect_attrs
);
return $connect;
};
sub startup {
my $app = shift;
# Load config
$app->plugin('Config');
# Add compression
$app->hook(
after_render => sub {
my ($c, $output, $format) = @_;
# Check if "gzip => 1" has been set in the stash
#return unless $c->stash->{gzip};
# Check if user agent accepts gzip compression
return unless ($c->req->headers->accept_encoding // '') =~ /gzip/i;
$c->res->headers->append(Vary => 'Accept-Encoding');
# Compress content with gzip
$c->res->headers->content_encoding('gzip');
gzip $output, \my $compressed;
$$output = $compressed;
}
);
# Router
my $r = $app->routes;
$r->get('/app/layout/csl-templates/templates/:csl_template')->to('resources#csl_template');
};
1;
package Rebus::Controller::Resources;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Util qw/b64_decode/;
sub csl_template {
my $c = shift;
# Cache for 1 day (for logged in users only)
$c->res->headers->cache_control('private, max-age=86400');
my $templateResult = $c->db->resultset('Preference')->find({code => 'csl_type_' . $c->param('csl_template')});
my $template = b64_decode($templateResult->content =~ s/^"(.*)"$/$1/r);
$c->res->headers->content_type('text/html');
return $c->render(text => $template);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment