| package Kalamar::Controller::Search; |
| use Mojo::Base 'Mojolicious::Controller'; |
| |
| |
| # Query the KorAP backends and render a template |
| sub query { |
| my $c = shift; |
| |
| my $query = $c->param('q'); |
| |
| # No query |
| unless ($query) { |
| return $c->render(template => 'intro'); |
| }; |
| |
| # Base parameters for remote access |
| my %param = ( |
| query_language => scalar $c->param('ql'), |
| query => $query, |
| ); |
| |
| # May be not relevant |
| my $inspect = (scalar $c->param('action') // '') eq 'inspect' ? 1 : 0; |
| |
| # Just check the serialization non-blocking |
| if ($inspect) { |
| $c->search->trace( |
| %param => sub { |
| return $c->render(template => 'query_info'); |
| } |
| ); |
| return; |
| }; |
| |
| # Choose the snippet based on the parameter |
| my $template = scalar $c->param('snippet') ? 'snippet' : 'search'; |
| |
| # Search non-blocking |
| $c->delay( |
| sub { |
| my $delay = shift; |
| |
| # Search with a callback (async) |
| $c->search( |
| cutoff => scalar $c->param('cutoff'), |
| count => scalar $c->param('count'), |
| start_page => scalar $c->param('p'), |
| cb => $delay->begin, |
| %param |
| ) if $query; |
| |
| # Search resource (async) |
| $c->search->resource( |
| type => 'collection', |
| $delay->begin |
| ); |
| }, |
| |
| # Collected search |
| sub { |
| |
| |
| |
| # Render to the template |
| return $c->render(template => $template); |
| } |
| ); |
| }; |
| |
| |
| 1; |
| |
| |
| __END__ |
| |
| |
| |
| |
| |
| |
| # Get informations about a match |
| sub match_info { |
| my $c = shift; |
| |
| my $foundry = '*'; |
| my %query = (foundry => '*'); |
| if ($c->param('foundry')) { |
| $query{foundry} = scalar $c->param('foundry'); |
| if ($c->param('layer')) { |
| $query{layer} = scalar $c->param('layer'); |
| }; |
| if ($c->param('spans')) { |
| $query{spans} = 'true'; |
| }; |
| }; |
| |
| $c->render_later; |
| |
| # Use the API for fetching matching information non-blocking |
| $c->search->match( |
| corpus_id => $c->stash('corpus_id'), |
| doc_id => $c->stash('doc_id'), |
| match_id => $c->stash('match_id'), |
| %query, |
| |
| # Callback for async search |
| sub { |
| my $index = shift; |
| return $c->respond_to( |
| |
| # Render json if requested |
| json => sub { |
| # Add notifications to the matching json |
| # TODO: There should be a special notification engine doing that! |
| my $notes = $c->notifications(json => $index->results->[0]); |
| $c->render(json => $notes); |
| }, |
| |
| # Render html if requested |
| html => sub { |
| return $c->render( |
| layout => 'default', |
| template => 'match_info' |
| ) |
| } |
| ); |
| } |
| ); |
| }; |
| |
| 1; |
| |
| |
| __END__ |
| |
| |
| # Todo: Add X-Forwarded-For to user agent call everywhere |
| $self->hook(before_dispatch => sub { |
| my $c = shift; |
| my $h = $c->res->headers; |
| $h->header( 'Access-Control-Allow-Origin' => '*' ); |
| $h->header( 'Access-Control-Allow-Methods' => 'GET, PUT, POST, DELETE, OPTIONS' ); |
| $h->header( 'Access-Control-Max-Age' => 3600 ); |
| $h->header( 'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With' ); |
| }); |