chore: put config into its own struct

feat/type-attributes
Tomáš Mládek 2022-10-18 18:10:17 +02:00
parent 2c707c9abb
commit 5950685bdf
3 changed files with 36 additions and 31 deletions

7
src/config.rs Normal file
View File

@ -0,0 +1,7 @@
#[derive(Clone, Debug)]
pub struct UpEndConfig {
pub vault_name: Option<String>,
pub desktop_enabled: bool,
pub secret: String,
pub key: Option<String>,
}

View File

@ -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

View File

@ -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<UpEndDatabase>,
pub store: Arc<Box<dyn UpStore + Sync + Send>>,
pub vault_name: Option<String>,
pub config: UpEndConfig,
pub job_container: JobContainer,
pub preview_store: Option<Arc<PreviewStore>>,
pub preview_pool: Option<Arc<rayon::ThreadPool>>,
pub desktop_enabled: bool,
pub secret: String,
pub key: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -66,7 +64,7 @@ pub async fn login(
state: web::Data<State>,
payload: web::Json<LoginRequest>,
) -> Result<HttpResponse, Error> {
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<State>) -> Result<HttpResponse, Error> {
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
})))
}