Use notmuch crate in server and web.
This commit is contained in:
@@ -6,6 +6,15 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rocket = "0.5.0-rc.1"
|
||||
rocket = { version = "0.5.0-rc.1", features = [ "json" ] }
|
||||
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", branch = "master" }
|
||||
notmuch = { path = "../notmuch" }
|
||||
serde_json = "1.0.64"
|
||||
thiserror = "1.0.26"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
log = "0.4.14"
|
||||
|
||||
[dependencies.rocket_contrib]
|
||||
version = "0.4.10"
|
||||
default-features = false
|
||||
features = ["json"]
|
||||
|
||||
@@ -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?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user