Moved config functionality out of DB

This commit is contained in:
Antoine Gersant 2017-07-01 13:51:01 -07:00
parent 89d746268e
commit 87d43d0da5
3 changed files with 31 additions and 20 deletions

View file

@ -1,9 +1,4 @@
use std::fs;
use std::io;
use std::path::*;
use std::ops::Deref;
use std::sync::Arc;
use diesel::prelude::*;
use iron::prelude::*;
use iron::headers::{Authorization, Basic};
use iron::{AroundMiddleware, Handler, status};
@ -12,10 +7,17 @@ use params;
use secure_session::middleware::{SessionMiddleware, SessionConfig};
use secure_session::session::{SessionManager, ChaCha20Poly1305SessionManager};
use serde_json;
use std::fs;
use std::io;
use std::path::*;
use std::ops::Deref;
use std::sync::Arc;
use typemap;
use url::percent_encoding::percent_decode;
use db::DB;
use config::MiscSettings;
use db::{ConnectionSource, DB};
use db::misc_settings;
use errors::*;
use thumbnails::*;
use index;
@ -52,11 +54,22 @@ impl typemap::Key for SessionKey {
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> {
let api_handler = get_endpoints(db.clone());
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 =
ChaCha20Poly1305SessionManager::<Session>::from_password(auth_secret.as_bytes());
let session_config = SessionConfig::default();

View file

@ -59,23 +59,29 @@ pub fn parse(path: &path::Path) -> Result<UserConfig> {
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 = connection.lock().unwrap();
let connection = connection.deref();
diesel::delete(mount_points::table).execute(connection)?;
diesel::delete(users::table).execute(connection)?;
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)?;
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 = connection.lock().unwrap();
let connection = connection.deref();

View file

@ -6,7 +6,6 @@ use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use config::MiscSettings;
use errors::*;
mod schema;
@ -64,13 +63,6 @@ impl DB {
embedded_migrations::run(connection)?;
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 {