Use notmuch crate in server and web.

This commit is contained in:
2021-10-29 20:10:06 -07:00
parent bec6f0f333
commit ad996643c9
6 changed files with 894 additions and 220 deletions

View File

@@ -1,12 +1,12 @@
#[macro_use]
extern crate rocket;
use std::error::Error;
use std::process::{Command, Output};
use std::str::FromStr;
use std::{error::Error, process::Command, str::FromStr};
use rocket::{http::Method, Route};
use rocket_cors::{AllowedHeaders, AllowedOrigins, Guard, Responder};
use rocket::{response::Debug, serde::json::Json, State};
use rocket_cors::{AllowedHeaders, AllowedOrigins};
use notmuch::{Notmuch, NotmuchError, SearchSummary, ThreadSet};
#[get("/")]
fn hello() -> &'static str {
@@ -14,29 +14,23 @@ fn hello() -> &'static str {
}
#[get("/search/<query>")]
async fn search(query: &str) -> std::io::Result<Vec<u8>> {
//format!("Search for '{}'", query)
let mut cmd = Command::new("notmuch");
let cmd = cmd.args(["search", "--format=json", "--limit=20", query]);
dbg!(&cmd);
let out = cmd.output()?;
Ok(out.stdout)
async fn search(
nm: &State<Notmuch>,
query: &str,
) -> Result<Json<SearchSummary>, Debug<NotmuchError>> {
let res = nm.search(query)?;
Ok(Json(res))
}
#[get("/show/<query>")]
async fn show(query: &str) -> std::io::Result<Vec<u8>> {
//format!("Search for '{}'", query)
let mut cmd = Command::new("notmuch");
let cmd = cmd.args(["show", "--format=json", "--body=true", query]);
dbg!(&cmd);
let out = cmd.output()?;
Ok(out.stdout)
async fn show(nm: &State<Notmuch>, query: &str) -> Result<Json<ThreadSet>, Debug<NotmuchError>> {
let res = nm.show(query)?;
Ok(Json(res))
}
#[rocket::main]
async fn main() -> Result<(), Box<dyn Error>> {
let allowed_origins = AllowedOrigins::all();
// You can also deserialize this
let cors = rocket_cors::CorsOptions {
allowed_origins,
allowed_methods: vec!["Get"]
@@ -52,6 +46,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
rocket::build()
.mount("/", routes![hello, search, show])
.attach(cors)
.manage(Notmuch::with_config("../notmuch/testdata/notmuch.config"))
.launch()
.await?;