diff options
author | metamuffin <metamuffin@disroot.org> | 2023-10-25 12:21:27 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-10-25 12:21:27 +0200 |
commit | a491792196c034efbd2f8998944af3f7958c0e52 (patch) | |
tree | a295342aef8cd38e32b05af273ff305b7f2a5cc5 /base | |
parent | 5aa2a6fa5a6f8daf3ed4d86082658027a44f83c8 (diff) | |
parent | 8fc2d47f1f6cde93554ba096b959b3bef3652ac1 (diff) | |
download | jellything-a491792196c034efbd2f8998944af3f7958c0e52.tar jellything-a491792196c034efbd2f8998944af3f7958c0e52.tar.bz2 jellything-a491792196c034efbd2f8998944af3f7958c0e52.tar.zst |
Merge branch 'master' of codeberg.org:metamuffin/jellything
Diffstat (limited to 'base')
-rw-r--r-- | base/Cargo.toml | 2 | ||||
-rw-r--r-- | base/src/database.rs | 37 | ||||
-rw-r--r-- | base/src/lib.rs | 11 |
3 files changed, 45 insertions, 5 deletions
diff --git a/base/Cargo.toml b/base/Cargo.toml index 45c3843..0dede64 100644 --- a/base/Cargo.toml +++ b/base/Cargo.toml @@ -13,3 +13,5 @@ base64 = "0.21.4" tokio = { workspace = true } anyhow = "1.0.75" bincode = "2.0.0-rc.3" +sled = "0.34.7" +typed-sled = "0.2.3" diff --git a/base/src/database.rs b/base/src/database.rs new file mode 100644 index 0000000..739d292 --- /dev/null +++ b/base/src/database.rs @@ -0,0 +1,37 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2023 metamuffin <metamuffin.org> +*/ +use anyhow::Context; +use jellycommon::{user::User, Node}; +use log::info; +use std::path::Path; +use typed_sled::Tree; + +pub use sled; +pub use typed_sled; + +pub struct Database { + pub db: sled::Db, + + pub user: Tree<String, User>, + pub invite: Tree<String, ()>, + pub node: Tree<String, Node>, +} + +impl Database { + pub fn open(path: &Path) -> Result<Self, anyhow::Error> { + info!("opening database… (might take up to O(n) time)"); + let db = sled::open(path).context("opening database")?; + info!("creating trees"); + let r = Ok(Self { + user: Tree::open(&db, "user"), + invite: Tree::open(&db, "invite"), + node: Tree::open(&db, "node"), + db, + }); + info!("ready"); + r + } +} diff --git a/base/src/lib.rs b/base/src/lib.rs index 132fd45..cfc5a11 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -5,6 +5,7 @@ */ #![feature(lazy_cell)] pub mod cache; +pub mod database; pub mod permission; pub mod temp; @@ -13,11 +14,11 @@ use std::{fs::File, path::PathBuf, sync::LazyLock}; pub static CONF: LazyLock<GlobalConfig> = LazyLock::new(|| { serde_yaml::from_reader( - File::open( - std::env::args() - .nth(1) - .expect("First argument must specify the configuration to use."), - ) + File::open(std::env::var("JELLYTHING_CONFIG").unwrap_or_else(|_| { + std::env::args().nth(1).expect( + "First argument or JELLYTHING_CONFIG must specify the configuration to use.", + ) + })) .unwrap(), ) .unwrap() |