App-DuckPAN
view release on metacpan or search on metacpan
Server: make failure to cache a fatal error.
Also, cleanup and improve the logging code around that area a tiny
bit.
Change: 35be9642401d9412528eb7bb0b2d481628635117
Author: Matt Miller <mwmiller@outlook.com>
Date : 2014-10-08 01:01:13 +0000
Server/Web: cache locales JS.
This makes things much faster when on a slow connection. Or so I
hear.
Change: b11bc6eeffdb8b58d21691c9398c5264f55029fb
Author: Matt Miller <mwmiller@outlook.com>
Date : 2014-10-08 00:55:54 +0000
Server: improve retrival and caching code reuse
lib/App/DuckPAN/Cmd/Server.pm view on Meta::CPAN
builder => '_build_page_info',
lazy=> 1,
);
sub _build_page_info {
my $self = shift;
my $cache_path = $self->asset_cache_path;
return +{
js => [],
locales => [],
css => [],
templates => [{
name => 'Template Compiling JS',
internal => $cache_path->child('template_compiler.js'),
# stored locally, no need to make web request for this
external => undef,
desc => 'Small script DuckPAN runs on SERP load; compiles Spice IA templates',
},
],
root => [{
lib/App/DuckPAN/Cmd/Server.pm view on Meta::CPAN
# If dpan.js is not present (ie. homepage)
# make sure we serve the js rather than blocking
# the call to d.js
if ($has_ddh){
$_->attr('src','/?duckduckhack_ignore=1');
}
else {
$_->attr('src','/?duckduckhack_js=1');
}
}
elsif ($src =~ /locales/) {
$_->attr('src','/?duckduckhack_locales=1');
}
elsif (substr($src,0,1) eq '/') {
$_->attr('src','http://'.$self->hostname.''.$_->attr('src'));
}
}
}
my @img = $root->look_down(
"_tag", "img"
);
lib/App/DuckPAN/Cmd/Server.pm view on Meta::CPAN
};
}
elsif ($src =~ m/^\/((?:g\d+|serp)\.js)/) {
unshift @{$self->page_info->{templates}},
{
name => 'Templating JS',
internal => $cache_path->child($1),
external => $1
};
}
elsif ($src =~ m/^\/(locales(?:.*)\.js)/) {
my $long_path = $1;
my $cache_name = $long_path;
$cache_name =~ s#^.+(\.\d+\.\d+\.js)#locales$1#g; # Turn long path into cacheable name
unshift @{$self->page_info->{locales}},
{
name => 'Locales JS',
internal => $cache_path->child($cache_name),
external => $long_path
};
}
}
}
my @cssfile;
lib/App/DuckPAN/Cmd/Server.pm view on Meta::CPAN
my $name = $_;
unshift @{$self->page_info->{css}},
{
name => $name . ' CSS',
internal => $cache_path->child($name),
external => $name
};
}
# Check if we need to request any new assets from hostname, otherwise use cached copies
foreach my $curr_asset (grep { defined $_ && $_->{internal} } map { @{$self->page_info->{$_}} } (qw(js templates css locales))) {
$self->retrieve_and_cache($curr_asset, $from);
}
}
sub retrieve_and_cache {
my ($self, $asset, $sub_of) = @_;
return unless ($asset->{internal} && $asset->{external});
my $to_file = $asset->{internal};
lib/App/DuckPAN/Web.pm view on Meta::CPAN
use HTTP::Headers;
use LWP::UserAgent;
use URI::Escape;
use JSON;
has blocks => ( is => 'ro', required => 1 );
has page_root => ( is => 'ro', required => 1 );
has page_spice => ( is => 'ro', required => 1 );
has page_css => ( is => 'ro', required => 1 );
has page_js => ( is => 'ro', required => 1 );
has page_locales => ( is => 'ro', required => 1 );
has page_templates => ( is => 'ro', required => 1 );
has server_hostname => ( is => 'ro', required => 0 );
has _our_hostname => ( is => 'rw' );
has _share_dir_hash => ( is => 'rw' );
has _path_hash => ( is => 'rw' );
has _rewrite_hash => ( is => 'rw' );
has ua => (
is => 'ro',
lib/App/DuckPAN/Web.pm view on Meta::CPAN
$body = "";
}
elsif ($request->param('duckduckhack_css')) {
$response->content_type('text/css');
$body = $self->page_css;
}
elsif ($request->param('duckduckhack_js')) {
$response->content_type('text/javascript');
$body = $self->page_js;
}
elsif ($request->param('duckduckhack_locales')) {
$response->content_type('text/javascript');
$body = $self->page_locales;
}
elsif ($request->param('duckduckhack_templates')) {
$response->content_type('text/javascript');
$body = $self->page_templates;
}
elsif ($request->param('q') && $request->path_info eq '/') {
my $query = $request->param('q');
$query =~ s/^\s+|\s+$//g; # strip leading & trailing whitespace
Encode::_utf8_on($query);
my $ddg_request = DDG::Request->new(
lib/App/DuckPAN/WebPublisher.pm view on Meta::CPAN
is => 'rw',
default => 'en_US',
);
sub request {
my ( $self, $request ) = @_;
my $response = Plack::Response->new(200);
my $body;
my $locale = defined $ENV{DDG_LOCALE} ? $ENV{DDG_LOCALE} : 'en_US';
my $site = $self->ports->{$request->port}->{site};
my $url = $self->ports->{$request->port}->{url};
$self->current_language($request->param('kad')) if $request->param('kad');
my $uri = $request->path_info eq '/' ? '/index' : $request->path_info;
$uri =~ s/\/$//;
my $file = $uri.'/'.$self->current_language.'.html';
lib/App/DuckPAN/WebStatic.pm view on Meta::CPAN
sub BUILD {
my ( $self ) = @_;
for my $site (keys %{$self->sites}) {
my $port = $self->sites->{$site}->{port};
my $url = $self->sites->{$site}->{url};
my $data_file = path($site.'.json');
die "Missing JSON publisher data file for ".$site unless $data_file->is_file;
my $data = decode_json($data_file->slurp_utf8);
my %urls;
for my $dir (keys %$data) {
next if $dir eq 'locales';
for my $filebase (keys %{$data->{$dir}}) {
for my $file (keys %{$data->{$dir}->{$filebase}}) {
next if $file eq 'static';
my $url = $data->{$dir}->{$filebase}->{$file}->{url};
my $locale = $data->{$dir}->{$filebase}->{$file}->{locale};
$urls{$url} = {} unless defined $urls{$url};
$urls{$url}->{$locale} = path($site,$dir,$file)->absolute->stringify;
}
}
}
$self->_ports->{$port} = {
base_url => $url,
urls => \%urls,
};
}
}
lib/App/DuckPAN/WebStatic.pm view on Meta::CPAN
my $response = $self->request($request);
return $response->finalize;
}
sub request {
my ( $self, $request ) = @_;
my $response = Plack::Response->new(200);
my $body;
my $locale = defined $ENV{DDG_LOCALE} ? $ENV{DDG_LOCALE} : 'en_US';
my $site = $self->_ports->{$request->port};
if ($site->{urls}->{$request->request_uri}) {
$body = path($site->{urls}->{$request->request_uri}->{$locale})->slurp_utf8;
$response->code("200");
$response->content_type('text/html');
}
else {
my $res = $self->ua->request(HTTP::Request->new(GET => $site->{base_url}.$request->request_uri));
if ($res->is_success) {
$body = $res->decoded_content;
$response->code($res->code);
$response->content_type($res->content_type);
}
( run in 1.903 second using v1.01-cache-2.11-cpan-ceb78f64989 )