Move sqlite functions into their own module
This commit is contained in:
parent
7dcd078a2b
commit
e32c0e2a70
36
src/main.rs
36
src/main.rs
@ -1,17 +1,8 @@
|
|||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use std::{
|
use std::{
|
||||||
path::PathBuf,
|
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
error::Error,
|
error::Error,
|
||||||
};
|
};
|
||||||
use sqlx::{
|
|
||||||
prelude::*,
|
|
||||||
sqlite::{
|
|
||||||
SqliteConnection,
|
|
||||||
SqliteConnectOptions,
|
|
||||||
SqliteQueryResult,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
sync::RwLock,
|
sync::RwLock,
|
||||||
fs::File,
|
fs::File,
|
||||||
@ -24,6 +15,8 @@ use crate::{
|
|||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
mod config;
|
mod config;
|
||||||
|
#[cfg(feature = "sqlite")]
|
||||||
|
mod sqlite;
|
||||||
|
|
||||||
const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite";
|
const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite";
|
||||||
|
|
||||||
@ -47,35 +40,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
println!("{:?}", config);
|
println!("{:?}", config);
|
||||||
|
|
||||||
#[cfg(feature = "sqlite")]
|
#[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")]
|
#[cfg(feature = "sqlite")]
|
||||||
load_sqlite_template(sqlite_connection.get_mut()).await?;
|
sqlite::load_template(sqlite_connection.get_mut()).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prepare sqlite database connection
|
|
||||||
#[cfg(feature = "sqlite")]
|
|
||||||
async fn connect_sqlite(path: &PathBuf) -> sqlx::Result<SqliteConnection> {
|
|
||||||
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<SqliteQueryResult> {
|
|
||||||
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<Config> {
|
async fn get_config(options: &Options) -> std::io::Result<Config> {
|
||||||
if options.no_config {
|
if options.no_config {
|
||||||
Ok(Config::default())
|
Ok(Config::default())
|
||||||
|
29
src/sqlite/mod.rs
Normal file
29
src/sqlite/mod.rs
Normal file
@ -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<SqliteConnection> {
|
||||||
|
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<SqliteQueryResult> {
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user