Add support for specifying a custom lists dir at startup-time

This commit is contained in:
Paul van Tilburg 2018-01-01 18:36:58 +01:00
parent 2ff634a01b
commit 4b33969427
2 changed files with 9 additions and 5 deletions

View File

@ -17,8 +17,8 @@ use std::sync::RwLock;
pub type ListStore = RwLock<Vec<models::list::List>>;
fn rocket() -> Rocket {
let lists = models::list::List::load_all();
fn rocket(lists_path: Option<&str>) -> Rocket {
let lists = models::list::List::load_all(lists_path);
rocket::ignite()
.manage(RwLock::new(lists))
.mount("/", routes![handlers::home::index,
@ -30,5 +30,5 @@ fn rocket() -> Rocket {
}
fn main() {
rocket().launch();
rocket(None).launch();
}

View File

@ -52,10 +52,14 @@ impl List {
self.mtime = metadata.modified().unwrap();
}
pub fn load_all() -> Vec<Self> {
pub fn load_all(list_path: Option<&str>) -> Vec<Self> {
let mut lists : Vec<List> = vec![];
let mut index = 0;
for entry in glob("lists/*.list").unwrap().filter_map(Result::ok) {
let path_glob = match list_path {
Some(dir) => format!("{}/lists/*.list", dir),
None => format!("lists/*.list")
};
for entry in glob(path_glob.as_str()).unwrap().filter_map(Result::ok) {
let file_name = entry.file_name().unwrap().to_str().unwrap();
let name = match file_name.find('.') {
Some(index) => &file_name[0..index],