use std::sync::RwLock; use rocket::fs::{relative, FileServer}; use rocket::serde::Deserialize; use rocket::{routes, Build, Rocket}; use rocket_dyn_templates::Template; use toml; mod handlers; mod models; type NoteStore = RwLock>; #[derive(Debug, Deserialize)] #[serde(crate = "rocket::serde")] pub struct Config { title: String, } fn build_rocket(notes_path: Option<&str>) -> Rocket { use std::fs::File; use std::io::prelude::*; use std::path::Path; let mut config_data = String::new(); let config_file_name = Path::new(env!("CARGO_MANIFEST_DIR")).join("config.toml"); let mut config_file = File::open(config_file_name).expect("Cannot find config file: config.toml"); config_file .read_to_string(&mut config_data) .expect("Cannot read config file: config.toml"); let config: Config = toml::from_str(&config_data).expect("Cannot parse config file: config.toml"); let notes = models::note::Note::load_all(notes_path); rocket::build() .manage(RwLock::new(notes)) .manage(config) .mount("/", FileServer::from(relative!("static"))) .mount("/", routes![handlers::home::index]) .mount( "/notes", routes![ handlers::note::index, handlers::note::show_html, handlers::note::show_json, handlers::note::update ], ) .attach(Template::fairing()) } #[rocket::launch] fn rocket() -> _ { build_rocket(None) }