Add OpenSearch XML

This commit is contained in:
Olivier 'reivilibre' 2022-11-28 22:49:18 +00:00
parent 8b439c1550
commit 34a05f84ff
5 changed files with 37 additions and 0 deletions

View File

@ -63,6 +63,8 @@
], ],
sqlite_db_path: "data/dev_qp_web.sqlite3", sqlite_db_path: "data/dev_qp_web.sqlite3",
public_base: "http://127.0.0.1:9001",
), ),
// Index (indexer, web) // Index (indexer, web)

View File

@ -7,6 +7,7 @@ use env_logger::Env;
use log::info; use log::info;
use quickpeep::config::WebConfig; use quickpeep::config::WebConfig;
use quickpeep::web::icon_retrieval::retrieve_icon; use quickpeep::web::icon_retrieval::retrieve_icon;
use quickpeep::web::metadata::get_opensearch_xml;
use quickpeep::web::searcher::{search_root, search_search}; use quickpeep::web::searcher::{search_root, search_search};
use quickpeep::web::seed_collector::{seed_collection_root, seed_collection_root_post}; use quickpeep::web::seed_collector::{seed_collection_root, seed_collection_root_post};
use quickpeep::web::IndexAccess; use quickpeep::web::IndexAccess;
@ -76,6 +77,7 @@ async fn main() -> anyhow::Result<()> {
.route("/", get(search_root)) .route("/", get(search_root))
.route("/search", get(search_search)) .route("/search", get(search_search))
.route("/icon.webp", get(retrieve_icon)) .route("/icon.webp", get(retrieve_icon))
.route("/opensearch.xml", get(get_opensearch_xml))
.layer(Extension(web_config)) .layer(Extension(web_config))
.layer(Extension(pool)) .layer(Extension(pool))
.layer(Extension(index_access)) .layer(Extension(index_access))

View File

@ -31,6 +31,10 @@ pub struct WebOnlyConfig {
pub sqlite_db_path: PathBuf, pub sqlite_db_path: PathBuf,
/// Name, URL pairs /// Name, URL pairs
pub contact: Vec<(String, String)>, pub contact: Vec<(String, String)>,
/// URL prefix for QuickPeep. Should include protocol. No trailing slash.
/// Example: https://quickpeep.net
pub public_base: String,
} }
impl WebConfig { impl WebConfig {

View File

@ -2,6 +2,7 @@ use quickpeep_index::backend::Backend;
use std::sync::Arc; use std::sync::Arc;
pub mod icon_retrieval; pub mod icon_retrieval;
pub mod metadata;
pub mod searcher; pub mod searcher;
pub mod seed_collector; pub mod seed_collector;

View File

@ -0,0 +1,28 @@
use crate::config::WebConfig;
use axum::extract::Extension;
use axum::response::{IntoResponse, Response};
pub async fn get_opensearch_xml(Extension(web_config): Extension<WebConfig>) -> impl IntoResponse {
let public_base = &web_config.web.public_base;
let formatted = format!(
r#"
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>QuickPeep</ShortName>
<Description>small-scale web search engine</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16" type="image/x-icon">{public_base}/favicon.ico</Image>
<Url type="text/html" template="{public_base}/search?q=%s"/>
</OpenSearchDescription>
"#
);
// Extras for the future:
// <Url type="application/x-suggestions+json" template="[suggestionURL]"/>
// <moz:SearchForm>[https://example.com/search]</moz:SearchForm>
Response::builder()
.header("content-type", "application/opensearchdescription+xml")
.body(formatted.into_response())
.unwrap()
}