Moved config functionality out of DB
This commit is contained in:
parent
89d746268e
commit
87d43d0da5
3 changed files with 31 additions and 20 deletions
29
src/api.rs
29
src/api.rs
|
@ -1,9 +1,4 @@
|
||||||
use std::fs;
|
use diesel::prelude::*;
|
||||||
use std::io;
|
|
||||||
use std::path::*;
|
|
||||||
use std::ops::Deref;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use iron::prelude::*;
|
use iron::prelude::*;
|
||||||
use iron::headers::{Authorization, Basic};
|
use iron::headers::{Authorization, Basic};
|
||||||
use iron::{AroundMiddleware, Handler, status};
|
use iron::{AroundMiddleware, Handler, status};
|
||||||
|
@ -12,10 +7,17 @@ use params;
|
||||||
use secure_session::middleware::{SessionMiddleware, SessionConfig};
|
use secure_session::middleware::{SessionMiddleware, SessionConfig};
|
||||||
use secure_session::session::{SessionManager, ChaCha20Poly1305SessionManager};
|
use secure_session::session::{SessionManager, ChaCha20Poly1305SessionManager};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
use std::fs;
|
||||||
|
use std::io;
|
||||||
|
use std::path::*;
|
||||||
|
use std::ops::Deref;
|
||||||
|
use std::sync::Arc;
|
||||||
use typemap;
|
use typemap;
|
||||||
use url::percent_encoding::percent_decode;
|
use url::percent_encoding::percent_decode;
|
||||||
|
|
||||||
use db::DB;
|
use config::MiscSettings;
|
||||||
|
use db::{ConnectionSource, DB};
|
||||||
|
use db::misc_settings;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use thumbnails::*;
|
use thumbnails::*;
|
||||||
use index;
|
use index;
|
||||||
|
@ -52,11 +54,22 @@ impl typemap::Key for SessionKey {
|
||||||
type Value = Session;
|
type Value = Session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_auth_secret<T>(db: &T) -> Result<String>
|
||||||
|
where T: ConnectionSource
|
||||||
|
{
|
||||||
|
use self::misc_settings::dsl::*;
|
||||||
|
let connection = db.get_connection();
|
||||||
|
let connection = connection.lock().unwrap();
|
||||||
|
let connection = connection.deref();
|
||||||
|
let misc: MiscSettings = misc_settings.get_result(connection)?;
|
||||||
|
Ok(misc.auth_secret.to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_handler(db: Arc<DB>) -> Result<Chain> {
|
pub fn get_handler(db: Arc<DB>) -> Result<Chain> {
|
||||||
let api_handler = get_endpoints(db.clone());
|
let api_handler = get_endpoints(db.clone());
|
||||||
let mut api_chain = Chain::new(api_handler);
|
let mut api_chain = Chain::new(api_handler);
|
||||||
|
|
||||||
let auth_secret = db.deref().get_auth_secret()?;
|
let auth_secret = get_auth_secret(db.deref())?;
|
||||||
let session_manager =
|
let session_manager =
|
||||||
ChaCha20Poly1305SessionManager::<Session>::from_password(auth_secret.as_bytes());
|
ChaCha20Poly1305SessionManager::<Session>::from_password(auth_secret.as_bytes());
|
||||||
let session_config = SessionConfig::default();
|
let session_config = SessionConfig::default();
|
||||||
|
|
|
@ -59,23 +59,29 @@ pub fn parse(path: &path::Path) -> Result<UserConfig> {
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reset<T>(db: &T) -> Result<()> where T:ConnectionSource {
|
fn reset<T>(db: &T) -> Result<()>
|
||||||
|
where T: ConnectionSource
|
||||||
|
{
|
||||||
let connection = db.get_connection();
|
let connection = db.get_connection();
|
||||||
let connection = connection.lock().unwrap();
|
let connection = connection.lock().unwrap();
|
||||||
let connection = connection.deref();
|
let connection = connection.deref();
|
||||||
|
|
||||||
diesel::delete(mount_points::table).execute(connection)?;
|
diesel::delete(mount_points::table).execute(connection)?;
|
||||||
diesel::delete(users::table).execute(connection)?;
|
diesel::delete(users::table).execute(connection)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn overwrite<T>(db: &T, new_config: &UserConfig) -> Result<()> where T:ConnectionSource {
|
pub fn overwrite<T>(db: &T, new_config: &UserConfig) -> Result<()>
|
||||||
|
where T: ConnectionSource
|
||||||
|
{
|
||||||
reset(db)?;
|
reset(db)?;
|
||||||
ammend(db, new_config)
|
ammend(db, new_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ammend<T>(db: &T, new_config: &UserConfig) -> Result<()> where T:ConnectionSource {
|
pub fn ammend<T>(db: &T, new_config: &UserConfig) -> Result<()>
|
||||||
|
where T: ConnectionSource
|
||||||
|
{
|
||||||
let connection = db.get_connection();
|
let connection = db.get_connection();
|
||||||
let connection = connection.lock().unwrap();
|
let connection = connection.lock().unwrap();
|
||||||
let connection = connection.deref();
|
let connection = connection.deref();
|
||||||
|
|
|
@ -6,7 +6,6 @@ use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use config::MiscSettings;
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
mod schema;
|
mod schema;
|
||||||
|
@ -64,13 +63,6 @@ impl DB {
|
||||||
embedded_migrations::run(connection)?;
|
embedded_migrations::run(connection)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_auth_secret(&self) -> Result<String> {
|
|
||||||
let connection = self.connection.lock().unwrap();
|
|
||||||
let connection = connection.deref();
|
|
||||||
let misc: MiscSettings = misc_settings::table.get_result(connection)?;
|
|
||||||
Ok(misc.auth_secret.to_owned())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConnectionSource for DB {
|
impl ConnectionSource for DB {
|
||||||
|
|
Loading…
Add table
Reference in a new issue