AkurAI Build
Menu

Public repository / dd5ceb09c5112682f7d0ab013d27ed6ac8d4d355

AkurAI-Monitor / src/db.rs

Latest change c39a9a924feb8021a7355a7299f92502eb8d3e76 - Initial commit: rust-monitor v0.1.0 by Olafur Bui

use crate::config;
use crate::schema;
use rusqlite::Connection;
use std::sync::{LazyLock, Mutex};

static DB: LazyLock<Mutex<Connection>> = LazyLock::new(|| {
    let cfg = config::get();
    if let Some(parent) = std::path::Path::new(&cfg.db_path).parent() {
        std::fs::create_dir_all(parent).ok();
    }
    let conn = Connection::open(&cfg.db_path).expect("failed to open SQLite database");
    conn.execute_batch("PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON;")
        .expect("failed to set pragmas");
    schema::create_tables(&conn);
    schema::seed_alert_rules(&conn);
    Mutex::new(conn)
});

pub fn with_db<F, R>(f: F) -> R
where
    F: FnOnce(&Connection) -> R,
{
    let conn = DB.lock().unwrap();
    f(&conn)
}