aboutsummaryrefslogtreecommitdiff
path: root/logic/src/lib.rs
blob: 444293af62ab70f95ab52d4ebf8fc5ae42716533 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
    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) 2025 metamuffin <metamuffin.org>
*/
#![feature(duration_constructors)]

pub mod account;
pub mod admin;
pub mod assets;
pub mod filter_sort;
pub mod home;
pub mod items;
pub mod login;
pub mod node;
pub mod permission;
pub mod search;
pub mod session;
pub mod stats;

use anyhow::Context;
use anyhow::Result;
use jellydb::Database;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::sync::LazyLock;
use std::sync::Mutex;

#[rustfmt::skip]
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Config {
  login_expire: i64,
  session_key: Option<String>,
  admin_username:Option<String>,
  admin_password:Option<String>,
  database_path: PathBuf,
}

pub static CONF_PRELOAD: Mutex<Option<Config>> = Mutex::new(None);
static CONF: LazyLock<Config> = LazyLock::new(|| {
    CONF_PRELOAD
        .lock()
        .unwrap()
        .take()
        .expect("logic config not preloaded. logic error")
});

static DATABASE_PRELOAD: Mutex<Option<Database>> = Mutex::new(None);
static DATABASE: LazyLock<Database> = LazyLock::new(|| {
    DATABASE_PRELOAD
        .lock()
        .unwrap()
        .take()
        .expect("database not preloaded. logic error")
});

pub fn init_database() -> Result<()> {
    let database = Database::open(&CONF.database_path)
        .context("opening database")
        .unwrap();
    *DATABASE_PRELOAD.lock().unwrap() = Some(database);
    Ok(())
}