diff options
Diffstat (limited to 'server/src/main.rs')
| -rw-r--r-- | server/src/main.rs | 79 |
1 files changed, 62 insertions, 17 deletions
diff --git a/server/src/main.rs b/server/src/main.rs index 9b6463f..bd9901a 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -7,38 +7,83 @@ #![allow(clippy::needless_borrows_for_generic_args)] #![recursion_limit = "4096"] -use crate::logger::setup_logger; -use config::load_config; -use log::{error, info, warn}; +use crate::{auth::token::SessionKey, logger::setup_logger}; +use anyhow::Result; +use jellycache::Cache; +use jellydb::table::Table; +use jellykv::Database; +use log::{error, info}; use routes::build_rocket; -use std::process::exit; +use serde::Deserialize; +use std::{env::args, fs::read_to_string, path::PathBuf, process::exit, sync::Arc}; pub mod api; +pub mod auth; pub mod compat; -pub mod config; pub mod logger; pub mod logic; pub mod request_info; pub mod responders; pub mod routes; pub mod ui; -pub mod auth; #[rocket::main] async fn main() { setup_logger(); - info!("loading config..."); - if let Err(e) = load_config().await { - error!("error {e:?}"); - exit(1); - } - - #[cfg(feature = "bypass-auth")] - logger::warn!("authentification bypass enabled"); - let r = build_rocket().launch().await; + let state = match create_state() { + Ok(s) => s, + Err(e) => { + error!("unable to start: {e:#}"); + exit(1); + } + }; + let r = build_rocket(state).launch().await; match r { - Ok(_) => warn!("server shutdown"), - Err(e) => error!("server exited: {e}"), + Ok(_) => info!("server shutdown"), + Err(e) => { + error!("server exited: {e}"); + exit(1); + } } } + +pub(crate) struct State { + pub config: Config, + + pub cache: Cache, + pub database: Arc<dyn Database>, + pub session_key: SessionKey, + + pub nodes: Table, + pub users: Table, +} + +#[derive(Debug, Deserialize)] +pub struct Config { + pub ui: jellyui::Config, + pub session_key: String, + pub asset_path: PathBuf, + pub database_path: PathBuf, + pub cache_path: PathBuf, + pub max_memory_cache_size: usize, + pub tls: bool, + pub hostname: String, +} + +pub fn create_state() -> Result<Arc<State>> { + let config_path = args().nth(1).unwrap(); + let config: Config = serde_yaml_ng::from_str(&read_to_string(config_path)?)?; + + let cache_storage = jellykv::rocksdb::new(&config.cache_path)?; + let db_storage = jellykv::rocksdb::new(&config.database_path)?; + + Ok(Arc::new(State { + cache: Cache::new(Box::new(cache_storage), config.max_memory_cache_size), + database: Arc::new(db_storage), + session_key: SessionKey::parse(&config.session_key)?, + nodes: Table::new(0), + users: Table::new(1), + config, + })) +} |