Last active
August 29, 2015 14:09
-
-
Save brianmed/9efcb96574221e4837d8 to your computer and use it in GitHub Desktop.
Use a Mojolicious as a proxy for a site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojolicious::Lite; | |
app->hook(before_dispatch => sub { | |
my $c = shift; | |
my $str = $c->tx->req->url->path->to_string; | |
if ("/" eq $str) { | |
return; | |
} | |
$c->stash(_path => $str); | |
$c->tx->req->url->path("/public"); | |
}); | |
get '/' => sub { | |
my $c = shift->render_later; | |
# $c->ua->proxy(...); | |
$c->inactivity_timeout(18); | |
$c->ua->connect_timeout(5); | |
$c->ua->inactivity_timeout(10); | |
$c->ua->get("https://www.google.com" => sub { | |
my ($ua, $tx) = @_; | |
$c->render(data => $tx->res->body); | |
}); | |
}; | |
get '/public' => sub { | |
my $c = shift->render_later; | |
# $c->ua->proxy(...); | |
$c->inactivity_timeout(18); | |
$c->ua->connect_timeout(5); | |
$c->ua->inactivity_timeout(10); | |
my $str = $c->stash("_path"); | |
$c->ua->get("https://www.google.com/$str" => sub { | |
my ($ua, $tx) = @_; | |
$c->render(data => $tx->res->body); | |
}); | |
}; | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment