letterbox/server/src/main.rs

71 lines
1.7 KiB
Rust

#[macro_use]
extern crate rocket;
use std::{error::Error, process::Command, str::FromStr};
use rocket::{
response::{content::Plain, 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/<query>")]
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(nm: &State<Notmuch>, query: &str) -> Result<Json<ThreadSet>, Debug<NotmuchError>> {
let res = nm.show(query)?;
Ok(Json(res))
}
#[get("/original/<id>")]
async fn original(nm: &State<Notmuch>, id: &str) -> Result<Plain<Vec<u8>>, Debug<NotmuchError>> {
let mid = if id.starts_with("id:") {
id.to_string()
} else {
format!("id:{}", id)
};
let res = nm.show_original(&mid)?;
Ok(Plain(res))
}
#[rocket::main]
async fn main() -> Result<(), Box<dyn Error>> {
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(())
}