Skip to content

Instantly share code, notes, and snippets.

@brianmed
Last active August 29, 2015 14:09
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 brianmed/9efcb96574221e4837d8 to your computer and use it in GitHub Desktop.
Save brianmed/9efcb96574221e4837d8 to your computer and use it in GitHub Desktop.
Use a Mojolicious as a proxy for a site
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