Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added publisher feature

  • Loading branch information...
commit b543f9d6b78be051d35ef0f485907760a1701cb6 1 parent 0f22fe8
@Getty Getty authored
View
62 lib/App/DuckPAN/Cmd/Publisher.pm
@@ -0,0 +1,62 @@
+package App::DuckPAN::Cmd::Publisher;
+# ABSTRACT: Starting up the publisher test webserver
+
+use Moo;
+with qw( App::DuckPAN::Cmd );
+
+use MooX::Options;
+
+use Path::Class;
+use Plack::Handler::Starman;
+
+sub run {
+ my ( $self, @args ) = @_;
+
+ print "\n\nChecking for Publisher...\n";
+
+ my $publisher_pm = file('lib','DDG','Publisher.pm')->absolute;
+ die "You must be in the root of the duckduckgo-publisher repository" unless -f $publisher_pm;
+
+ print "\n\nStarting up publisher webserver...";
+ print "\n\nYou can stop the webserver with Ctrl-C";
+ print "\n\n";
+
+ my %sites = (
+ duckduckgo => {
+ port => 5000,
+ url => "http://duckduckgo.com/",
+ },
+ dontbubbleus => {
+ port => 5001,
+ url => "http://dontbubble.us/",
+ },
+ donttrackus => {
+ port => 5002,
+ url => "http://donttrack.us/",
+ },
+ whatisdnt => {
+ port => 5003,
+ url => "http://whatisdnt.com/",
+ },
+ #fixtracking => {
+ # port => 5004,
+ # url => "http://fixtracking.com/",
+ #},
+ );
+
+ for (keys %sites) {
+ print "Serving ".$sites{$_}->{url}." on ".$sites{$_}->{port}."\n";
+ }
+
+ print "\n\n";
+
+ require App::DuckPAN::WebPublisher;
+ my $web = App::DuckPAN::WebPublisher->new(
+ app => $self->app,
+ sites => \%sites,
+ );
+ my @ports = map { $sites{$_}->{port} } keys %sites;
+ exit Plack::Handler::Starman->new(listen => [ map { ":$_" } @ports ])->run(sub { $web->run_psgi(@_) });
+}
+
+1;
View
4 lib/App/DuckPAN/Help.pm
@@ -52,6 +52,10 @@ duckpan poupload
---------------
Upload a po file to the Community Platform (Translation manager only)
+duckpan publisher
+---------------
+ Live testing of duckduckgo-publisher
+
duckpan release
---------------
Release the project of the current directory to DuckPAN [TODO]
View
97 lib/App/DuckPAN/WebPublisher.pm
@@ -0,0 +1,97 @@
+package App::DuckPAN::WebPublisher;
+# ABSTRACT: Webserver for duckpan publisher
+
+use Moo;
+with qw( App::DuckPAN::HasApp );
+
+use IO::All -utf8;
+use HTTP::Request;
+use Plack::Request;
+use Plack::Response;
+use Path::Class;
+
+has sites => ( is => 'ro', required => 1 );
+
+has ports => (
+ is => 'ro',
+ lazy => 1,
+ default => sub {
+ my ( $self ) = @_;
+ my %ports;
+ for my $key (keys %{$self->sites}) {
+ my $port = $self->sites->{$key}->{port};
+ my $url = $self->sites->{$key}->{url};
+ my $site = $self->publisher->sites->{$key};
+ $ports{$port} = {
+ site => $site,
+ url => $url,
+ };
+ }
+ return \%ports;
+ },
+);
+
+has publisher => (
+ is => 'ro',
+ lazy => 1,
+ default => sub {
+ require lib;
+ lib->import('lib');
+ require DDG::Publisher;
+ DDG::Publisher->new;
+ },
+);
+
+sub run_psgi {
+ my ( $self, $env ) = @_;
+ my $request = Plack::Request->new($env);
+ my $response = $self->request($request);
+ return $response->finalize;
+}
+
+has current_language => (
+ 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';
+
+ if (defined $site->fullpath_files->{$file}) {
+ print 'Request '.$request->path_info.' uses '.$file.' from DDG::Publisher...'."\n";
+ $body = $site->fullpath_files->{$file}->content;
+ $response->code("200");
+ $response->content_type('text/html');
+ } else {
+ my $res = $self->app->http->request(HTTP::Request->new(GET => $url.$request->request_uri));
+ if ($res->is_success) {
+ $body = $res->decoded_content;
+ $response->code($res->code);
+ $response->content_type($res->content_type);
+ } else {
+ warn $res->status_line, "\n";
+ $body = "";
+ }
+ }
+
+ Encode::_utf8_off($body);
+ $response->body($body);
+ return $response;
+}
+
+1;
Please sign in to comment.
Something went wrong with that request. Please try again.