From 5950685bdfdc294838fa95aa0b07e2bc7e6daf8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Tue, 18 Oct 2022 18:10:17 +0200 Subject: [PATCH] chore: put config into its own struct --- src/config.rs | 7 +++++++ src/main.rs | 36 ++++++++++++++++++++---------------- src/routes.rs | 24 +++++++++--------------- 3 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 src/config.rs diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..6f3e764 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,7 @@ +#[derive(Clone, Debug)] +pub struct UpEndConfig { + pub vault_name: Option, + pub desktop_enabled: bool, + pub secret: String, + pub key: Option, +} diff --git a/src/main.rs b/src/main.rs index 09113b4..0489163 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ use tracing_subscriber::filter::{EnvFilter, LevelFilter}; use crate::{ common::{get_static_dir, PKG_VERSION}, + config::UpEndConfig, database::{ stores::{fs::FsStore, UpStore}, UpEndDatabase, @@ -28,6 +29,7 @@ use crate::{ mod addressing; mod common; +mod config; mod database; mod extractors; mod previews; @@ -197,26 +199,28 @@ fn main() -> Result<()> { let state = routes::State { upend: upend.clone(), - vault_name: Some( - matches - .value_of("VAULT_NAME") - .map(|s| s.to_string()) - .unwrap_or_else(|| { - vault_path - .iter() - .last() - .unwrap() - .to_string_lossy() - .into_owned() - }), - ), store, job_container: job_container.clone(), preview_store, preview_pool, - desktop_enabled, - secret, - key, + config: UpEndConfig { + vault_name: Some( + matches + .value_of("VAULT_NAME") + .map(|s| s.to_string()) + .unwrap_or_else(|| { + vault_path + .iter() + .last() + .unwrap() + .to_string_lossy() + .into_owned() + }), + ), + desktop_enabled, + secret, + key, + }, }; // Start HTTP server diff --git a/src/routes.rs b/src/routes.rs index 6f3b346..ccd2aae 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,4 +1,5 @@ use crate::addressing::{Address, Addressable}; +use crate::config::UpEndConfig; use crate::database::constants::{ADDED_ATTR, LABEL_ATTR}; use crate::database::entry::{Entry, EntryValue, InvariantEntry}; use crate::database::hierarchies::{list_roots, resolve_path, UHierPath}; @@ -42,13 +43,10 @@ use is_executable::IsExecutable; pub struct State { pub upend: Arc, pub store: Arc>, - pub vault_name: Option, + pub config: UpEndConfig, pub job_container: JobContainer, pub preview_store: Option>, pub preview_pool: Option>, - pub desktop_enabled: bool, - pub secret: String, - pub key: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -66,7 +64,7 @@ pub async fn login( state: web::Data, payload: web::Json, ) -> Result { - if state.key.is_none() || Some(&payload.key) == state.key.as_ref() { + if state.config.key.is_none() || Some(&payload.key) == state.config.key.as_ref() { let claims = JwtClaims { exp: (SystemTime::now() .duration_since(UNIX_EPOCH) @@ -78,7 +76,7 @@ pub async fn login( let token = jsonwebtoken::encode( &jsonwebtoken::Header::default(), &claims, - &jsonwebtoken::EncodingKey::from_secret(state.secret.as_ref()), + &jsonwebtoken::EncodingKey::from_secret(state.config.secret.as_ref()), ) .map_err(ErrorInternalServerError)?; @@ -89,7 +87,7 @@ pub async fn login( } fn check_auth(req: &HttpRequest, state: &State) -> Result<(), actix_web::Error> { - if let Some(key) = &state.key { + if let Some(key) = &state.config.key { if let Some(auth_header) = req.headers().get("Authorization") { let auth_header = auth_header.to_str().map_err(|err| { ErrorBadRequest(format!("Invalid value in Authorization header: {err:?}")) @@ -157,7 +155,7 @@ pub async fn get_raw( ]), ), )); - } else if state.desktop_enabled { + } else if state.config.desktop_enabled { #[cfg(feature = "desktop")] { info!("Opening {:?}...", file_path); @@ -223,11 +221,7 @@ pub async fn get_thumbnail( let (tx, rx) = oneshot::channel(); let _job_container = state.job_container.clone(); state.preview_pool.as_ref().unwrap().spawn(move || { - let result = preview_store.get( - address_hash, - query, - _job_container, - ); + let result = preview_store.get(address_hash, query, _job_container); tx.send(result).unwrap(); }); @@ -757,10 +751,10 @@ pub async fn get_jobs( #[get("/api/info")] pub async fn get_info(state: web::Data) -> Result { Ok(HttpResponse::Ok().json(json!({ - "name": state.vault_name, + "name": state.config.vault_name, // "location": &*state.store.path, "version": crate::common::PKG_VERSION, - "desktop": state.desktop_enabled + "desktop": state.config.desktop_enabled }))) }