use feature 'switch'; use Mojolicious::Lite -signatures; use Mojo::SQLite; #helper unstable => sub { state $sql = Mojo::SQLite->new('sqlite:unstable.db') }; helper stable => sub { state $sql = Mojo::SQLite->new('sqlite:stable.db') }; my $query = q{ SELECT FULLPKGNAME, FULLPKGPATH, COMMENT, DESCRIPTION, highlight(ports_fts, 2, '', '') AS COMMENT_MATCH, highlight(ports_fts, 3, '', '') AS DESCR_MATCH FROM ports_fts WHERE ports_fts MATCH ? ORDER BY rank; }; get '/' => sub ($c) { my $v = $c->validation; my $search = $c->param('search'); #my $unstable = $c->param('unstable'); my $format = $c->param('format'); if ( defined $search && $search ne "" ) { #return $c->render( text => 'Bad CSRF token!', status => 403 ) # if $v->csrf_protect->has_error('csrf_token'); my $db = $c->stable->db; #$db = $c->unstable->db if defined $unstable; my $results = $db->query( $query, $search )->hashes; given ($format) { when ("json") { $c->render( json => $results ); } default { $c->render( template => 'results', search => $search, results => $results ); } } } else { $c->render( template => 'index' ); } }; app->start; __DATA__ @@ layouts/default.html.ep OpenBSD.app

OpenBSD.app - search packages

<%== content %>

@@ results.html.ep % layout 'default';

Found <%= @$results %> results for '<%= $search %>'
View as JSON

% foreach my $result (@$results) { % }
Package Name Path Comment Description
<%= $result->{FULLPKGNAME} %> <%= $result->{FULLPKGPATH} %> <%== $result->{COMMENT_MATCH} %> <%== $result->{DESCR_MATCH} %>
@@ index.html.ep % layout 'default'; Welcome!