Less DB coupling with VFS and DDNS
This commit is contained in:
parent
bdc33a29aa
commit
8a7a63ce61
3 changed files with 36 additions and 29 deletions
|
@ -7,11 +7,9 @@ use std::path::{Path, PathBuf};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use config::{MiscSettings, UserConfig};
|
use config::{MiscSettings, UserConfig};
|
||||||
use ddns::{DDNSConfigSource, DDNSConfig};
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use index;
|
use index;
|
||||||
use user::*;
|
use user::*;
|
||||||
use vfs::{MountPoint, VFS, VFSSource};
|
|
||||||
|
|
||||||
mod schema;
|
mod schema;
|
||||||
|
|
||||||
|
@ -126,33 +124,6 @@ impl ConnectionSource for DB {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DDNSConfigSource for DB {
|
|
||||||
fn get_ddns_config(&self) -> Result<DDNSConfig> {
|
|
||||||
use self::ddns_config::dsl::*;
|
|
||||||
let connection = self.connection.lock().unwrap();
|
|
||||||
let connection = connection.deref();
|
|
||||||
Ok(ddns_config
|
|
||||||
.select((host, username, password))
|
|
||||||
.get_result(connection)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl VFSSource for DB {
|
|
||||||
fn get_vfs(&self) -> Result<VFS> {
|
|
||||||
use self::mount_points::dsl::*;
|
|
||||||
let mut vfs = VFS::new();
|
|
||||||
let connection = self.connection.lock().unwrap();
|
|
||||||
let connection = connection.deref();
|
|
||||||
let points: Vec<MountPoint> = mount_points
|
|
||||||
.select((source, name))
|
|
||||||
.get_results(connection)?;
|
|
||||||
for point in points {
|
|
||||||
vfs.mount(&Path::new(&point.source), &point.name)?;
|
|
||||||
}
|
|
||||||
Ok(vfs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn _get_test_db(name: &str) -> DB {
|
fn _get_test_db(name: &str) -> DB {
|
||||||
let config_path = Path::new("test/config.toml");
|
let config_path = Path::new("test/config.toml");
|
||||||
let config = UserConfig::parse(&config_path).unwrap();
|
let config = UserConfig::parse(&config_path).unwrap();
|
||||||
|
|
16
src/ddns.rs
16
src/ddns.rs
|
@ -1,9 +1,13 @@
|
||||||
|
use core::ops::Deref;
|
||||||
|
use diesel::prelude::*;
|
||||||
use reqwest;
|
use reqwest;
|
||||||
use reqwest::header::{Authorization, Basic};
|
use reqwest::header::{Authorization, Basic};
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
|
use db::{ConnectionSource, DB};
|
||||||
|
use db::ddns_config;
|
||||||
use errors;
|
use errors;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Queryable)]
|
#[derive(Debug, Deserialize, Queryable)]
|
||||||
|
@ -17,6 +21,18 @@ pub trait DDNSConfigSource {
|
||||||
fn get_ddns_config(&self) -> errors::Result<DDNSConfig>;
|
fn get_ddns_config(&self) -> errors::Result<DDNSConfig>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DDNSConfigSource for DB {
|
||||||
|
fn get_ddns_config(&self) -> errors::Result<DDNSConfig> {
|
||||||
|
use self::ddns_config::dsl::*;
|
||||||
|
let connection = self.get_connection();
|
||||||
|
let connection = connection.lock().unwrap();
|
||||||
|
let connection = connection.deref();
|
||||||
|
Ok(ddns_config
|
||||||
|
.select((host, username, password))
|
||||||
|
.get_result(connection)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum DDNSError {
|
enum DDNSError {
|
||||||
InternalError(errors::Error),
|
InternalError(errors::Error),
|
||||||
|
|
20
src/vfs.rs
20
src/vfs.rs
|
@ -1,7 +1,10 @@
|
||||||
|
use core::ops::Deref;
|
||||||
|
use diesel::prelude::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
use db::{ConnectionSource, DB};
|
||||||
use db::mount_points;
|
use db::mount_points;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
|
@ -9,6 +12,23 @@ pub trait VFSSource {
|
||||||
fn get_vfs(&self) -> Result<VFS>;
|
fn get_vfs(&self) -> Result<VFS>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl VFSSource for DB {
|
||||||
|
fn get_vfs(&self) -> Result<VFS> {
|
||||||
|
use self::mount_points::dsl::*;
|
||||||
|
let mut vfs = VFS::new();
|
||||||
|
let connection = self.get_connection();
|
||||||
|
let connection = connection.lock().unwrap();
|
||||||
|
let connection = connection.deref();
|
||||||
|
let points: Vec<MountPoint> = mount_points
|
||||||
|
.select((source, name))
|
||||||
|
.get_results(connection)?;
|
||||||
|
for point in points {
|
||||||
|
vfs.mount(&Path::new(&point.source), &point.name)?;
|
||||||
|
}
|
||||||
|
Ok(vfs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Insertable, Queryable)]
|
#[derive(Debug, Deserialize, Insertable, Queryable)]
|
||||||
#[table_name="mount_points"]
|
#[table_name="mount_points"]
|
||||||
pub struct MountPoint {
|
pub struct MountPoint {
|
||||||
|
|
Loading…
Add table
Reference in a new issue