Adding search controller.

This commit is contained in:
sergiotarxz 2023-09-04 12:54:38 +02:00
parent 758da4f84f
commit 78f56c1342
4 changed files with 42 additions and 1 deletions

View File

@ -6,6 +6,7 @@
}, },
"base_url": "https://burguillos.info", "base_url": "https://burguillos.info",
"search_backend": "http://localhost:3303", "search_backend": "http://localhost:3303",
"search_index": "burguillos_info",
"geoip_database": "/usr/share/GeoLite2-City_20230804/GeoLite2-City.mmdb", "geoip_database": "/usr/share/GeoLite2-City_20230804/GeoLite2-City.mmdb",
"onion_base_url": "http://example.onion"; "onion_base_url": "http://example.onion";
"listen": "https:localhost:3555" "listen": "https:localhost:3555"

View File

@ -49,6 +49,7 @@ sub startup ($self) {
# $r->get('/:post')->to('Page#post'); # $r->get('/:post')->to('Page#post');
$r->get('/stats')->to('Metrics#stats'); $r->get('/stats')->to('Metrics#stats');
$r->get('/search.json')->to('Search#search');
$r->get('/<:category>.rss')->to('Page#category_rss'); $r->get('/<:category>.rss')->to('Page#category_rss');
$r->get('/:category_slug/atributo/<:attribute_slug>-preview.png')->to('Attribute#get_attribute_preview'); $r->get('/:category_slug/atributo/<:attribute_slug>-preview.png')->to('Attribute#get_attribute_preview');
$r->get('/:category_slug/atributo/:attribute_slug')->to('Attribute#get'); $r->get('/:category_slug/atributo/:attribute_slug')->to('Attribute#get');

View File

@ -30,8 +30,9 @@ sub run ( $self, @args ) {
my $index = []; my $index = [];
$self->_index_posts( $index, $posts ); $self->_index_posts( $index, $posts );
$self->_index_categories( $index, $categories ); $self->_index_categories( $index, $categories );
$ua->put( $search_backend . '/index/' . $search_index, my $response = $ua->put( $search_backend . '/index/' . $search_index,
{} => json => $index ); {} => json => $index );
say $response->result->body;
} }
sub _index_categories ( $self, $index, $categories ) { sub _index_categories ( $self, $index, $categories ) {

View File

@ -0,0 +1,38 @@
package BurguillosInfo::Controller::Search;
use v5.34.1;
use strict;
use warnings;
use Data::Dumper;
use Mojo::Base 'Mojolicious::Controller', '-signatures';
use Mojo::UserAgent;
sub search ($self) {
my $ua = Mojo::UserAgent->new;
my $query = $self->param('q');
my $config = $self->config;
my $search_backend = $config->{search_backend};
my $search_index = $config->{search_index};
my $tx = $ua->get( $search_backend . '/search/' . $search_index,
{}, form => { q => $query } );
my $result = $tx->result;
my $output = $result->json;
if ( !defined $output ) {
return $self->render( status => 500, json => { ok => 0 } );
}
my $ok = $output->{ok};
my $reason = $output->{reason};
if ( !$ok ) {
return $self->render( status => 400, json => { ok => 0 } );
}
my $searchObjects = $output->{searchObjects};
return $self->render(
status => 200,
json => { ok => 1, searchObjects => $searchObjects }
);
}
1;