#[macro_use] extern crate rocket; use std::{error::Error, process::Command, str::FromStr}; use rocket::{response::Debug, serde::json::Json, State}; use rocket_cors::{AllowedHeaders, AllowedOrigins}; use notmuch::{MessageId, Notmuch, NotmuchError, SearchSummary, ThreadSet}; #[get("/")] fn hello() -> &'static str { "Hello, world!" } #[get("/search/")] async fn search( nm: &State, query: &str, ) -> Result, Debug> { let res = nm.search(query)?; Ok(Json(res)) } #[get("/show/")] async fn show(nm: &State, query: &str) -> Result, Debug> { let res = nm.show(query)?; Ok(Json(res)) } #[get("/original/")] async fn original(nm: &State, id: &str) -> Result, Debug> { let mid = if id.starts_with("id:") { id.to_string() } else { format!("id:{}", id) }; let res = nm.show_original(&mid)?; Ok(res) } #[rocket::main] async fn main() -> Result<(), Box> { let allowed_origins = AllowedOrigins::all(); let cors = rocket_cors::CorsOptions { allowed_origins, allowed_methods: vec!["Get"] .into_iter() .map(|s| FromStr::from_str(s).unwrap()) .collect(), allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]), allow_credentials: true, ..Default::default() } .to_cors()?; rocket::build() .mount("/", routes![original, hello, search, show]) .attach(cors) .manage(Notmuch::default()) //.manage(Notmuch::with_config("../notmuch/testdata/notmuch.config")) .launch() .await?; Ok(()) }