2020-02-03 21:38:20 +01:00
|
|
|
use std::sync::RwLock;
|
2021-06-13 21:59:22 +02:00
|
|
|
|
|
|
|
use rocket::fs::{relative, FileServer};
|
|
|
|
use rocket::serde::Deserialize;
|
|
|
|
use rocket::{routes, Build, Rocket};
|
|
|
|
use rocket_dyn_templates::Template;
|
2020-02-03 21:38:20 +01:00
|
|
|
use toml;
|
2017-12-06 15:29:10 +01:00
|
|
|
|
2017-12-30 22:06:48 +01:00
|
|
|
mod handlers;
|
2017-12-30 22:23:16 +01:00
|
|
|
mod models;
|
2017-12-06 15:38:07 +01:00
|
|
|
|
2018-01-26 21:18:04 +01:00
|
|
|
type NoteStore = RwLock<Vec<models::note::Note>>;
|
2017-12-30 22:23:16 +01:00
|
|
|
|
2018-03-09 22:24:04 +01:00
|
|
|
#[derive(Debug, Deserialize)]
|
2021-06-13 21:59:22 +02:00
|
|
|
#[serde(crate = "rocket::serde")]
|
2018-12-18 15:47:34 +01:00
|
|
|
pub struct Config {
|
2018-03-09 22:24:04 +01:00
|
|
|
title: String,
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:18:35 +02:00
|
|
|
fn build_rocket(notes_path: Option<&str>) -> Rocket<Build> {
|
2018-12-18 15:48:36 +01:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2018-03-09 22:24:04 +01:00
|
|
|
let mut config_data = String::new();
|
2018-12-18 15:48:36 +01:00
|
|
|
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");
|
2018-03-09 22:24:04 +01:00
|
|
|
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");
|
|
|
|
|
2018-01-26 21:18:04 +01:00
|
|
|
let notes = models::note::Note::load_all(notes_path);
|
2018-03-09 22:24:04 +01:00
|
|
|
|
2021-06-13 21:59:22 +02:00
|
|
|
rocket::build()
|
2018-01-26 21:18:04 +01:00
|
|
|
.manage(RwLock::new(notes))
|
2018-03-09 22:24:04 +01:00
|
|
|
.manage(config)
|
2021-06-13 21:59:22 +02:00
|
|
|
.mount("/", FileServer::from(relative!("static")))
|
|
|
|
.mount("/", routes![handlers::home::index])
|
2018-02-15 20:49:42 +01:00
|
|
|
.mount(
|
|
|
|
"/notes",
|
|
|
|
routes![
|
|
|
|
handlers::note::index,
|
|
|
|
handlers::note::show_html,
|
|
|
|
handlers::note::show_json,
|
|
|
|
handlers::note::update
|
|
|
|
],
|
|
|
|
)
|
2021-06-13 21:59:22 +02:00
|
|
|
.attach(Template::fairing())
|
2017-12-06 15:38:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-17 19:18:35 +02:00
|
|
|
#[rocket::launch]
|
|
|
|
fn rocket() -> _ {
|
|
|
|
build_rocket(None)
|
2017-12-06 15:29:10 +01:00
|
|
|
}
|