openbsd.app/openbsd.app.pl

217 lines
5.3 KiB
Perl
Raw Normal View History

use warnings;
use strict;
use feature 'switch';
2022-09-23 16:45:55 -06:00
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') };
2022-09-23 16:45:55 -06:00
my $query = q{
SELECT
FULLPKGNAME,
FULLPKGPATH,
COMMENT,
DESCRIPTION,
highlight(ports_fts, 2, '<b>', '</b>') AS COMMENT_MATCH,
highlight(ports_fts, 3, '<b>', '</b>') AS DESCR_MATCH
FROM ports_fts
WHERE ports_fts MATCH ? ORDER BY rank;
};
my $title = "OpenBSD.app";
my $descr = "OpenBSD package search";
2022-09-23 16:45:55 -06:00
get '/' => sub ($c) {
my $v = $c->validation;
2022-09-23 16:45:55 -06:00
my $search = $c->param('search');
2022-09-23 20:25:02 -06:00
my $current = $c->param('current');
my $format = $c->param('format');
2022-09-23 16:45:55 -06:00
$c->stash( title => $title );
$c->stash( descr => $descr );
2022-09-23 20:25:02 -06:00
if ( defined $search && $search ne "" ) {
2022-09-23 20:25:02 -06:00
my $db = $c->stable->db;
$db = $c->current->db if defined $current;
2022-09-23 16:45:55 -06:00
2022-09-23 20:25:02 -06:00
my $results = $db->query( $query, $search )->hashes;
given ($format) {
when ("json") {
$c->render( json => $results );
}
default {
$c->render(
template => 'results',
search => $search,
results => $results
);
}
}
2022-09-23 16:45:55 -06:00
}
else {
$c->render( template => 'index' );
}
};
get '/openbsd-app-opensearch.xml' => sub ($c) {
$c->res->headers->content_type('application/opensearchdescription+xml');
$c->render(
template => 'openbsd-app-opensearch',
format => 'xml',
title => $title,
descr => $descr
);
};
2022-09-23 16:45:55 -06:00
app->start;
__DATA__
@@ layouts/default.html.ep
<!doctype html>
<html class="no-js" lang="">
<head>
<title><%= $title %></title>
2022-09-23 16:45:55 -06:00
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="<%= $descr %>">
<link
rel="search"
type="application/opensearchdescription+xml"
href="/openbsd-app-opensearch.xml" />
2022-09-23 16:45:55 -06:00
<style>
body {
font-family: Avenir, 'Open Sans', sans-serif;
2022-09-23 21:45:29 -06:00
background-color: #ffffea;
2022-09-23 16:45:55 -06:00
}
2022-09-23 20:25:02 -06:00
table {
border-collapse:separate;
border:solid black 1px;
border-radius:6px;
2022-09-23 21:45:29 -06:00
background-color: #fff;
2022-09-23 16:45:55 -06:00
}
2022-09-23 20:25:02 -06:00
td, th {
border-left:solid black 1px;
border-top:solid black 1px;
}
2022-09-23 21:45:29 -06:00
2022-09-24 06:51:01 -06:00
th {
white-space: nowrap;
2022-09-24 06:51:55 -06:00
padding: 6px;
2022-09-24 06:51:01 -06:00
}
2022-09-23 21:45:29 -06:00
.search {
padding: 10px;
margin: 10px;
border-radius:6px;
box-shadow: 2px 2px 2px black;
}
2022-09-23 20:25:02 -06:00
2022-09-23 21:45:29 -06:00
th, .search {
2022-09-23 20:25:02 -06:00
border-top: none;
2022-09-23 21:45:29 -06:00
background-color: #eaeaff;
2022-09-23 20:25:02 -06:00
}
td:first-child, th:first-child {
border-left: none;
}
td {
padding: 10px;
2022-09-23 21:45:29 -06:00
text-align: left;
2022-09-23 16:45:55 -06:00
}
2022-09-23 20:25:02 -06:00
.nowrap {
white-space: nowrap;
2022-09-23 16:45:55 -06:00
}
2022-09-23 21:45:29 -06:00
footer, .wrap, .results {
text-align: center;
2022-09-23 16:45:55 -06:00
}
</style>
</head>
<body>
<div class="wrap">
<h3><a href="/">OpenBSD.app - search packages</a></h3>
2022-09-23 16:45:55 -06:00
<div class="search">
%= form_for '/' => begin
%= text_field search => ""
2022-09-24 08:24:38 -06:00
-current
%= check_box 'current'
2022-09-23 16:45:55 -06:00
%= submit_button 'Search...'
% end
</div>
</div>
2022-09-23 21:45:29 -06:00
<div class="results">
<%== content %>
</div>
2022-09-23 16:45:55 -06:00
<hr />
<footer>
<p><a href="https://github.com/qbit/openbsd.app">OpenBSD.app</a> © 2022</p>
<p><a href="https://github.com/qbit/pkg">Prefer CLI?</a></p>
2022-09-23 16:45:55 -06:00
</footer>
</body>
</html>
@@ results.html.ep
% layout 'default';
<p>
Found <b><%= @$results %></b> results for '<b><%= $search %></b>'<br />
<a href="/?search=<%= $search %>&format=json">View as JSON</a>
</p>
2022-09-23 21:45:29 -06:00
<table class="results">
2022-09-23 16:45:55 -06:00
<thead>
<tr>
2022-09-23 21:07:13 -06:00
<th>Package Name</th>
2022-09-23 21:45:29 -06:00
<th>Path</th>
2022-09-23 16:45:55 -06:00
<th>Comment</th>
<th>Description</th>
</tr>
</thead>
% foreach my $result (@$results) {
<tr>
2022-09-23 21:07:13 -06:00
<td class="nowrap"><%= $result->{FULLPKGNAME} %></td>
2022-09-23 21:45:29 -06:00
<td class="nowrap"><%= $result->{FULLPKGPATH} %></td>
2022-09-23 20:25:02 -06:00
<td class="nowrap"><%== $result->{COMMENT_MATCH} %></td>
2022-09-23 16:45:55 -06:00
<td><%== $result->{DESCR_MATCH} %></td>
</tr>
% }
</table>
@@ index.html.ep
% layout 'default';
Welcome! Default search queries OpenBSD 7.2 package sets. You can search -current (packages from 2022-09-23) by toggling the '-current' checkbox.
@@ openbsd-app-opensearch.xml.ep
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName><%= $title %></ShortName>
<Description><%= $descr %></Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="32" height="32" type="image/x-icon">https://openbsd.org/favicon.ico</Image>
<Url type="text/html" template="https://openbsd.app/?search={searchTerms}"/>
<moz:SearchForm>https://openbsd.app/</moz:SearchForm>
</OpenSearchDescription>