use warnings; use strict; use feature 'switch'; use Mojolicious::Lite -signatures; use Mojo::SQLite; if ( $^O eq "openbsd" ) { require OpenBSD::Pledge; require OpenBSD::Unveil; OpenBSD::Unveil::unveil( "/", "" ) or die; OpenBSD::Unveil::unveil( "./current.db", "r" ) or die; OpenBSD::Unveil::unveil( "./stable.db", "r" ) or die; OpenBSD::Unveil::unveil( "/usr/local", "r" ) or die; # Needed to create the -shm and -wal db files. OpenBSD::Unveil::unveil( ".", "rwc" ) or die; OpenBSD::Pledge::pledge(qw( stdio dns inet rpath proc flock wpath cpath )) or die; } helper current => sub { state $sql = Mojo::SQLite->new('sqlite:current.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 $current = $c->param('current'); 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->current->db if defined $current; 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
Found <%= @$results %> results for '<%= $search %>'
View as JSON
Package Name | Path | Comment | Description |
---|---|---|---|
<%= $result->{FULLPKGNAME} %> | <%= $result->{FULLPKGPATH} %> | <%== $result->{COMMENT_MATCH} %> | <%== $result->{DESCR_MATCH} %> |