-
-
Save yko/a3ee562cd495996b684a to your computer and use it in GitHub Desktop.
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
#!/usr/bin/perl | |
use strict; | |
use Mojolicious::Lite; | |
get '/' => sub { | |
my $self = shift; | |
# берется переменная из адресной строки | |
my $site = $self->param('site'); | |
# 404 если нет параметра 'site' | |
return $self->render_not_found unless $site; | |
# запрашиваем страницу по адресу "http://www.google.com/search?q=site%3A$site" | |
# используя встроенный инстанс Mojo::UserAgent (ua) в неблокирующем режиме: | |
# ответ обрабатывается в калбеке | |
$self->ua->get( | |
"http://www.google.com/search?q=site%3A$site" => sub { | |
my ($ua, $tx) = @_; | |
# из полученной страницы берем содержимое блока | |
my $text = $tx->res->dom->at('div#resultStats'); | |
# Элемент найден/не найден | |
$text = $text ? $text->text : 'Not found'; | |
# Название шаблона совпадает с именем роута: index | |
$self->render(result => $text); | |
} | |
); | |
# откладываем генерацию страницы: пока google думает, | |
# наш сервер обработает запросы от других пользователей | |
$self->render_later; | |
} => 'index'; | |
app->start; | |
# ниже в этом же файле объявляем шаблон index.html.ep | |
__DATA__ | |
@@ index.html.ep | |
<%= $result %><br /> | |
<form action="/cgi-bin/get_string.pl"> | |
site name: <input type="text" name="site" value="<%= param('site') %>"/> | |
<input type="submit" /> | |
</form> | |
</code> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment