diff --git a/src/main.rs b/src/main.rs index 0dc1e9e..e3c1252 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,8 @@ use structopt::StructOpt; use std::{ - path::PathBuf, boxed::Box, error::Error, }; -use sqlx::{ - prelude::*, - sqlite::{ - SqliteConnection, - SqliteConnectOptions, - SqliteQueryResult, - }, -}; use tokio::{ sync::RwLock, fs::File, @@ -24,6 +15,8 @@ use crate::{ mod cli; mod config; +#[cfg(feature = "sqlite")] +mod sqlite; const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite"; @@ -47,35 +40,14 @@ async fn main() -> Result<(), Box> { println!("{:?}", config); #[cfg(feature = "sqlite")] - let mut sqlite_connection = RwLock::new(connect_sqlite(&config.database_path).await?); + let mut sqlite_connection = RwLock::new(sqlite::connect(&config.database_path).await?); #[cfg(feature = "sqlite")] - load_sqlite_template(sqlite_connection.get_mut()).await?; + sqlite::load_template(sqlite_connection.get_mut()).await?; Ok(()) } -/// Prepare sqlite database connection -#[cfg(feature = "sqlite")] -async fn connect_sqlite(path: &PathBuf) -> sqlx::Result { - let connect_options = SqliteConnectOptions::new() - .filename(&path) - .create_if_missing(true); - SqliteConnection::connect_with(&connect_options).await -} - -#[cfg(feature = "sqlite")] -async fn load_sqlite_template(sqlite_handle: &mut SqliteConnection) -> sqlx::Result { - sqlx::query(concat!( - include_str!("sqlite/users.sql"), - include_str!("sqlite/security_preferences.sql"), - include_str!("sqlite/privacy_preferences.sql"), - include_str!("sqlite/notification_preferences.sql"), - include_str!("sqlite/external_servers_privacy_preferences.sql"), - include_str!("sqlite/external_servers_preferences.sql"), - )).execute(sqlite_handle).await -} - async fn get_config(options: &Options) -> std::io::Result { if options.no_config { Ok(Config::default()) diff --git a/src/sqlite/mod.rs b/src/sqlite/mod.rs new file mode 100644 index 0000000..06e2738 --- /dev/null +++ b/src/sqlite/mod.rs @@ -0,0 +1,29 @@ +use std::path::PathBuf; +use sqlx::{ + prelude::*, + sqlite::{ + SqliteConnection, + SqliteConnectOptions, + SqliteQueryResult, + }, +}; + +/// Prepare sqlite database connection +pub async fn connect(path: &PathBuf) -> sqlx::Result { + let connect_options = SqliteConnectOptions::new() + .filename(&path) + .create_if_missing(true); + SqliteConnection::connect_with(&connect_options).await +} + +/// Create tables from template +pub async fn load_template(sqlite_handle: &mut SqliteConnection) -> sqlx::Result { + sqlx::query(concat!( + include_str!("users.sql"), + include_str!("security_preferences.sql"), + include_str!("privacy_preferences.sql"), + include_str!("notification_preferences.sql"), + include_str!("external_servers_privacy_preferences.sql"), + include_str!("external_servers_preferences.sql"), + )).execute(sqlite_handle).await +} \ No newline at end of file