WIP: Simple frontend and backend to search notmuch mail.

This commit is contained in:
2021-10-24 10:21:25 -07:00
parent 9085d7f0d2
commit 8f2e14049c
11 changed files with 1377 additions and 145 deletions

11
server/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "server"
version = "0.1.0"
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_cors = { git = "https://github.com/lawliet89/rocket_cors", branch = "master" }

9
server/Rocket.toml Normal file
View File

@@ -0,0 +1,9 @@
[release]
address = "0.0.0.0"
port = 9345
[debug]
address = "0.0.0.0"
port = 9345
# Uncomment to make it production like.
#log_level = "critical"

59
server/src/main.rs Normal file
View File

@@ -0,0 +1,59 @@
#[macro_use]
extern crate rocket;
use std::error::Error;
use std::process::{Command, Output};
use std::str::FromStr;
use rocket::{http::Method, Route};
use rocket_cors::{AllowedHeaders, AllowedOrigins, Guard, Responder};
#[get("/")]
fn hello() -> &'static str {
"Hello, world!"
}
#[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)
}
#[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)
}
#[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"]
.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![hello, search, show])
.attach(cors)
.launch()
.await?;
Ok(())
}