From db5736ca98d5d6e9de2b590bfd93796a71824ac7 Mon Sep 17 00:00:00 2001 From: Difrex Date: Mon, 1 Feb 2016 10:58:00 +0300 Subject: [PATCH] initial --- umbrella.pl | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 umbrella.pl diff --git a/umbrella.pl b/umbrella.pl new file mode 100644 index 0000000..536dcc0 --- /dev/null +++ b/umbrella.pl @@ -0,0 +1,126 @@ +# Copyright © 2015-2016 Difrex + +use strict; +use warnings; + +use Plack::Builder; +use Plack::Request; +use Plack::Response; + +use Template; + +use Search::Elasticsearch; +use II::Config; + +use Encode qw(decode encode); + +my $config = II::Config->new()->load(); + +# Connect to localhost:9200: +my $e = Search::Elasticsearch->new( + nodes => [$config->{elastic_host}] +); + +# Template::Toolkit +my $tt = Template->new({ + INCLUDE_PATH => 't', + EVAL_PERL => 1, + }) || die $Template::ERROR, "\n"; + +# Debug +use Data::Dumper; + +my $root = sub { + my $env = shift; + + my $req = Plack::Request->new($env); + my $query = $req->param('query'); + + my $vars = {}; + + my $b = ''; + my $body = ''; + if ($query) { + $vars->{query} = $query; + my @messages = search($query); + foreach my $hit (@messages) { + $vars->{score} = $hit->{_score}; + $vars->{message} = $hit->{_source}->{message}; + $vars->{msgid} = $hit->{_source}->{msgid}; + $vars->{author} = $hit->{_source}->{author}; + $vars->{to} = $hit->{_source}->{to}; + $vars->{subg} = $hit->{_source}->{subg}; + $vars->{echo} = $hit->{_source}->{echo}; + $vars->{time} = gmtime($hit->{_source}->{date}); + + $tt->process('body.tpl', $vars, \$b); + $body .= $b; + $b = ''; + } + + print Dumper @messages; + } + + + my $h = ''; + my $form = ''; + my $f = ''; + $body = encode("UTF-8", $body); + $tt->process('header.tpl', $vars, \$h); + $tt->process('footer.tpl', $vars, \$f); + $tt->process('form.tpl', $vars, \$form); + return [ 200, [ 'Content-type' => 'text/html' ], [$h.$form.$body.$f], ]; +}; + +# Mountpoints +# ########### +builder { + mount '/' => $root; +}; + + +# Functions +# ######### +sub search { + my $req = shift; + + my $orig_req = $req; + $req = decode('UTF-8', $req); + my $results = $e->search( + index => $config->{elastic_index}, + type => 'post', + body => { + query => { + match => { _all => "$req" } + } + } + ); + + my @m; + foreach my $h ($results->{hits}->{hits}) { + foreach my $hit (@$h) { + print Dumper $hit; + $hit->{_source}->{message} =~ s/\n/
\n/g; + $hit->{_source}->{message} =~ s/^$/
\n/g; + + if ( $hit->{_score} <= 1 ) { + $hit->{_score} = ''; + } + elsif ( $hit->{_score} >= 1 and $hit->{_score} < 1.5 ) { + $hit->{_score} = '★★'; + } + elsif ( $hit->{_score} >= 1.5 ) { + $hit->{_score} = '★★★'; + } + foreach my $keyword (split / /, $req) { + $hit->{_source}->{post} =~ s/$keyword/$keyword<\/font>/g; + $hit->{_source}->{message} =~ s/$keyword/$keyword<\/font>/g; + $hit->{_source}->{subg} =~ s/$keyword/$keyword<\/font>/g; + } + push @m, $hit; + } + } + + return @m; +} +