Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
A bit more documentation, fixing problems about port in rewrite, disa…
Browse files Browse the repository at this point in the history
…dvantage: now we have port everywhere
  • Loading branch information
Getty committed Jun 30, 2012
1 parent dec7a47 commit a7f69d9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
20 changes: 19 additions & 1 deletion lib/DDG/Rewrite.pm
@@ -1,4 +1,20 @@
package DDG::Rewrite;
# ABSTRACT: A (mostly spice related) Rewrite definition in our system

=head1 SYNOPSIS
my $zci = DDG::ZeroClickInfo->new(
answer => "I'm a little teapot!",
is_cached => 1,
ttl => 500,
);
=head1 DESCRIPTION
This is the extension of the WWW::DuckDuckGo::ZeroClickInfo class, how it is used on the server side of DuckDuckGo.
It adds attributes to the ZeroClickInfo class which are not required for the "output" part of it.
=cut

use Moo;
use Carp qw( croak );
Expand Down Expand Up @@ -55,8 +71,10 @@ sub _build_nginx_conf {

my $uri = URI->new($self->parsed_to);
my $host = $uri->host;
my $port = $uri->port;
my $scheme = $uri->scheme;
my $uri_path = $self->parsed_to;
$uri_path =~ s!$scheme://$host:$port!!;
$uri_path =~ s!$scheme://$host!!;

# wrap various other things into jsonp
Expand All @@ -68,7 +86,7 @@ sub _build_nginx_conf {
$cfg .= "\techo_before_body '".$self->callback."(';\n" if $wrap_jsonp_callback;
$cfg .= "\techo_before_body '".$self->callback.qq|("';\n| if $wrap_string_callback;
$cfg .= "\trewrite ^".$self->path.($self->has_from ? $self->from : "(.*)")." ".$uri_path." break;\n";
$cfg .= "\tproxy_pass ".$scheme."://".$host."/;\n";
$cfg .= "\tproxy_pass ".$scheme."://".$host.":".$port."/;\n";
$cfg .= "\tproxy_cache_valid ".$self->proxy_cache_valid.";\n" if $self->has_proxy_cache_valid;
$cfg .= "\tproxy_ssl_session_reuse ".$self->proxy_ssl_session_reuse.";\n" if $self->has_proxy_ssl_session_reuse;
$cfg .= "\techo_after_body ');';\n" if $wrap_jsonp_callback;
Expand Down
10 changes: 5 additions & 5 deletions lib/DDG/ZeroClickInfo.pm
@@ -1,4 +1,5 @@
package DDG::ZeroClickInfo;
# ABSTRACT: DuckDuckGo server side used ZeroClickInfo class

=head1 SYNOPSIS
Expand All @@ -10,17 +11,15 @@ package DDG::ZeroClickInfo;
=head1 DESCRIPTION
This is the extension of the WWW::DuckDuckGo::ZeroClickInfo class, how it is used on the server side of DuckDuckGo.
This is the extension of the L<WWW::DuckDuckGo::ZeroClickInfo> class, how it is used on the server side of DuckDuckGo.
It adds attributes to the ZeroClickInfo class which are not required for the "output" part of it.
=cut

use Moo;
extends qw( WWW::DuckDuckGo::ZeroClickInfo );

=head1 ATTRIBUTES
=head2 is_cached
=attr is_cached
This attribute sets if the ZeroClickInfo should get cached on the caching layer of DuckDuckGo
Expand All @@ -31,7 +30,7 @@ has is_cached => (
default => sub { 0 },
);

=head2 ttl
=attr ttl
The TTL defines how long this data should get cached
Expand All @@ -48,3 +47,4 @@ has ttl => (
L<WWW::DuckDuckGo::ZeroClickInfo>
=cut
2 changes: 1 addition & 1 deletion t/50-spice.t
Expand Up @@ -42,7 +42,7 @@ is_deeply(DDGTest::Spice::Regexp->get_triggers,{

is(DDGTest::Spice::Regexp->get_nginx_conf,'location ^~ /js/spice/regexp/ {
rewrite ^/js/spice/regexp/(.*) / break;
proxy_pass http://some.api/;
proxy_pass http://some.api:80/;
}
',"Checking standard nginx_conf");

Expand Down
32 changes: 30 additions & 2 deletions t/55-rewrite.t
Expand Up @@ -47,7 +47,7 @@ is($rewrite->missing_envs ? 1 : 0,0,'Checking now not missing ENV');
is($rewrite->nginx_conf,'location ^~ /js/test/ {
echo_before_body \'test(\';
rewrite ^/js/test/([^/]+)/?(?:([^/]+)/?(?:([^/]+)|)|) /$1/?a=$2&b=$3&cb=test&ak=1 break;
proxy_pass http://some.api/;
proxy_pass http://some.api:80/;
proxy_cache_valid 418 1d;
echo_after_body \');\';
}
Expand All @@ -63,7 +63,35 @@ isa_ok($minrewrite,'DDG::Rewrite');
is($minrewrite->missing_envs ? 1 : 0,0,'Checking now not missing ENV');
is($minrewrite->nginx_conf,'location ^~ /js/test/ {
rewrite ^/js/test/(.*) /$1 break;
proxy_pass http://some.api/;
proxy_pass http://some.api:80/;
}
','Checking generated nginx.conf');

my $minrewrite_https = DDG::Rewrite->new(
path => '/js/test/',
to => 'https://some.api/$1',
);

isa_ok($minrewrite_https,'DDG::Rewrite');

is($minrewrite_https->missing_envs ? 1 : 0,0,'Checking now not missing ENV');
is($minrewrite_https->nginx_conf,'location ^~ /js/test/ {
rewrite ^/js/test/(.*) /$1 break;
proxy_pass https://some.api:443/;
}
','Checking generated nginx.conf');

my $minrewrite_with_port = DDG::Rewrite->new(
path => '/js/test2/',
to => 'http://some.api:3000/$1',
);

isa_ok($minrewrite_with_port,'DDG::Rewrite');

is($minrewrite_with_port->missing_envs ? 1 : 0,0,'Checking now not missing ENV');
is($minrewrite_with_port->nginx_conf,'location ^~ /js/test2/ {
rewrite ^/js/test2/(.*) /$1 break;
proxy_pass http://some.api:3000/;
}
','Checking generated nginx.conf');

Expand Down

0 comments on commit a7f69d9

Please sign in to comment.