From 78f56c1342ee8a1764a1dd3473ae9d8527fdf0cd Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Mon, 4 Sep 2023 12:54:38 +0200 Subject: [PATCH] Adding search controller. --- burguillos_info.json.example | 1 + lib/BurguillosInfo.pm | 1 + lib/BurguillosInfo/Command/index.pm | 3 +- lib/BurguillosInfo/Controller/Search.pm | 38 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/BurguillosInfo/Controller/Search.pm diff --git a/burguillos_info.json.example b/burguillos_info.json.example index 4516ed5..1abf4da 100644 --- a/burguillos_info.json.example +++ b/burguillos_info.json.example @@ -6,6 +6,7 @@ }, "base_url": "https://burguillos.info", "search_backend": "http://localhost:3303", + "search_index": "burguillos_info", "geoip_database": "/usr/share/GeoLite2-City_20230804/GeoLite2-City.mmdb", "onion_base_url": "http://example.onion"; "listen": "https:localhost:3555" diff --git a/lib/BurguillosInfo.pm b/lib/BurguillosInfo.pm index 98001e5..d724bff 100644 --- a/lib/BurguillosInfo.pm +++ b/lib/BurguillosInfo.pm @@ -49,6 +49,7 @@ sub startup ($self) { # $r->get('/:post')->to('Page#post'); $r->get('/stats')->to('Metrics#stats'); + $r->get('/search.json')->to('Search#search'); $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')->to('Attribute#get'); diff --git a/lib/BurguillosInfo/Command/index.pm b/lib/BurguillosInfo/Command/index.pm index 091bc8f..1aaf48d 100644 --- a/lib/BurguillosInfo/Command/index.pm +++ b/lib/BurguillosInfo/Command/index.pm @@ -30,8 +30,9 @@ sub run ( $self, @args ) { my $index = []; $self->_index_posts( $index, $posts ); $self->_index_categories( $index, $categories ); - $ua->put( $search_backend . '/index/' . $search_index, + my $response = $ua->put( $search_backend . '/index/' . $search_index, {} => json => $index ); + say $response->result->body; } sub _index_categories ( $self, $index, $categories ) { diff --git a/lib/BurguillosInfo/Controller/Search.pm b/lib/BurguillosInfo/Controller/Search.pm new file mode 100644 index 0000000..617e6c6 --- /dev/null +++ b/lib/BurguillosInfo/Controller/Search.pm @@ -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;