Smol refactor #1

Merged
glyph merged 3 commits from smol_refactor into master 2021-10-30 13:31:12 +00:00
4 changed files with 1249 additions and 753 deletions

1886
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,22 +1,14 @@
[package] [package]
name = "mycelial_technology" name = "mycelial_technology"
version = "0.1.0" version = "0.2.0"
authors = ["glyph <glyph@mycelial.technology>"] authors = ["glyph <glyph@mycelial.technology>"]
edition = "2018" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
log = "0.4" log = "0.4"
regex = "1" regex = "1"
rocket = "0.4" rocket = "0.5.0-rc.1"
rocket_dyn_templates = { version = "0.1.0-rc.1", features = ["tera"] }
rss = "1" rss = "1"
serde = "1" miniserde = "0.1"
serde_derive = "1"
serde_json = "1"
tera = "1" tera = "1"
[dependencies.rocket_contrib]
version = "0.4"
default-features = false
features = ["json", "tera_templates"]

View File

@ -1,12 +1,12 @@
extern crate regex; extern crate regex;
extern crate rss; extern crate rss;
use regex::Regex;
use rss::{ChannelBuilder, Item};
use std::error; use std::error;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use regex::Regex;
use rss::{ChannelBuilder, Item};
fn main() -> Result<(), Box<dyn error::Error>> { fn main() -> Result<(), Box<dyn error::Error>> {
// create rss channel for mycelial.technology // create rss channel for mycelial.technology
@ -25,11 +25,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
let plants = "./templates/plants"; let plants = "./templates/plants";
// add directories to a vector // add directories to a vector
let mut dirs = Vec::new(); let dirs = vec![bacteria, computers, fungi, plants];
dirs.push(bacteria);
dirs.push(computers);
dirs.push(fungi);
dirs.push(plants);
// create vectors for item fields // create vectors for item fields
let mut titles = Vec::new(); let mut titles = Vec::new();
@ -46,8 +42,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
// populate item url vector from article filenames // populate item url vector from article filenames
let re_url = Regex::new("./templates/(.*).html.tera")?; let re_url = Regex::new("./templates/(.*).html.tera")?;
let caps_url = re_url.captures( let caps_url = re_url.captures(
&path path.to_str()
.to_str()
.expect("Failed to convert file path to string slice for regex capture"), .expect("Failed to convert file path to string slice for regex capture"),
); );
if let Some(url) = caps_url { if let Some(url) = caps_url {

View File

@ -1,18 +1,16 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate log; extern crate log;
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
extern crate rocket_contrib;
#[macro_use]
extern crate serde_derive;
extern crate tera; extern crate tera;
use std::path::{Path, PathBuf}; use miniserde::{json, Serialize};
use rocket::{
use rocket::response::NamedFile; fs::{relative, FileServer},
use rocket_contrib::templates::Template; get, routes,
};
use rocket_dyn_templates::Template;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct FlashContext { struct FlashContext {
@ -20,18 +18,13 @@ struct FlashContext {
flash_msg: Option<String>, flash_msg: Option<String>,
} }
#[get("/<file..>")]
fn files(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("static/").join(file)).ok()
}
#[get("/art")] #[get("/art")]
fn art() -> Template { fn art() -> Template {
let context = FlashContext { let context = FlashContext {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("art", &context) Template::render("art", json::to_string(&context))
} }
#[get("/background")] #[get("/background")]
@ -40,7 +33,7 @@ fn background() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("background", &context) Template::render("background", json::to_string(&context))
} }
#[get("/bacteria")] #[get("/bacteria")]
@ -49,7 +42,7 @@ fn bacteria() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("bacteria", &context) Template::render("bacteria", json::to_string(&context))
} }
#[get("/bacteria/sauerkraut-beginnings")] #[get("/bacteria/sauerkraut-beginnings")]
@ -58,7 +51,7 @@ fn bacteria_sauerkraut_beginnings() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("bacteria/sauerkraut_beginnings", &context) Template::render("bacteria/sauerkraut_beginnings", json::to_string(&context))
} }
#[get("/bacteria/sauerkraut-bottled")] #[get("/bacteria/sauerkraut-bottled")]
@ -67,7 +60,7 @@ fn bacteria_sauerkraut_bottled() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("bacteria/sauerkraut_bottled", &context) Template::render("bacteria/sauerkraut_bottled", json::to_string(&context))
} }
#[get("/computers")] #[get("/computers")]
@ -76,7 +69,7 @@ fn computers() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("computers", &context) Template::render("computers", json::to_string(&context))
} }
#[get("/computers/esp8266-dht11")] #[get("/computers/esp8266-dht11")]
@ -85,7 +78,7 @@ fn computers_esp8266_dht11() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("computers/esp8266_dht11", &context) Template::render("computers/esp8266_dht11", json::to_string(&context))
} }
#[get("/computers/i2c-adventures")] #[get("/computers/i2c-adventures")]
@ -94,7 +87,7 @@ fn computers_i2c_adventures() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("computers/i2c_adventures", &context) Template::render("computers/i2c_adventures", json::to_string(&context))
} }
#[get("/computers/rust-compilation")] #[get("/computers/rust-compilation")]
@ -103,7 +96,7 @@ fn computers_rust_compilation() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("computers/rust_compilation", &context) Template::render("computers/rust_compilation", json::to_string(&context))
} }
#[get("/fungi")] #[get("/fungi")]
@ -112,7 +105,7 @@ fn fungi() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("fungi", &context) Template::render("fungi", json::to_string(&context))
} }
#[get("/fungi/design-patterns")] #[get("/fungi/design-patterns")]
@ -121,7 +114,7 @@ fn fungi_design_patterns() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("fungi/design_patterns", &context) Template::render("fungi/design_patterns", json::to_string(&context))
} }
#[get("/fungi/glossary")] #[get("/fungi/glossary")]
@ -130,7 +123,7 @@ fn fungi_glossary() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("fungi/glossary", &context) Template::render("fungi/glossary", json::to_string(&context))
} }
#[get("/fungi/grow-forests")] #[get("/fungi/grow-forests")]
@ -139,7 +132,7 @@ fn fungi_grow_forests() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("fungi/grow_forests", &context) Template::render("fungi/grow_forests", json::to_string(&context))
} }
#[get("/fungi/grow-together")] #[get("/fungi/grow-together")]
@ -148,7 +141,7 @@ fn fungi_grow_together() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("fungi/grow_together", &context) Template::render("fungi/grow_together", json::to_string(&context))
} }
#[get("/fungi/lichen-space")] #[get("/fungi/lichen-space")]
@ -157,7 +150,7 @@ fn fungi_lichen_space() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("fungi/lichen_space", &context) Template::render("fungi/lichen_space", json::to_string(&context))
} }
#[get("/fungi/network-resilience")] #[get("/fungi/network-resilience")]
@ -166,7 +159,7 @@ fn fungi_network_resilience() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("fungi/network_resilience", &context) Template::render("fungi/network_resilience", json::to_string(&context))
} }
#[get("/fungi/photo-guide")] #[get("/fungi/photo-guide")]
@ -175,7 +168,7 @@ fn fungi_photo_guide() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("fungi/photo_guide", &context) Template::render("fungi/photo_guide", json::to_string(&context))
} }
#[get("/fungi/reading-list")] #[get("/fungi/reading-list")]
@ -184,7 +177,7 @@ fn fungi_reading_list() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("fungi/reading_list", &context) Template::render("fungi/reading_list", json::to_string(&context))
} }
#[get("/")] #[get("/")]
@ -193,7 +186,7 @@ fn home() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("home", &context) Template::render("home", json::to_string(&context))
} }
#[get("/lists")] #[get("/lists")]
@ -202,7 +195,7 @@ fn lists() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("lists", &context) Template::render("lists", json::to_string(&context))
} }
#[get("/meditation")] #[get("/meditation")]
@ -211,7 +204,7 @@ fn meditation() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("meditation", &context) Template::render("meditation", json::to_string(&context))
} }
#[get("/plants")] #[get("/plants")]
@ -220,7 +213,7 @@ fn plants() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("plants", &context) Template::render("plants", json::to_string(&context))
} }
#[get("/plants/aloe-there")] #[get("/plants/aloe-there")]
@ -229,7 +222,7 @@ fn plants_aloe_there() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("plants/aloe_there", &context) Template::render("plants/aloe_there", json::to_string(&context))
} }
#[get("/plants/blueberry-dance")] #[get("/plants/blueberry-dance")]
@ -238,7 +231,7 @@ fn plants_blueberry_dance() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("plants/blueberry_dance", &context) Template::render("plants/blueberry_dance", json::to_string(&context))
} }
#[get("/plants/botanical-deceptions")] #[get("/plants/botanical-deceptions")]
@ -247,7 +240,7 @@ fn plants_botanical_deceptions() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("plants/botanical_deceptions", &context) Template::render("plants/botanical_deceptions", json::to_string(&context))
} }
#[get("/plants/potato-tech")] #[get("/plants/potato-tech")]
@ -256,7 +249,7 @@ fn plants_potato_tech() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("plants/potato_tech", &context) Template::render("plants/potato_tech", json::to_string(&context))
} }
#[get("/projects")] #[get("/projects")]
@ -265,7 +258,7 @@ fn projects() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("projects", &context) Template::render("projects", json::to_string(&context))
} }
#[get("/support")] #[get("/support")]
@ -274,7 +267,7 @@ fn support() -> Template {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
}; };
Template::render("support", &context) Template::render("support", json::to_string(&context))
} }
#[catch(404)] #[catch(404)]
@ -284,15 +277,15 @@ fn not_found() -> Template {
flash_name: Some("error".to_string()), flash_name: Some("error".to_string()),
flash_msg: Some("No resource found for given URL".to_string()), flash_msg: Some("No resource found for given URL".to_string()),
}; };
Template::render("not_found", context) Template::render("not_found", json::to_string(&context))
} }
fn main() { #[launch]
rocket::ignite() fn rocket() -> _ {
rocket::build()
.mount( .mount(
"/", "/",
routes![ routes![
files,
art, art,
background, background,
bacteria, bacteria,
@ -323,7 +316,7 @@ fn main() {
support support
], ],
) )
.register(catchers![not_found]) .mount("/", FileServer::from(relative!("static")))
.register("/", catchers![not_found])
.attach(Template::fairing()) .attach(Template::fairing())
.launch();
} }