Moved backend code into its own subfolder

This commit is contained in:
rzuasti
2026-02-10 14:03:33 -05:00
parent a86efc015a
commit 39a58983d6
17 changed files with 3 additions and 2 deletions
+50
View File
@@ -0,0 +1,50 @@
pub mod devices;
use include_dir::{Dir, include_dir};
use log::{debug, error};
use rusqlite::Connection;
use rusqlite_migration::Migrations;
use std::result::Result;
use std::sync::LazyLock;
use crate::settings;
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/database_migrations");
// Define migrations. These are applied atomically.
static MIGRATIONS: LazyLock<Migrations<'static>> =
LazyLock::new(|| Migrations::from_directory(&MIGRATIONS_DIR).unwrap());
pub fn init_db() -> Result<Connection, String> {
if settings::CONFIG.database.path.is_empty() {
error!("Database path not set. Make sure to define database.path in your config file.");
Err(format!(
"Database path not set. Make sure to define database.path in your config file."
))
} else {
debug!("Opening database at {}.", settings::CONFIG.database.path);
let database_path = settings::CONFIG.database.path.clone();
let mut conn = match Connection::open(database_path) {
Ok(value) => value,
Err(error) => {
error!("Error opening database (oott.db): {error}");
return Err(format!("Error opening database (oott.db): {error}"));
}
};
debug!("Database open, executing migrations if needed.");
// Update the database schema, atomically
match MIGRATIONS.to_latest(&mut conn) {
Ok(_) => {
debug!("Database up to date.");
Ok(conn)
}
Err(error) => {
error!("Error updating database: {error}");
Err(format!("Error updating database: {error}"))
}
}
}
}