Read config from config.toml; add it to the state

This commit is contained in:
Paul van Tilburg 2018-03-09 22:24:04 +01:00
parent 225e9c4cde
commit fdcb4f3799
4 changed files with 21 additions and 0 deletions

1
Cargo.lock generated
View File

@ -449,6 +449,7 @@ dependencies = [
"serde 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View File

@ -18,6 +18,7 @@ rocket_codegen = "0.3.6"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
toml = "0.4"
[dependencies.rocket_contrib]
version = "*"

1
config.toml Normal file
View File

@ -0,0 +1 @@
title = "Online Pinboard"

View File

@ -10,19 +10,37 @@ extern crate rocket_contrib;
extern crate serde_derive;
#[macro_use]
extern crate serde_json;
extern crate toml;
mod handlers;
mod models;
use rocket::Rocket;
use std::fs::File;
use std::io::prelude::*;
use std::sync::RwLock;
type NoteStore = RwLock<Vec<models::note::Note>>;
#[derive(Debug, Deserialize)]
struct Config {
title: String,
}
fn rocket(notes_path: Option<&str>) -> Rocket {
let mut config_data = String::new();
let mut config_file = File::open("config.toml").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::ignite()
.manage(RwLock::new(notes))
.manage(config)
.mount(
"/",
routes![handlers::home::index, handlers::static_files::all],