From 74e3b99e23e3f64403c8bc750e96f1d916465f0a Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Sat, 27 Mar 2021 09:33:24 +0100 Subject: [PATCH 01/13] Update project structure --- .gitignore | 1 + src/auth.rs | 1 + src/bin/caty-blog.rs | 22 +-- src/config.rs | 1 + src/dirs.rs | 1 + src/logging.rs | 1 + src/server.rs | 1 + templates/main.tmpl | 29 --- webroot/index.html | 31 ---- webroot/main/post1.md | 3 - webroot/main/post2.md | 3 - webroot/static/caty.css | 384 ---------------------------------------- 12 files changed, 12 insertions(+), 466 deletions(-) create mode 100644 src/auth.rs create mode 100644 src/config.rs create mode 100644 src/dirs.rs create mode 100644 src/logging.rs create mode 100644 src/server.rs delete mode 100644 templates/main.tmpl delete mode 100644 webroot/index.html delete mode 100644 webroot/main/post1.md delete mode 100644 webroot/main/post2.md delete mode 100644 webroot/static/caty.css diff --git a/.gitignore b/.gitignore index ea8c4bf..bf670f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/working diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..2f502f0 --- /dev/null +++ b/src/auth.rs @@ -0,0 +1 @@ +// Placeholder for authentication diff --git a/src/bin/caty-blog.rs b/src/bin/caty-blog.rs index 0af6089..a3362ab 100644 --- a/src/bin/caty-blog.rs +++ b/src/bin/caty-blog.rs @@ -1,25 +1,15 @@ use caty_blog::*; -use warp::Filter; - #[tokio::main] async fn main() -> Result<(), Box> { - // TODO: Load configuration file defining paths to stuff - // Load into memory as a Struct in an Arc(Mutex(something)) - // This should also include the admin username/hash(password) - - write_main(&render_main())?; - - let static_files = warp::path("static") - .and(warp::fs::dir("webroot/static")); - - let index = warp::get() - .and(warp::path::end()) - .and(warp::fs::file("webroot/index.html")); + // Load config - let routes = static_files.or(index); + // Configure logging - warp::serve(routes).run(([127,0,0,1], 3030)).await; + // Do shit based on arguments + // - Either generate the base directory structure for a blog + // - Or load an existing site and serve its routes based on the loaded config + // warp::serve(routes).run(([127,0,0,1], 3030)).await; Ok(()) } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..188b644 --- /dev/null +++ b/src/config.rs @@ -0,0 +1 @@ +// Placeholder for config loading diff --git a/src/dirs.rs b/src/dirs.rs new file mode 100644 index 0000000..1aa36b5 --- /dev/null +++ b/src/dirs.rs @@ -0,0 +1 @@ +// Placeholder for the directory creation logic diff --git a/src/logging.rs b/src/logging.rs new file mode 100644 index 0000000..5bfec07 --- /dev/null +++ b/src/logging.rs @@ -0,0 +1 @@ +// Placeholder for logging setup diff --git a/src/server.rs b/src/server.rs new file mode 100644 index 0000000..b55fa63 --- /dev/null +++ b/src/server.rs @@ -0,0 +1 @@ +// Placeholder for Warp routes diff --git a/templates/main.tmpl b/templates/main.tmpl deleted file mode 100644 index 8783611..0000000 --- a/templates/main.tmpl +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - -A Blog Example - - -
-
-

Quite Magical Really

- -
-
- -
-{%- for post in posts %} -{{ post }} -{%- endfor -%} -
- -
-

This brilliance was brought to you by.. ME

-
- - diff --git a/webroot/index.html b/webroot/index.html deleted file mode 100644 index 6ff1072..0000000 --- a/webroot/index.html +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - -A Blog Example - - -
-
-

Quite Magical Really

- -
-
- -
-

WOWWWWWWWWWWWWWW

-

Another one.

- -

Omfg Amazing

-

This is some cool shit here. Like srsly wtf.

-
- -
-

This brilliance was brought to you by.. ME

-
- - diff --git a/webroot/main/post1.md b/webroot/main/post1.md deleted file mode 100644 index eb94f1c..0000000 --- a/webroot/main/post1.md +++ /dev/null @@ -1,3 +0,0 @@ -### Omfg Amazing - -This is some cool shit here. Like srsly wtf. diff --git a/webroot/main/post2.md b/webroot/main/post2.md deleted file mode 100644 index c4a8169..0000000 --- a/webroot/main/post2.md +++ /dev/null @@ -1,3 +0,0 @@ -### WOWWWWWWWWWWWWWW - -Another one. diff --git a/webroot/static/caty.css b/webroot/static/caty.css deleted file mode 100644 index 49d056d..0000000 --- a/webroot/static/caty.css +++ /dev/null @@ -1,384 +0,0 @@ -:root { - --sans-font:-apple-system,BlinkMacSystemFont,"Avenir Next",Avenir,"Nimbus Sans L",Roboto,Noto,"Segoe UI",Arial,Helvetica,"Helvetica Neue",sans-serif; - --mono-font:Consolas,Menlo,Monaco,"Andale Mono","Ubuntu Mono",monospace; - --base-fontsize:1.15rem; - --header-scale:1.25; - --line-height:1.618; - --bg: #135F4Cff; - --accent-bg:#136044ff; - --text: #B2BABBff; - --text-light: #AFB8B9ff; - --border:#829191; - --accent:#79989B; - --accent-light:#90CAF9; - --code: #BABFBFff; - --preformatted: #A4B2B3ff; - --marked: #A0A6BE; - --disabled:#EFEFEF -} -@media (prefers-color-scheme:dark) { - :root { - --bg:#212121; - --accent-bg:#2B2B2B; - --text:#DCDCDC; - --text-light:#ABABAB; - --border:#666; - --accent:#FFB300; - --accent-light:#FFECB3; - --code:#F06292; - --preformatted:#CCC; - --disabled:#111 - } - img, - video { - opacity:.6 - } -} -html { - font-family:var(--sans-font); - font-size:16px -} -body { - color:var(--text); - background:var(--bg); - font-size:var(--base-fontsize); - line-height:var(--line-height); - display:flex; - min-height:100vh; - flex-direction:column; - flex:1; - margin:0 auto; - max-width:45rem; - padding:0 .5rem; - overflow-x:hidden; - word-break:break-word; - overflow-wrap:break-word -} -header { - background:var(--accent-bg); - border-bottom:1px solid var(--border); - text-align:center; - padding:2rem .5rem; - width:100vw; - position:relative; - left:50%; - right:50%; - margin-left:-50vw; - margin-right:-50vw -} -header h1, -header p { - margin:0 -} -header h1 { - line-height:1.1 -} -nav { - font-size:1rem; - line-height:2; - padding:1rem 0 -} -nav a { - margin:1rem 1rem 0 0; - border:1px solid var(--border); - border-radius:5px; - color:var(--text)!important; - display:inline-block; - padding:.1rem 1rem; - text-decoration:none; - transition:.4s -} -nav a:hover { - color:var(--accent)!important; - border-color:var(--accent) -} -nav a.current:hover { - text-decoration:none -} -footer { - margin-top:4rem; - padding:2rem 1rem 1.5rem 1rem; - color:var(--text-light); - font-size:.9rem; - text-align:center; - border-top:1px solid var(--border) -} -h1 { - font-size:calc(var(--base-fontsize) * var(--header-scale) * var(--header-scale) * var(--header-scale) * var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -h2 { - font-size:calc(var(--base-fontsize) * var(--header-scale) * var(--header-scale) * var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -h3 { - font-size:calc(var(--base-fontsize) * var(--header-scale) * var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -h4 { - font-size:calc(var(--base-fontsize) * var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -h5 { - font-size:var(--base-fontsize); - margin-top:calc(var(--line-height) * 1.5rem) -} -h6 { - font-size:calc(var(--base-fontsize)/ var(--header-scale)); - margin-top:calc(var(--line-height) * 1.5rem) -} -a, -a:visited { - color:var(--accent) -} -a:hover { - text-decoration:none -} -a button, -button, -input[type=button], -input[type=reset], -input[type=submit] { - border:none; - border-radius:5px; - background:var(--accent); - font-size:1rem; - color:var(--bg); - padding:.7rem .9rem; - margin:.5rem 0; - transition:.4s -} -a button[disabled], -button[disabled], -input[type=button][disabled], -input[type=checkbox][disabled], -input[type=radio][disabled], -input[type=reset][disabled], -input[type=submit][disabled], -select[disabled] { - cursor:default; - opacity:.5; - cursor:not-allowed -} -input:disabled, -select:disabled, -textarea:disabled { - cursor:not-allowed; - background-color:var(--disabled) -} -input[type=range] { - padding:0 -} -abbr { - cursor:help -} -button:enabled:hover, -button:focus, -input[type=button]:enabled:hover, -input[type=button]:focus, -input[type=checkbox]:enabled:hover, -input[type=checkbox]:focus, -input[type=radio]:enabled:hover, -input[type=radio]:focus, -input[type=reset]:enabled:hover, -input[type=reset]:focus, -input[type=submit]:enabled:hover, -input[type=submit]:focus { - opacity:.8 -} -details { - padding:.6rem 1rem; - background:var(--accent-bg); - border:1px solid var(--border); - border-radius:5px; - margin-bottom:1rem -} -summary { - cursor:pointer; - font-weight:700 -} -details[open] { - padding-bottom:.75rem -} -details[open] summary { - margin-bottom:.5rem -} -details[open]>:last-child { - margin-bottom:0 -} -table { - border-collapse:collapse; - width:100% margin: 1.5rem 0 -} -td, -th { - border:1px solid var(--border); - text-align:left; - padding:.5rem -} -th { - background:var(--accent-bg); - font-weight:700 -} -tr:nth-child(even) { - background:var(--accent-bg) -} -table caption { - font-weight:700; - margin-bottom:.5rem -} -ol, -ul { - padding-left:3rem -} -input, -select, -textarea { - font-size:inherit; - font-family:inherit; - padding:.5rem; - margin-bottom:.5rem; - color:var(--text); - background:var(--bg); - border:1px solid var(--border); - border-radius:5px; - box-shadow:none; - box-sizing:border-box; - width:60%; - appearance:none; - -moz-appearance:none; - -webkit-appearance:none -} -select { - background-image:linear-gradient(45deg,transparent 49%,var(--text) 51%),linear-gradient(135deg,var(--text) 51%,transparent 49%); - background-position:calc(100% - 20px),calc(100% - 15px); - background-size:5px 5px,5px 5px; - background-repeat:no-repeat -} -input[type=checkbox], -input[type=radio] { - vertical-align:bottom; - position:relative -} -input[type=radio] { - border-radius:100% -} -input[type=checkbox]:checked, -input[type=radio]:checked { - background:var(--accent) -} -input[type=checkbox]:checked::after { - content:' '; - width:.1em; - height:.25em; - border-radius:0; - position:absolute; - top:.05em; - left:.18em; - background:0 0; - border-right:solid var(--bg) .08em; - border-bottom:solid var(--bg) .08em; - font-size:1.8em; - transform:rotate(45deg) -} -input[type=radio]:checked::after { - content:' '; - width:.25em; - height:.25em; - border-radius:100%; - position:absolute; - top:.125em; - background:var(--bg); - left:.125em; - font-size:32px -} -textarea { - width:80% -} -@media only screen and (max-width:720px) { - input, - select, - textarea { - width:100% - } -} -input[type=checkbox], -input[type=radio] { - width:auto -} -input[type=file] { - border:0 -} -fieldset { - border:0; - padding:0; - margin:0 -} -hr { - color:var(--border); - border-top:1px; - margin:1rem auto -} -mark { - padding:2px 5px; - border-radius:4px; - background:var(--marked) -} -main img, -main video { - max-width:100%; - border-radius:5px -} -figure { - margin:0 -} -figcaption { - font-size:.9rem; - color:var(--text-light); - text-align:center; - margin-bottom:1rem -} -blockquote { - margin:2rem 0 2rem 2rem; - padding:.4rem .8rem; - border-left:.35rem solid var(--accent); - opacity:.8; - font-style:italic -} -cite { - font-size:.9rem; - color:var(--text-light); - font-style:normal -} -code, -kbd, -pre, -pre span, -samp { - font-size:1.075rem; - font-family:var(--mono-font); - color:var(--code) -} -kbd { - color:var(--preformatted); - border:1px solid var(--preformatted); - border-bottom:3px solid var(--preformatted); - border-radius:5px; - padding:.1rem -} -pre { - padding:1rem 1.4rem; - max-width:100%; - overflow:auto; - overflow-x:auto; - color:var(--preformatted); - background:var(--accent-bg); - border:1px solid var(--border); - border-radius:5px -} -pre code { - color:var(--preformatted); - background:0 0; - margin:0; - padding:0 -} From 3b5b815b21019f68a4a391060cf7a704a1595d86 Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Sat, 27 Mar 2021 16:11:22 +0100 Subject: [PATCH 02/13] Complete work on loading arguments and configuration --- Cargo.lock | 32 +++++++-- Cargo.toml | 3 +- src/bin/caty-blog.rs | 3 +- src/config.rs | 134 +++++++++++++++++++++++++++++++++++- src/lib.rs | 87 +---------------------- src/server.rs | 54 +++++++++++++++ test_files/test-config.toml | 10 +++ 7 files changed, 227 insertions(+), 96 deletions(-) create mode 100644 test_files/test-config.toml diff --git a/Cargo.lock b/Cargo.lock index e20253c..4a84481 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -119,10 +119,11 @@ name = "caty_blog" version = "0.1.0" dependencies = [ "clap", - "glob", "pulldown-cmark", + "serde", "tera", "tokio", + "toml", "warp", ] @@ -336,12 +337,6 @@ dependencies = [ "wasi 0.10.2+wasi-snapshot-preview1", ] -[[package]] -name = "glob" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" - [[package]] name = "globset" version = "0.4.6" @@ -1023,6 +1018,20 @@ name = "serde" version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.124" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1800f7693e94e186f5e25a28291ae1570da908aff7d97a095dec1e56ff99069b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "serde_json" @@ -1278,6 +1287,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + [[package]] name = "tower-service" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 194c70f..219a002 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,11 @@ edition = "2018" [dependencies] clap = "2.33.3" -glob = "0.3.0" tera = "1.7.0" pulldown-cmark = { version = "0.8", default-features = false, features = ["simd"] } +serde = { version = "1", features = ["derive"] } tokio = { version = "1", features = ["full"] } +toml = "0.5" warp = "0.3" [profile.release] diff --git a/src/bin/caty-blog.rs b/src/bin/caty-blog.rs index a3362ab..ece40ec 100644 --- a/src/bin/caty-blog.rs +++ b/src/bin/caty-blog.rs @@ -2,7 +2,8 @@ use caty_blog::*; #[tokio::main] async fn main() -> Result<(), Box> { - // Load config + let config = config::load_config()?; + println!("{:?}", config); // Configure logging diff --git a/src/config.rs b/src/config.rs index 188b644..f58902b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1 +1,133 @@ -// Placeholder for config loading +use std::collections::HashMap; +use std::path::Path; + +use clap::{App, AppSettings, Arg, ArgMatches, SubCommand, Values}; +use serde::Deserialize; +use toml; + + +#[derive(Debug)] +pub enum Config { + Main(AppConfig), + New(BuildConfig), +} + +fn args() -> App<'static, 'static> { + App::new("Caty's Blog") + .version("1.0") + .author("Anthony Martinez") + .setting(AppSettings::ArgRequiredElseHelp) + .subcommand(SubCommand::with_name("run") + .about("Run the blog server") + .arg(Arg::with_name("config") + .required(true) + .takes_value(true) + .index(1) + .help("Provides the configuration"))) + .subcommand(SubCommand::with_name("new") + .arg(Arg::with_name("topics") + .required(true) + .short("t") + .long("topic") + .multiple(true) + .min_values(1))) +} + +pub fn load_config() -> Result> { + let matches = args().get_matches(); + let config: Result>; + if matches.is_present("run") { + config = runner_config(matches); + } else if matches.is_present("new") { + config = init_config(matches); + } else { + let msg = format!("Unable to load configuration"); + config = Err(From::from(msg)); + } + + config +} + +fn runner_config(m: ArgMatches) -> Result> { + if let Some(run) = m.subcommand_matches("run") { + if let Some(value) = run.value_of("config") { + let config = AppConfig::new(value)?; + Ok(Config::Main(config)) + } else { + let msg = format!("Failed to load configuration"); + Err(From::from(msg)) + } + } else { + let msg = format!("Failed to read arguments for 'run' subcommand"); + Err(From::from(msg)) + } +} + +fn init_config(m: ArgMatches) -> Result> { + if let Some(new) = m.subcommand_matches("new") { + if let Some(values) = new.values_of("topics") { + let config = BuildConfig::new(values)?; + Ok(Config::New(config)) + } else { + let msg = format!("Failed to load configuration"); + Err(From::from(msg)) + } + } else { + let msg = format!("Failed to read arguments for 'new' subcommand"); + Err(From::from(msg)) + } +} + +type Dict = HashMap; + +#[derive(Debug, Deserialize)] +pub struct AppConfig { + creds: Dict, + logging: Dict, + docpaths: Dict, +} + +impl AppConfig { + pub fn new(config: T) -> Result> + where T: AsRef { + let config = std::fs::read_to_string(config)?; + let app_config: AppConfig = toml::from_str(&config)?; + Ok(app_config) + } +} + +#[derive(Debug, Deserialize)] +pub struct BuildConfig { + topics: Vec +} + +impl BuildConfig { + pub fn new(values: Values) -> Result> { + let topics: Vec = values.map(|x| String::from(x)).collect(); + Ok(BuildConfig { topics } ) + } +} + +#[cfg(test)] +mod tests{ + use super::*; + #[test] + fn build_run_config() { + let arg_vec = vec!["caty-blog", "run", "./test_files/test-config.toml"]; + let matches = args().get_matches_from(arg_vec); + let config = runner_config(matches); + assert!(config.is_ok()); + let config = config.unwrap(); + assert!(matches!(config, Config::Main(_))); + } + + #[test] + fn build_new_config() { + let arg_vec = vec!["caty-blog", "new", "-t", "yoga", "cooking"]; + let matches = args().get_matches_from(arg_vec); + let config = init_config(matches); + assert!(config.is_ok()); + let config = config.unwrap(); + assert!(matches!(config, Config::New(_))); + } +} diff --git a/src/lib.rs b/src/lib.rs index 87ca204..ef68c36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,86 +1 @@ -use std::fs::File; -use std::path::PathBuf; -use std::io::prelude::*; - -use clap::{App, Arg}; -use glob::glob; -use pulldown_cmark::{Parser, html}; -use tera::{Tera, Context}; - - -// Todo expand parsing to include a path for loading config -pub fn parse_args() -> String { - let mut pat_string = String::new(); - let matches = App::new("Make Blog Posts") - .version("1.0") - .author("Anthony Martinez") - .about("I made it of course it rocks") - .arg(Arg::with_name("glob_pattern") - .short("p") - .long("--pattern") - .help("Provides the input file glob pattern") - .takes_value(true) - .required(true)) - .get_matches(); - - if let Some(pattern) = matches.value_of("glob_pattern") { - pat_string = String::from(pattern); - } - - pat_string -} - -pub fn path_matches(pat: &str) -> Vec { - let mut path_vec: Vec = Vec::new(); - for path in glob(pat).unwrap().filter_map(Result::ok) { - path_vec.push(path) - } - - path_vec -} - -pub fn read_to_html(paths: Vec) -> Vec { - let mut contents: Vec = Vec::new(); - for path in paths { - let buffer = std::fs::read_to_string(path).unwrap(); - let parser = Parser::new(&buffer); - let mut html_output = String::new(); - html::push_html(&mut html_output, parser); - contents.push(html_output); - } - contents -} - -pub fn load_templates(dir: &str) -> Tera { - match Tera::new(dir) { - Ok(t) => t, - Err(e) => panic!("Failed with {}", e) - } -} - -pub fn render_main() -> String { - let pattern = parse_args(); - let path_vec = path_matches(&pattern); - let mut data = read_to_html(path_vec); - data.reverse(); - - let tera = load_templates("templates/*.tmpl"); - let mut context = Context::new(); - - context.insert("posts", &data); - - let output = tera.render("main.tmpl", &context); - - if let Ok(output) = output { - output - } else { - String::new() - } -} - -pub fn write_main(rendered: &str) -> Result<(), Box> { - let buf = rendered.as_bytes(); - let mut f = File::create("webroot/index.html")?; - f.write_all(buf)?; - Ok(()) -} +pub mod config; diff --git a/src/server.rs b/src/server.rs index b55fa63..47778e0 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1 +1,55 @@ +use std::fs::File; +use std::io::prelude::*; +use std::path::PathBuf; +use pulldown_cmark::{Parser, html}; +use tera::{Tera, Context}; + // Placeholder for Warp routes + +/* +pub fn read_to_html(paths: Vec) -> Vec { + let mut contents: Vec = Vec::new(); + for path in paths { + let buffer = std::fs::read_to_string(path).unwrap(); + let parser = Parser::new(&buffer); + let mut html_output = String::new(); + html::push_html(&mut html_output, parser); + contents.push(html_output); + } + contents +} + +pub fn load_templates(dir: &str) -> Tera { + match Tera::new(dir) { + Ok(t) => t, + Err(e) => panic!("Failed with {}", e) + } +} + +pub fn render_main() -> String { + let pattern = parse_args(); + let path_vec = path_matches(&pattern); + let mut data = read_to_html(path_vec); + data.reverse(); + + let tera = load_templates("templates/*.tmpl"); + let mut context = Context::new(); + + context.insert("posts", &data); + + let output = tera.render("main.tmpl", &context); + + if let Ok(output) = output { + output + } else { + String::new() + } +} + +pub fn write_main(rendered: &str) -> Result<(), Box> { + let buf = rendered.as_bytes(); + let mut f = File::create("webroot/index.html")?; + f.write_all(buf)?; + Ok(()) +} +*/ diff --git a/test_files/test-config.toml b/test_files/test-config.toml new file mode 100644 index 0000000..799051d --- /dev/null +++ b/test_files/test-config.toml @@ -0,0 +1,10 @@ +[creds] +user = "caty" +password = "123456" + +[logging] +level = "INFO" + +[docpaths] +templates = "/var/www/blog/templates" +topics = "/var/www/blog/topics" \ No newline at end of file From 6beb6c6af737916a2925204061191a5aaae8fdaa Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Sat, 27 Mar 2021 16:18:27 +0100 Subject: [PATCH 03/13] Update about/help messages for Clap --- src/config.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index f58902b..3cc88a5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,12 +20,14 @@ fn args() -> App<'static, 'static> { .subcommand(SubCommand::with_name("run") .about("Run the blog server") .arg(Arg::with_name("config") + .help("Provides the path to the server configuration file.") .required(true) .takes_value(true) - .index(1) - .help("Provides the configuration"))) + .index(1))) .subcommand(SubCommand::with_name("new") + .about("Generates a base directory structure for a new blog") .arg(Arg::with_name("topics") + .help("Provides the list of topics for the generator.") .required(true) .short("t") .long("topic") From 5015f21edee476e913b8086de4a26b676bcd4426 Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Sun, 28 Mar 2021 23:01:59 +0200 Subject: [PATCH 04/13] Expand config.rs with additional options --- src/bin/caty-blog.rs | 2 +- src/config.rs | 98 +++++++++++++++++++++++++++---------- test_files/test-config.toml | 9 +++- 3 files changed, 81 insertions(+), 28 deletions(-) diff --git a/src/bin/caty-blog.rs b/src/bin/caty-blog.rs index ece40ec..f55e064 100644 --- a/src/bin/caty-blog.rs +++ b/src/bin/caty-blog.rs @@ -2,7 +2,7 @@ use caty_blog::*; #[tokio::main] async fn main() -> Result<(), Box> { - let config = config::load_config()?; + let config = config::load()?; println!("{:?}", config); // Configure logging diff --git a/src/config.rs b/src/config.rs index 3cc88a5..0b9b40c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::io::prelude::*; use std::path::Path; use clap::{App, AppSettings, Arg, ArgMatches, SubCommand, Values}; @@ -25,7 +25,13 @@ fn args() -> App<'static, 'static> { .takes_value(true) .index(1))) .subcommand(SubCommand::with_name("new") - .about("Generates a base directory structure for a new blog") + .about("Generates a base directory structure and configuration file for a new blog") + .arg(Arg::with_name("username") + .help("Provides the username for the blog's admin") + .required(true) + .takes_value(true) + .short("u") + .long("user")) .arg(Arg::with_name("topics") .help("Provides the list of topics for the generator.") .required(true) @@ -35,7 +41,7 @@ fn args() -> App<'static, 'static> { .min_values(1))) } -pub fn load_config() -> Result> { +pub fn load() -> Result> { let matches = args().get_matches(); let config: Result>; if matches.is_present("run") { @@ -52,13 +58,9 @@ pub fn load_config() -> Result> { fn runner_config(m: ArgMatches) -> Result> { if let Some(run) = m.subcommand_matches("run") { - if let Some(value) = run.value_of("config") { - let config = AppConfig::new(value)?; - Ok(Config::Main(config)) - } else { - let msg = format!("Failed to load configuration"); - Err(From::from(msg)) - } + let value = run.value_of("config").unwrap(); + let config = AppConfig::new(value)?; + Ok(Config::Main(config)) } else { let msg = format!("Failed to read arguments for 'run' subcommand"); Err(From::from(msg)) @@ -67,26 +69,46 @@ fn runner_config(m: ArgMatches) -> Result> { fn init_config(m: ArgMatches) -> Result> { if let Some(new) = m.subcommand_matches("new") { - if let Some(values) = new.values_of("topics") { - let config = BuildConfig::new(values)?; - Ok(Config::New(config)) - } else { - let msg = format!("Failed to load configuration"); - Err(From::from(msg)) - } + let username = new.value_of("username").unwrap(); + let topic_values = new.values_of("topics").unwrap(); + let config = BuildConfig::new(&username, topic_values)?; + Ok(Config::New(config)) } else { let msg = format!("Failed to read arguments for 'new' subcommand"); Err(From::from(msg)) } } -type Dict = HashMap; +#[derive(Debug, Deserialize)] +pub struct Blog { + name: String, + author: String, + topics: Vec, +} + +#[derive(Debug, Deserialize)] +pub struct Credentials { + user: String, + password: String, +} + +#[derive(Debug, Deserialize)] +pub struct DocPaths { + templates: String, + webroot: String, +} + +#[derive(Debug, Deserialize)] +pub struct LogConfig { + level: String +} #[derive(Debug, Deserialize)] pub struct AppConfig { - creds: Dict, - logging: Dict, - docpaths: Dict, + blog: Blog, + creds: Credentials, + logging: LogConfig, + docpaths: DocPaths, } impl AppConfig { @@ -100,13 +122,20 @@ impl AppConfig { #[derive(Debug, Deserialize)] pub struct BuildConfig { + username: String, topics: Vec } impl BuildConfig { - pub fn new(values: Values) -> Result> { - let topics: Vec = values.map(|x| String::from(x)).collect(); - Ok(BuildConfig { topics } ) + pub fn new(username: &str, topic_values: Values) -> Result> { + let username = username.to_string(); + let topics: Vec = topic_values.map(|x| String::from(x)).collect(); + Ok(BuildConfig { username, topics } ) + } + + pub fn to_app_config(&self, src: R) -> Result> { + + Err(From::from("just working on bits")) } } @@ -125,11 +154,30 @@ mod tests{ #[test] fn build_new_config() { - let arg_vec = vec!["caty-blog", "new", "-t", "yoga", "cooking"]; + let arg_vec = vec!["caty-blog", "new", "-u", "caty", + "-t", "yoga", "cooking"]; let matches = args().get_matches_from(arg_vec); let config = init_config(matches); assert!(config.is_ok()); let config = config.unwrap(); assert!(matches!(config, Config::New(_))); } + + #[test] + // Fix this test to provide input for each "prompt" for information + fn generate_blog_config() { + let src: &[u8] = b"MagicPassword"; + let arg_vec = vec!["caty-blog", "new", "-u", "caty", + "-t", "yoga", "cooking"]; + let matches = args().get_matches_from(arg_vec); + if let Ok(build_config) = init_config(matches) { + match build_config { + Config::New(bc) => { + assert!(bc.to_app_config(src).is_ok()) + }, + _ => () + } + + } + } } diff --git a/test_files/test-config.toml b/test_files/test-config.toml index 799051d..0d68b46 100644 --- a/test_files/test-config.toml +++ b/test_files/test-config.toml @@ -1,5 +1,10 @@ +[blog] +name = "My Awesome Blog!" +author = "Neo" +topics = ["one", "two", "three"] + [creds] -user = "caty" +user = "admin" password = "123456" [logging] @@ -7,4 +12,4 @@ level = "INFO" [docpaths] templates = "/var/www/blog/templates" -topics = "/var/www/blog/topics" \ No newline at end of file +webroot = "/var/www/blog/webroot" From d5c21318f7332d5c1739880721417f39c2455112 Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Mon, 29 Mar 2021 20:27:01 +0200 Subject: [PATCH 05/13] Generate AppConfig from user input - Add auth::generate_alphanum_password - Use above when generating AppConfig from user input - Add TODO doc comments to public interface --- Cargo.lock | 1 + Cargo.toml | 1 + src/auth.rs | 17 +++- src/config.rs | 224 ++++++++++++++++++++++++++++++++------------------ src/lib.rs | 1 + 5 files changed, 163 insertions(+), 81 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a84481..f82726a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,6 +120,7 @@ version = "0.1.0" dependencies = [ "clap", "pulldown-cmark", + "rand 0.8.3", "serde", "tera", "tokio", diff --git a/Cargo.toml b/Cargo.toml index 219a002..abde6fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" clap = "2.33.3" tera = "1.7.0" pulldown-cmark = { version = "0.8", default-features = false, features = ["simd"] } +rand = "0.8" serde = { version = "1", features = ["derive"] } tokio = { version = "1", features = ["full"] } toml = "0.5" diff --git a/src/auth.rs b/src/auth.rs index 2f502f0..6d407c3 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1 +1,16 @@ -// Placeholder for authentication +/// TODO Document +pub fn generate_alphanum_password(len: usize) -> Result> { + use rand::{thread_rng, Rng, + distributions::Alphanumeric}; + + if len < 32 { + Err(From::from("Random passwords shorter than 32ch are useless")) + } else { + let pass: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(len) + .map(char::from) + .collect(); + Ok(pass) + } +} diff --git a/src/config.rs b/src/config.rs index 0b9b40c..3f855fc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,16 +1,12 @@ -use std::io::prelude::*; +use std::{io::BufRead, usize}; use std::path::Path; -use clap::{App, AppSettings, Arg, ArgMatches, SubCommand, Values}; +use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; + use serde::Deserialize; use toml; - -#[derive(Debug)] -pub enum Config { - Main(AppConfig), - New(BuildConfig), -} +use crate::auth; fn args() -> App<'static, 'static> { App::new("Caty's Blog") @@ -26,28 +22,18 @@ fn args() -> App<'static, 'static> { .index(1))) .subcommand(SubCommand::with_name("new") .about("Generates a base directory structure and configuration file for a new blog") - .arg(Arg::with_name("username") - .help("Provides the username for the blog's admin") - .required(true) - .takes_value(true) - .short("u") - .long("user")) - .arg(Arg::with_name("topics") - .help("Provides the list of topics for the generator.") - .required(true) - .short("t") - .long("topic") - .multiple(true) - .min_values(1))) + ) } -pub fn load() -> Result> { +/// TODO Document this public function +/// And Include an Example of its Use +pub fn load() -> Result> { let matches = args().get_matches(); - let config: Result>; + let config: Result>; if matches.is_present("run") { config = runner_config(matches); } else if matches.is_present("new") { - config = init_config(matches); + config = init_config(); } else { let msg = format!("Unable to load configuration"); config = Err(From::from(msg)); @@ -56,54 +42,54 @@ pub fn load() -> Result> { config } -fn runner_config(m: ArgMatches) -> Result> { +fn runner_config(m: ArgMatches) -> Result> { if let Some(run) = m.subcommand_matches("run") { let value = run.value_of("config").unwrap(); let config = AppConfig::new(value)?; - Ok(Config::Main(config)) + Ok(config) } else { let msg = format!("Failed to read arguments for 'run' subcommand"); Err(From::from(msg)) } } -fn init_config(m: ArgMatches) -> Result> { - if let Some(new) = m.subcommand_matches("new") { - let username = new.value_of("username").unwrap(); - let topic_values = new.values_of("topics").unwrap(); - let config = BuildConfig::new(&username, topic_values)?; - Ok(Config::New(config)) - } else { - let msg = format!("Failed to read arguments for 'new' subcommand"); - Err(From::from(msg)) - } +fn init_config() -> Result> { + let reader = std::io::stdin(); + let mut reader = reader.lock(); + let config = AppConfig::generate(&mut reader)?; + Ok(config) } -#[derive(Debug, Deserialize)] +/// TODO Document +#[derive(Debug, Deserialize, PartialEq)] pub struct Blog { name: String, author: String, topics: Vec, } -#[derive(Debug, Deserialize)] +/// TODO Document +#[derive(Debug, Deserialize, PartialEq)] pub struct Credentials { user: String, password: String, } -#[derive(Debug, Deserialize)] +/// TODO Document +#[derive(Debug, Deserialize, PartialEq)] pub struct DocPaths { templates: String, webroot: String, } -#[derive(Debug, Deserialize)] +/// TODO Document +#[derive(Debug, Deserialize, PartialEq)] pub struct LogConfig { level: String } -#[derive(Debug, Deserialize)] +/// TODO Document +#[derive(Debug, Deserialize, PartialEq)] pub struct AppConfig { blog: Blog, creds: Credentials, @@ -112,72 +98,150 @@ pub struct AppConfig { } impl AppConfig { - pub fn new(config: T) -> Result> + fn new(config: T) -> Result> where T: AsRef { let config = std::fs::read_to_string(config)?; let app_config: AppConfig = toml::from_str(&config)?; Ok(app_config) } -} -#[derive(Debug, Deserialize)] -pub struct BuildConfig { - username: String, - topics: Vec -} + fn generate(reader: &mut R) -> Result> { + let current_path = std::env::current_dir()?; + let current_path = current_path.display(); -impl BuildConfig { - pub fn new(username: &str, topic_values: Values) -> Result> { - let username = username.to_string(); - let topics: Vec = topic_values.map(|x| String::from(x)).collect(); - Ok(BuildConfig { username, topics } ) - } + let name = Self::get_input("Please enter a name for the blog: ", reader)?; + let author = Self::get_input("Please enter the blog author's name: ", reader)?; + let topics = Self::get_input("Please enter comma-separated blog topics: ", reader)?; + let topics: Vec = topics.split(",") + .map(|s| s + .trim_start_matches(char::is_whitespace) + .trim_end_matches(char::is_whitespace) + .to_string()) + .collect(); + let blog = Blog { name, author, topics }; + + let user = Self::get_input("Please enter an username for the blog admin: ", reader)?; + + const PASSWORD_LEN: usize = 32; + let password = auth::generate_alphanum_password(PASSWORD_LEN)?; + println!("Save this random password for your admin: {}", password); + + let creds = Credentials { user, password }; - pub fn to_app_config(&self, src: R) -> Result> { + let templates = format!("{}/{}/templates", current_path, "blog"); + let webroot = format!("{}/{}/webroot", current_path, "blog"); + let docpaths = DocPaths { templates, webroot }; + + let level = format!("INFO"); + let logging = LogConfig { level }; - Err(From::from("just working on bits")) + let config = AppConfig { + blog, + creds, + logging, + docpaths, + }; + + Ok(config) + } + + fn get_input(prompt: &str, reader: &mut R) -> Result> { + let mut buf = String::new(); + println!("{}", prompt); + reader.read_line(&mut buf)?; + let buf = String::from(buf + .trim_start_matches(char::is_whitespace) + .trim_end_matches(char::is_whitespace)); + Ok(buf) } } #[cfg(test)] mod tests{ use super::*; + #[test] fn build_run_config() { let arg_vec = vec!["caty-blog", "run", "./test_files/test-config.toml"]; let matches = args().get_matches_from(arg_vec); let config = runner_config(matches); assert!(config.is_ok()); - let config = config.unwrap(); - assert!(matches!(config, Config::Main(_))); - } - - #[test] - fn build_new_config() { - let arg_vec = vec!["caty-blog", "new", "-u", "caty", - "-t", "yoga", "cooking"]; - let matches = args().get_matches_from(arg_vec); - let config = init_config(matches); - assert!(config.is_ok()); - let config = config.unwrap(); - assert!(matches!(config, Config::New(_))); } #[test] - // Fix this test to provide input for each "prompt" for information fn generate_blog_config() { - let src: &[u8] = b"MagicPassword"; - let arg_vec = vec!["caty-blog", "new", "-u", "caty", - "-t", "yoga", "cooking"]; + // Setup all target fields + let name = format!("Blog Name"); + let author = format!("Author Name"); + let topics: Vec = vec![format!("One"), format!("Two"), format!("Three"), format!("And More")]; + let blog = Blog { name, author, topics }; + + let user = format!("admin"); + let password = format!("MagicPassword"); + let creds = Credentials { user, password }; + + let current_path = std::env::current_dir().unwrap(); + let current_path = current_path.display(); + let templates = format!("{}/blog/templates", current_path); + let webroot = format!("{}/blog/webroot", current_path); + let docpaths = DocPaths { templates, webroot }; + + let level = format!("INFO"); + let logging = LogConfig { level }; + + let reference_config = AppConfig { + blog, + creds, + logging, + docpaths, + }; + + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\nMagicPassword\nINFO\n"; + let arg_vec = vec!["caty-blog", "new"]; let matches = args().get_matches_from(arg_vec); - if let Ok(build_config) = init_config(matches) { - match build_config { - Config::New(bc) => { - assert!(bc.to_app_config(src).is_ok()) - }, - _ => () + if matches.is_present("new") { + if let Ok(config) = generate(&mut src) { + assert_eq!(reference_config, config) + } else { + panic!("Failed to generate the reference config") } - } } + + // Generate function locally for testing AppConfig::get_input + fn generate(reader: &mut R) -> Result> { + let current_path = std::env::current_dir()?; + let current_path = current_path.display(); + + let name = AppConfig::get_input("Please enter a name for the blog: ", reader)?; + let author = AppConfig::get_input("Please enter the blog author's name: ", reader)?; + let topics = AppConfig::get_input("Please enter comma-separated blog topics: ", reader)?; + let topics: Vec = topics.split(",") + .map(|s| s + .trim_start_matches(char::is_whitespace) + .trim_end_matches(char::is_whitespace) + .to_string()) + .collect(); + let blog = Blog { name, author, topics }; + + let user = AppConfig::get_input("Please enter an username for the blog admin: ", reader)?; + let password = AppConfig::get_input("Please enter a password for the blog admin: ", reader)?; + let creds = Credentials { user, password }; + + let templates = format!("{}/{}/templates", current_path, "blog"); + let webroot = format!("{}/{}/webroot", current_path, "blog"); + let docpaths = DocPaths { templates, webroot }; + + let level = format!("INFO"); + let logging = LogConfig { level }; + + let config = AppConfig { + blog, + creds, + logging, + docpaths, + }; + + Ok(config) + } } diff --git a/src/lib.rs b/src/lib.rs index ef68c36..c73c20d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ +pub mod auth; pub mod config; From fb2b39aeb2a328769235e339925df995f3dd4b9e Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Tue, 30 Mar 2021 18:47:04 +0200 Subject: [PATCH 06/13] Refactor again for a more logical AppConfig::generate() method - Write password to read-only file rather than stdout closing - Creates the directory structure, may still refactor more - Fixes #1 --- Cargo.lock | 7 ++ Cargo.toml | 1 + src/auth.rs | 39 +++++++++++- src/config.rs | 173 +++++++++++++++++++++++--------------------------- 4 files changed, 124 insertions(+), 96 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f82726a..4b9dc1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -119,6 +119,7 @@ name = "caty_blog" version = "0.1.0" dependencies = [ "clap", + "data-encoding", "pulldown-cmark", "rand 0.8.3", "serde", @@ -189,6 +190,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "data-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57" + [[package]] name = "deunicode" version = "0.4.3" diff --git a/Cargo.toml b/Cargo.toml index abde6fa..896e52e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] clap = "2.33.3" +data-encoding = "2.3.2" tera = "1.7.0" pulldown-cmark = { version = "0.8", default-features = false, features = ["simd"] } rand = "0.8" diff --git a/src/auth.rs b/src/auth.rs index 6d407c3..ff1c4f1 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,5 +1,7 @@ +use std::io::prelude::*; + /// TODO Document -pub fn generate_alphanum_password(len: usize) -> Result> { +pub fn generate_secret(len: usize) -> Result> { use rand::{thread_rng, Rng, distributions::Alphanumeric}; @@ -14,3 +16,38 @@ pub fn generate_alphanum_password(len: usize) -> Result(secret: &str, dest: &mut T) -> std::io::Result<()> { + dest.write_all(secret.as_bytes()) +} + +pub use data_encoding::BASE32_NOPAD; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn check_secret_len() { + const SECLEN: usize = 32; + let secret = generate_secret(SECLEN).unwrap(); + assert_eq!(SECLEN, secret.len()) + } + + #[test] + fn check_short_secret() { + const SECLEN: usize = 12; + let secret = generate_secret(SECLEN); + assert!(secret.is_err()) + } + + #[test] + fn check_write_secret() { + let mut writer: Vec = vec![]; + const SECLEN: usize = 32; + let secret = generate_secret(SECLEN).unwrap(); + write_secret(&secret, &mut writer).unwrap(); + assert_eq!(SECLEN, writer.len()) + } +} diff --git a/src/config.rs b/src/config.rs index 3f855fc..e0107cc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,4 @@ +use std::fs::{create_dir_all, File, OpenOptions}; use std::{io::BufRead, usize}; use std::path::Path; @@ -33,7 +34,9 @@ pub fn load() -> Result> { if matches.is_present("run") { config = runner_config(matches); } else if matches.is_present("new") { - config = init_config(); + let reader = std::io::stdin(); + let mut reader = reader.lock(); + config = AppConfig::generate(&mut reader); } else { let msg = format!("Unable to load configuration"); config = Err(From::from(msg)); @@ -53,11 +56,25 @@ fn runner_config(m: ArgMatches) -> Result> } } -fn init_config() -> Result> { - let reader = std::io::stdin(); - let mut reader = reader.lock(); - let config = AppConfig::generate(&mut reader)?; - Ok(config) +fn get_input(prompt: &str, reader: &mut R) -> Result> { + let mut buf = String::new(); + println!("{}", prompt); + reader.read_line(&mut buf)?; + let buf = String::from(buf + .trim_start_matches(char::is_whitespace) + .trim_end_matches(char::is_whitespace)); + Ok(buf) +} + +fn topics_to_vec(topics: &str) -> Vec { + let topics: Vec = topics.split(",") + .map(|s| s + .trim_start_matches(char::is_whitespace) + .trim_end_matches(char::is_whitespace) + .to_string()) + .collect(); + + topics } /// TODO Document @@ -82,6 +99,18 @@ pub struct DocPaths { webroot: String, } +impl DocPaths { + fn create_paths(&self, blog: &Blog) -> Result<(), Box> { + create_dir_all(&self.templates)?; + create_dir_all(format!("{}/static/ext", &self.webroot))?; + for topic in &blog.topics { + create_dir_all(format!("{}/{}/ext", &self.webroot, &topic))?; + create_dir_all(format!("{}/{}/posts", &self.webroot, &topic))?; + } + Ok(()) + } +} + /// TODO Document #[derive(Debug, Deserialize, PartialEq)] pub struct LogConfig { @@ -109,28 +138,43 @@ impl AppConfig { let current_path = std::env::current_dir()?; let current_path = current_path.display(); - let name = Self::get_input("Please enter a name for the blog: ", reader)?; - let author = Self::get_input("Please enter the blog author's name: ", reader)?; - let topics = Self::get_input("Please enter comma-separated blog topics: ", reader)?; - let topics: Vec = topics.split(",") - .map(|s| s - .trim_start_matches(char::is_whitespace) - .trim_end_matches(char::is_whitespace) - .to_string()) - .collect(); + let templates = format!("{}/blog/templates", current_path); + let webroot = format!("{}/blog/webroot", current_path); + let docpaths = DocPaths { templates, webroot }; + + let name = get_input("Please enter a name for the blog: ", reader)?; + let author = get_input("Please enter the blog author's name: ", reader)?; + let topics = get_input("Please enter comma-separated blog topics: ", reader)?; + let topics = topics_to_vec(&topics); let blog = Blog { name, author, topics }; - let user = Self::get_input("Please enter an username for the blog admin: ", reader)?; + docpaths.create_paths(&blog)?; + let user = get_input("Please enter an username for the blog admin: ", reader)?; const PASSWORD_LEN: usize = 32; - let password = auth::generate_alphanum_password(PASSWORD_LEN)?; - println!("Save this random password for your admin: {}", password); + let password = auth::generate_secret(PASSWORD_LEN)?; + let password_file = format!("{}/blog/{}.pass", current_path, user); + let password_file = Path::new(&password_file); + if cfg!(unix) { + use std::os::unix::fs::OpenOptionsExt; + let mut options = OpenOptions::new(); + options.create(true); + options.write(true); + options.mode(0o600); + let mut password_file = options.open(password_file)?; + auth::write_secret(&password, &mut password_file)?; + } else { + let mut password_file = File::create(&password_file)?; + auth::write_secret(&password, &mut password_file)?; + let metadata = password_file.metadata()?; + let mut perms = metadata.permissions(); + perms.set_readonly(true); + } + + println!("Password generated and saved at: {}", password_file.to_path_buf().display()); let creds = Credentials { user, password }; - let templates = format!("{}/{}/templates", current_path, "blog"); - let webroot = format!("{}/{}/webroot", current_path, "blog"); - let docpaths = DocPaths { templates, webroot }; let level = format!("INFO"); let logging = LogConfig { level }; @@ -144,20 +188,10 @@ impl AppConfig { Ok(config) } - - fn get_input(prompt: &str, reader: &mut R) -> Result> { - let mut buf = String::new(); - println!("{}", prompt); - reader.read_line(&mut buf)?; - let buf = String::from(buf - .trim_start_matches(char::is_whitespace) - .trim_end_matches(char::is_whitespace)); - Ok(buf) - } } #[cfg(test)] -mod tests{ +mod tests { use super::*; #[test] @@ -169,79 +203,28 @@ mod tests{ } #[test] - fn generate_blog_config() { + fn get_user_input() { // Setup all target fields let name = format!("Blog Name"); let author = format!("Author Name"); - let topics: Vec = vec![format!("One"), format!("Two"), format!("Three"), format!("And More")]; - let blog = Blog { name, author, topics }; - + let topics = format!("One, Two, Three, And More"); let user = format!("admin"); let password = format!("MagicPassword"); - let creds = Credentials { user, password }; - - let current_path = std::env::current_dir().unwrap(); - let current_path = current_path.display(); - let templates = format!("{}/blog/templates", current_path); - let webroot = format!("{}/blog/webroot", current_path); - let docpaths = DocPaths { templates, webroot }; - let level = format!("INFO"); - let logging = LogConfig { level }; - - let reference_config = AppConfig { - blog, - creds, - logging, - docpaths, - }; + let reference_strings = vec![name, author, topics, user, password, level]; let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\nMagicPassword\nINFO\n"; - let arg_vec = vec!["caty-blog", "new"]; - let matches = args().get_matches_from(arg_vec); - if matches.is_present("new") { - if let Ok(config) = generate(&mut src) { - assert_eq!(reference_config, config) - } else { - panic!("Failed to generate the reference config") - } - } - } - - // Generate function locally for testing AppConfig::get_input - fn generate(reader: &mut R) -> Result> { - let current_path = std::env::current_dir()?; - let current_path = current_path.display(); - - let name = AppConfig::get_input("Please enter a name for the blog: ", reader)?; - let author = AppConfig::get_input("Please enter the blog author's name: ", reader)?; - let topics = AppConfig::get_input("Please enter comma-separated blog topics: ", reader)?; - let topics: Vec = topics.split(",") - .map(|s| s - .trim_start_matches(char::is_whitespace) - .trim_end_matches(char::is_whitespace) - .to_string()) - .collect(); - let blog = Blog { name, author, topics }; - - let user = AppConfig::get_input("Please enter an username for the blog admin: ", reader)?; - let password = AppConfig::get_input("Please enter a password for the blog admin: ", reader)?; - let creds = Credentials { user, password }; - - let templates = format!("{}/{}/templates", current_path, "blog"); - let webroot = format!("{}/{}/webroot", current_path, "blog"); - let docpaths = DocPaths { templates, webroot }; - let level = format!("INFO"); - let logging = LogConfig { level }; + for field in reference_strings { + assert_eq!(field, get_input(&field, &mut src).unwrap()) + } - let config = AppConfig { - blog, - creds, - logging, - docpaths, - }; + } - Ok(config) + #[test] + fn handle_csv_topics() { + let reference_topics: Vec = vec![format!("One"), format!("Two"), format!("Three"), format!("And More")]; + let topics = format!("One, Two, Three, And More"); + assert_eq!(reference_topics, topics_to_vec(&topics)) } } From f3ab948253c4dd04e11183d36ad77fc5281e7e74 Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Wed, 31 Mar 2021 18:08:58 +0200 Subject: [PATCH 07/13] Add dependencies for logging, testing, pw hashing - Closes #4 - Closes #6 - Improves test coverage - Adds password hashing - Adds totp token generation --- Cargo.lock | 77 ++++++++++++++++ Cargo.toml | 8 +- src/auth.rs | 49 +++++++++-- src/config.rs | 169 +++++++++++++++++++++--------------- test_files/test-config.toml | 1 + 5 files changed, 228 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4b9dc1a..6f2fa7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,6 +18,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "argon2" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ee15f9f5e0f846cab0aa13d5dd1edbc49331dcae95a2e43b84ccc0b406dcda4" +dependencies = [ + "blake2", + "password-hash", +] + [[package]] name = "atty" version = "0.2.14" @@ -41,12 +51,29 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "base64ct" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0d27fb6b6f1e43147af148af49d49329413ba781aa0d5e10979831c210173b5" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blake2" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a5720225ef5daecf08657f23791354e1685a8c91a4c60c7f3d3b2892f978f4" +dependencies = [ + "crypto-mac", + "digest 0.9.0", + "opaque-debug 0.3.0", +] + [[package]] name = "block-buffer" version = "0.7.3" @@ -118,11 +145,15 @@ checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" name = "caty_blog" version = "0.1.0" dependencies = [ + "argon2", "clap", "data-encoding", + "log", "pulldown-cmark", "rand 0.8.3", "serde", + "simplelog", + "tempfile", "tera", "tokio", "toml", @@ -190,6 +221,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crypto-mac" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" +dependencies = [ + "generic-array 0.14.4", + "subtle", +] + [[package]] name = "data-encoding" version = "2.3.2" @@ -750,6 +791,16 @@ dependencies = [ "regex", ] +[[package]] +name = "password-hash" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85d8faea6c018131952a192ee55bd9394c51fc6f63294b668d97636e6f842d40" +dependencies = [ + "base64ct", + "rand_core 0.6.2", +] + [[package]] name = "percent-encoding" version = "2.1.0" @@ -1098,6 +1149,17 @@ dependencies = [ "libc", ] +[[package]] +name = "simplelog" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59d0fe306a0ced1c88a58042dc22fc2ddd000982c26d75f6aa09a394547c41e0" +dependencies = [ + "chrono", + "log", + "termcolor", +] + [[package]] name = "slab" version = "0.4.2" @@ -1136,6 +1198,12 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "subtle" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" + [[package]] name = "syn" version = "1.0.64" @@ -1183,6 +1251,15 @@ dependencies = [ "unic-segment", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + [[package]] name = "textwrap" version = "0.11.0" diff --git a/Cargo.toml b/Cargo.toml index 896e52e..8cc7bb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,16 +7,22 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +argon2 = "0.1" clap = "2.33.3" data-encoding = "2.3.2" -tera = "1.7.0" +log = "0.4.14" pulldown-cmark = { version = "0.8", default-features = false, features = ["simd"] } rand = "0.8" serde = { version = "1", features = ["derive"] } +simplelog = "0.10.0" +tera = "1.7.0" tokio = { version = "1", features = ["full"] } toml = "0.5" warp = "0.3" +[dev-dependencies] +tempfile = "3" + [profile.release] panic = "abort" lto = true diff --git a/src/auth.rs b/src/auth.rs index ff1c4f1..c8b9a81 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,4 +1,7 @@ +use std::fs::OpenOptions; use std::io::prelude::*; +use std::path::Path; + /// TODO Document pub fn generate_secret(len: usize) -> Result> { @@ -18,10 +21,45 @@ pub fn generate_secret(len: usize) -> Result> } /// TODO Document -pub fn write_secret(secret: &str, dest: &mut T) -> std::io::Result<()> { - dest.write_all(secret.as_bytes()) +pub fn generate_argon2_phc(secret: &str) -> Result> { + use rand::rngs::OsRng; + use argon2::{Argon2, password_hash::{SaltString, PasswordHasher}}; + + let secret = secret.as_bytes(); + let salt = SaltString::generate(&mut OsRng); + let argon2 = Argon2::default(); + let argon2_phc: Result>; + if let Ok(phc) = argon2.hash_password_simple(secret, salt.as_ref()) { + argon2_phc = Ok(phc.to_string()); + } else { + argon2_phc = Err(From::from("Failed to hash password")); + } + + argon2_phc } +#[cfg(target_family = "unix")] +pub fn write_secret_file>(secret: &str, dest: P) -> std::io::Result<()> { + use std::os::unix::fs::OpenOptionsExt; + let mut options = OpenOptions::new(); + options.create(true); + options.write(true); + options.mode(0o600); + let mut secret_file = options.open(dest)?; + secret_file.write_all(secret.as_bytes()) +} + +#[cfg(target_family = "windows")] +pub fn write_secret_file>(secret: &str, dest: P) -> Result<(), Box> { + let mut secret_file = File::create(dest)?; + secret_file.write_all(secret.as_bytes())?; + let metadata = secret_file.metadata()?; + let mut perms = metadata.permissions(); + perms.set_readonly(true); + Ok(()) +} + + pub use data_encoding::BASE32_NOPAD; #[cfg(test)] @@ -43,11 +81,10 @@ mod tests { } #[test] - fn check_write_secret() { - let mut writer: Vec = vec![]; + fn check_argon2_hasher() { const SECLEN: usize = 32; let secret = generate_secret(SECLEN).unwrap(); - write_secret(&secret, &mut writer).unwrap(); - assert_eq!(SECLEN, writer.len()) + let phc = generate_argon2_phc(&secret); + assert!(phc.is_ok()) } } diff --git a/src/config.rs b/src/config.rs index e0107cc..b553151 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use std::fs::{create_dir_all, File, OpenOptions}; +use std::fs::create_dir_all; use std::{io::BufRead, usize}; use std::path::Path; @@ -36,7 +36,8 @@ pub fn load() -> Result> { } else if matches.is_present("new") { let reader = std::io::stdin(); let mut reader = reader.lock(); - config = AppConfig::generate(&mut reader); + let current_path = std::env::current_dir()?; + config = AppConfig::generate(current_path, &mut reader); } else { let msg = format!("Unable to load configuration"); config = Err(From::from(msg)); @@ -66,15 +67,15 @@ fn get_input(prompt: &str, reader: &mut R) -> Result Vec { - let topics: Vec = topics.split(",") +fn csv_to_vec(csv: &str) -> Vec { + let val_vec: Vec = csv.split(",") .map(|s| s .trim_start_matches(char::is_whitespace) .trim_end_matches(char::is_whitespace) .to_string()) .collect(); - topics + val_vec } /// TODO Document @@ -85,11 +86,46 @@ pub struct Blog { topics: Vec, } +impl Blog { + fn new_from_input(reader: &mut R) -> Result> { + let name = get_input("Please enter a name for the blog: ", reader)?; + let author = get_input("Please enter the blog author's name: ", reader)?; + let topics = get_input("Please enter comma-separated blog topics: ", reader)?; + let topics = csv_to_vec(&topics); + let blog = Blog { name, author, topics }; + + Ok(blog) + } +} + /// TODO Document #[derive(Debug, Deserialize, PartialEq)] pub struct Credentials { user: String, password: String, + token: String, +} + +impl Credentials { + fn new_from_input, R: BufRead>(dir: P, reader: &mut R) -> Result> { + let user = get_input("Please enter an username for the blog admin: ", reader)?; + const PASSWORD_LEN: usize = 32; + let password = auth::generate_secret(PASSWORD_LEN)?; + let password_file = dir.as_ref().join("admin.pass"); + auth::write_secret_file(&password, password_file)?; + let password = auth::generate_argon2_phc(&password)?; + + const TOKEN_LEN: usize = 34; + let token = auth::generate_secret(TOKEN_LEN)?; + let token = token.as_bytes(); + let token = auth::BASE32_NOPAD.encode(token); + let token_file = dir.as_ref().join("admin.totp"); + auth::write_secret_file(&token, token_file)?; + + let creds = Credentials { user, password, token }; + + Ok(creds) + } } /// TODO Document @@ -100,14 +136,13 @@ pub struct DocPaths { } impl DocPaths { - fn create_paths(&self, blog: &Blog) -> Result<(), Box> { - create_dir_all(&self.templates)?; - create_dir_all(format!("{}/static/ext", &self.webroot))?; - for topic in &blog.topics { - create_dir_all(format!("{}/{}/ext", &self.webroot, &topic))?; - create_dir_all(format!("{}/{}/posts", &self.webroot, &topic))?; - } - Ok(()) + fn new>(dir: P) -> Result> { + let dir = dir.as_ref().display(); + let templates = format!("{}/blog/templates", dir); + let webroot = format!("{}/blog/webroot", dir); + let docpaths = DocPaths { templates, webroot }; + + Ok(docpaths) } } @@ -134,48 +169,10 @@ impl AppConfig { Ok(app_config) } - fn generate(reader: &mut R) -> Result> { - let current_path = std::env::current_dir()?; - let current_path = current_path.display(); - - let templates = format!("{}/blog/templates", current_path); - let webroot = format!("{}/blog/webroot", current_path); - let docpaths = DocPaths { templates, webroot }; - - let name = get_input("Please enter a name for the blog: ", reader)?; - let author = get_input("Please enter the blog author's name: ", reader)?; - let topics = get_input("Please enter comma-separated blog topics: ", reader)?; - let topics = topics_to_vec(&topics); - let blog = Blog { name, author, topics }; - - docpaths.create_paths(&blog)?; - - let user = get_input("Please enter an username for the blog admin: ", reader)?; - const PASSWORD_LEN: usize = 32; - let password = auth::generate_secret(PASSWORD_LEN)?; - let password_file = format!("{}/blog/{}.pass", current_path, user); - let password_file = Path::new(&password_file); - if cfg!(unix) { - use std::os::unix::fs::OpenOptionsExt; - let mut options = OpenOptions::new(); - options.create(true); - options.write(true); - options.mode(0o600); - let mut password_file = options.open(password_file)?; - auth::write_secret(&password, &mut password_file)?; - } else { - let mut password_file = File::create(&password_file)?; - auth::write_secret(&password, &mut password_file)?; - let metadata = password_file.metadata()?; - let mut perms = metadata.permissions(); - perms.set_readonly(true); - } - - println!("Password generated and saved at: {}", password_file.to_path_buf().display()); - - let creds = Credentials { user, password }; - - + fn generate, R: BufRead>(dir: P, reader: &mut R) -> Result> { + let docpaths = DocPaths::new(&dir)?; + let blog = Blog::new_from_input(reader)?; + let creds = Credentials::new_from_input(&dir, reader)?; let level = format!("INFO"); let logging = LogConfig { level }; @@ -186,13 +183,33 @@ impl AppConfig { docpaths, }; + config.create_paths()?; + Ok(config) } + + fn create_paths(&self) -> Result<(), Box> { + create_dir_all(&self.docpaths.templates)?; + create_dir_all(format!("{}/static/ext", &self.docpaths.webroot))?; + create_dir_all(format!("{}/main/ext", &self.docpaths.webroot))?; + create_dir_all(format!("{}/main/posts", &self.docpaths.webroot))?; + + for topic in &self.blog.topics { + let topic = topic + .to_ascii_lowercase() + .replace(char::is_whitespace, "-"); + + create_dir_all(format!("{}/{}/ext", &self.docpaths.webroot, &topic))?; + create_dir_all(format!("{}/{}/posts", &self.docpaths.webroot, &topic))?; + } + Ok(()) + } } #[cfg(test)] mod tests { use super::*; + use tempfile; #[test] fn build_run_config() { @@ -203,28 +220,42 @@ mod tests { } #[test] - fn get_user_input() { + fn build_config_from_input() { + let dir = tempfile::tempdir().unwrap(); // Setup all target fields - let name = format!("Blog Name"); - let author = format!("Author Name"); - let topics = format!("One, Two, Three, And More"); - let user = format!("admin"); - let password = format!("MagicPassword"); - let level = format!("INFO"); - - let reference_strings = vec![name, author, topics, user, password, level]; - let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\nMagicPassword\nINFO\n"; + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let config = AppConfig::generate(&dir, &mut src); + assert!(config.is_ok()); - for field in reference_strings { - assert_eq!(field, get_input(&field, &mut src).unwrap()) + let tmp_dir = &dir.path(); + let admin = &tmp_dir.join("admin.pass"); + let token = &tmp_dir.join("admin.totp"); + let blog = &tmp_dir.join("blog"); + let templates = &tmp_dir.join("blog/templates"); + let webroot = &tmp_dir.join("blog/webroot"); + let static_ext = &tmp_dir.join("blog/webroot/static/ext"); + let main_ext = &tmp_dir.join("blog/webroot/main/ext"); + let main_posts = &tmp_dir.join("blog/webroot/main/posts"); + let one_ext = &tmp_dir.join("blog/webroot/one/ext"); + let one_posts = &tmp_dir.join("blog/webroot/one/posts"); + let two_ext = &tmp_dir.join("blog/webroot/two/ext"); + let two_posts = &tmp_dir.join("blog/webroot/two/posts"); + let three_ext = &tmp_dir.join("blog/webroot/three/ext"); + let three_posts = &tmp_dir.join("blog/webroot/three/posts"); + let and_more_ext = &tmp_dir.join("blog/webroot/and-more/ext"); + let and_more_posts = &tmp_dir.join("blog/webroot/and-more/posts"); + let core = vec![admin, token, blog, templates, webroot, static_ext, main_ext, main_posts, + one_ext, one_posts, two_ext, two_posts, three_ext, three_posts, and_more_ext, and_more_posts]; + for p in core { + assert!(Path::new(p).exists()) } - } #[test] fn handle_csv_topics() { let reference_topics: Vec = vec![format!("One"), format!("Two"), format!("Three"), format!("And More")]; let topics = format!("One, Two, Three, And More"); - assert_eq!(reference_topics, topics_to_vec(&topics)) + assert_eq!(reference_topics, csv_to_vec(&topics)) } + } diff --git a/test_files/test-config.toml b/test_files/test-config.toml index 0d68b46..4a34259 100644 --- a/test_files/test-config.toml +++ b/test_files/test-config.toml @@ -6,6 +6,7 @@ topics = ["one", "two", "three"] [creds] user = "admin" password = "123456" +token = "my very secret token" [logging] level = "INFO" From 52404dc233a6e7b700309df9f11a04654418c36e Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Sat, 3 Apr 2021 18:49:41 +0200 Subject: [PATCH 08/13] Restructure mods, write config to disk on generation --- src/auth.rs | 27 --------------------- src/config.rs | 31 ++++++++++++++++-------- src/dirs.rs | 1 - src/io.rs | 31 ++++++++++++++++++++++++ src/lib.rs | 2 ++ src/server.rs | 57 ++------------------------------------------ src/server/render.rs | 11 +++++++++ src/server/routes.rs | 11 +++++++++ 8 files changed, 78 insertions(+), 93 deletions(-) delete mode 100644 src/dirs.rs create mode 100644 src/io.rs create mode 100644 src/server/render.rs create mode 100644 src/server/routes.rs diff --git a/src/auth.rs b/src/auth.rs index c8b9a81..7b871a6 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,8 +1,3 @@ -use std::fs::OpenOptions; -use std::io::prelude::*; -use std::path::Path; - - /// TODO Document pub fn generate_secret(len: usize) -> Result> { use rand::{thread_rng, Rng, @@ -38,28 +33,6 @@ pub fn generate_argon2_phc(secret: &str) -> Result>(secret: &str, dest: P) -> std::io::Result<()> { - use std::os::unix::fs::OpenOptionsExt; - let mut options = OpenOptions::new(); - options.create(true); - options.write(true); - options.mode(0o600); - let mut secret_file = options.open(dest)?; - secret_file.write_all(secret.as_bytes()) -} - -#[cfg(target_family = "windows")] -pub fn write_secret_file>(secret: &str, dest: P) -> Result<(), Box> { - let mut secret_file = File::create(dest)?; - secret_file.write_all(secret.as_bytes())?; - let metadata = secret_file.metadata()?; - let mut perms = metadata.permissions(); - perms.set_readonly(true); - Ok(()) -} - - pub use data_encoding::BASE32_NOPAD; #[cfg(test)] diff --git a/src/config.rs b/src/config.rs index b553151..67410a3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ use std::path::Path; use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; -use serde::Deserialize; +use serde::{Serialize, Deserialize}; use toml; use crate::auth; @@ -79,7 +79,7 @@ fn csv_to_vec(csv: &str) -> Vec { } /// TODO Document -#[derive(Debug, Deserialize, PartialEq)] +#[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct Blog { name: String, author: String, @@ -99,7 +99,7 @@ impl Blog { } /// TODO Document -#[derive(Debug, Deserialize, PartialEq)] +#[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct Credentials { user: String, password: String, @@ -112,7 +112,7 @@ impl Credentials { const PASSWORD_LEN: usize = 32; let password = auth::generate_secret(PASSWORD_LEN)?; let password_file = dir.as_ref().join("admin.pass"); - auth::write_secret_file(&password, password_file)?; + crate::io::str_to_ro_file(&password, password_file)?; let password = auth::generate_argon2_phc(&password)?; const TOKEN_LEN: usize = 34; @@ -120,7 +120,7 @@ impl Credentials { let token = token.as_bytes(); let token = auth::BASE32_NOPAD.encode(token); let token_file = dir.as_ref().join("admin.totp"); - auth::write_secret_file(&token, token_file)?; + crate::io::str_to_ro_file(&token, token_file)?; let creds = Credentials { user, password, token }; @@ -129,7 +129,7 @@ impl Credentials { } /// TODO Document -#[derive(Debug, Deserialize, PartialEq)] +#[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct DocPaths { templates: String, webroot: String, @@ -147,13 +147,13 @@ impl DocPaths { } /// TODO Document -#[derive(Debug, Deserialize, PartialEq)] +#[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct LogConfig { level: String } /// TODO Document -#[derive(Debug, Deserialize, PartialEq)] +#[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct AppConfig { blog: Blog, creds: Credentials, @@ -184,6 +184,7 @@ impl AppConfig { }; config.create_paths()?; + config.write(&dir)?; Ok(config) } @@ -204,6 +205,13 @@ impl AppConfig { } Ok(()) } + + fn write>(&self, dir: P) -> Result<(), Box> { + let config = toml::to_string_pretty(&self)?; + let conf_path = &dir.as_ref().join("config.toml"); + crate::io::str_to_ro_file(&config, &conf_path)?; + Ok(()) + } } #[cfg(test)] @@ -228,6 +236,7 @@ mod tests { assert!(config.is_ok()); let tmp_dir = &dir.path(); + let config_path = &tmp_dir.join("config.toml"); let admin = &tmp_dir.join("admin.pass"); let token = &tmp_dir.join("admin.totp"); let blog = &tmp_dir.join("blog"); @@ -244,8 +253,10 @@ mod tests { let three_posts = &tmp_dir.join("blog/webroot/three/posts"); let and_more_ext = &tmp_dir.join("blog/webroot/and-more/ext"); let and_more_posts = &tmp_dir.join("blog/webroot/and-more/posts"); - let core = vec![admin, token, blog, templates, webroot, static_ext, main_ext, main_posts, - one_ext, one_posts, two_ext, two_posts, three_ext, three_posts, and_more_ext, and_more_posts]; + let core = vec![config_path, admin, token, blog, templates, + webroot, static_ext, main_ext, main_posts, + one_ext, one_posts, two_ext, two_posts, + three_ext, three_posts, and_more_ext, and_more_posts]; for p in core { assert!(Path::new(p).exists()) } diff --git a/src/dirs.rs b/src/dirs.rs deleted file mode 100644 index 1aa36b5..0000000 --- a/src/dirs.rs +++ /dev/null @@ -1 +0,0 @@ -// Placeholder for the directory creation logic diff --git a/src/io.rs b/src/io.rs new file mode 100644 index 0000000..6581898 --- /dev/null +++ b/src/io.rs @@ -0,0 +1,31 @@ +use std::fs::OpenOptions; +use std::io::prelude::*; +use std::path::Path; + +#[cfg(target_family = "unix")] +pub fn str_to_ro_file>(content: &str, dest: P) -> Result<(), Box> { + use std::os::unix::fs::OpenOptionsExt; + let mut options = OpenOptions::new(); + options.create(true); + options.write(true); + options.mode(0o600); + let mut ro_file = options.open(dest)?; + ro_file.write_all(content.as_bytes())?; + if !content.ends_with("\n") { + ro_file.write(b"\n")?; + } + Ok(()) +} + +#[cfg(target_family = "windows")] +pub fn str_to_ro_file>(content: &str, dest: P) -> Result<(), Box> { + let mut ro_file = File::create(dest)?; + ro_file.write_all(content.as_bytes())?; + let metadata = secret_file.metadata()?; + let mut perms = metadata.permissions(); + if !content.ends_with("\n") { + ro_file.write(b"\n")?; + } + perms.set_readonly(true); + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index c73c20d..e4f8bc5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,4 @@ pub mod auth; pub mod config; +pub mod io; +pub mod server; diff --git a/src/server.rs b/src/server.rs index 47778e0..fe8f58c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,55 +1,2 @@ -use std::fs::File; -use std::io::prelude::*; -use std::path::PathBuf; -use pulldown_cmark::{Parser, html}; -use tera::{Tera, Context}; - -// Placeholder for Warp routes - -/* -pub fn read_to_html(paths: Vec) -> Vec { - let mut contents: Vec = Vec::new(); - for path in paths { - let buffer = std::fs::read_to_string(path).unwrap(); - let parser = Parser::new(&buffer); - let mut html_output = String::new(); - html::push_html(&mut html_output, parser); - contents.push(html_output); - } - contents -} - -pub fn load_templates(dir: &str) -> Tera { - match Tera::new(dir) { - Ok(t) => t, - Err(e) => panic!("Failed with {}", e) - } -} - -pub fn render_main() -> String { - let pattern = parse_args(); - let path_vec = path_matches(&pattern); - let mut data = read_to_html(path_vec); - data.reverse(); - - let tera = load_templates("templates/*.tmpl"); - let mut context = Context::new(); - - context.insert("posts", &data); - - let output = tera.render("main.tmpl", &context); - - if let Ok(output) = output { - output - } else { - String::new() - } -} - -pub fn write_main(rendered: &str) -> Result<(), Box> { - let buf = rendered.as_bytes(); - let mut f = File::create("webroot/index.html")?; - f.write_all(buf)?; - Ok(()) -} -*/ +pub mod routes; +pub mod render; diff --git a/src/server/render.rs b/src/server/render.rs new file mode 100644 index 0000000..e7c0468 --- /dev/null +++ b/src/server/render.rs @@ -0,0 +1,11 @@ +use std::fs::File; +use std::io::prelude::*; +use std::path::PathBuf; +use pulldown_cmark::{Parser, html}; +use tera::{Tera, Context}; + +// Placeholder for template and markdown rendering + +#[cfg(test)] +mod tests { +} diff --git a/src/server/routes.rs b/src/server/routes.rs new file mode 100644 index 0000000..d8b1834 --- /dev/null +++ b/src/server/routes.rs @@ -0,0 +1,11 @@ +use warp::{Filter, Reply, filters::BoxedFilter}; + +// Placeholder for Warp routes + +#[cfg(test)] +mod tests { + #[tokio::test] + async fn main_page() { + //let filter = + } +} From 55a07d2626f48d6228c4003ecb8ce8d52113c496 Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Sun, 4 Apr 2021 14:16:53 +0200 Subject: [PATCH 09/13] Make config struct fields public, start adding routes --- src/config.rs | 34 +++++++------- src/server/routes.rs | 103 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index 67410a3..db7128c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -81,13 +81,13 @@ fn csv_to_vec(csv: &str) -> Vec { /// TODO Document #[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct Blog { - name: String, - author: String, - topics: Vec, + pub name: String, + pub author: String, + pub topics: Vec, } impl Blog { - fn new_from_input(reader: &mut R) -> Result> { + pub fn new_from_input(reader: &mut R) -> Result> { let name = get_input("Please enter a name for the blog: ", reader)?; let author = get_input("Please enter the blog author's name: ", reader)?; let topics = get_input("Please enter comma-separated blog topics: ", reader)?; @@ -101,13 +101,13 @@ impl Blog { /// TODO Document #[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct Credentials { - user: String, - password: String, - token: String, + pub user: String, + pub password: String, + pub token: String, } impl Credentials { - fn new_from_input, R: BufRead>(dir: P, reader: &mut R) -> Result> { + pub fn new_from_input, R: BufRead>(dir: P, reader: &mut R) -> Result> { let user = get_input("Please enter an username for the blog admin: ", reader)?; const PASSWORD_LEN: usize = 32; let password = auth::generate_secret(PASSWORD_LEN)?; @@ -131,12 +131,12 @@ impl Credentials { /// TODO Document #[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct DocPaths { - templates: String, - webroot: String, + pub templates: String, + pub webroot: String, } impl DocPaths { - fn new>(dir: P) -> Result> { + pub fn new>(dir: P) -> Result> { let dir = dir.as_ref().display(); let templates = format!("{}/blog/templates", dir); let webroot = format!("{}/blog/webroot", dir); @@ -155,21 +155,21 @@ pub struct LogConfig { /// TODO Document #[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct AppConfig { - blog: Blog, - creds: Credentials, - logging: LogConfig, - docpaths: DocPaths, + pub blog: Blog, + pub creds: Credentials, + pub logging: LogConfig, + pub docpaths: DocPaths, } impl AppConfig { - fn new(config: T) -> Result> + pub fn new(config: T) -> Result> where T: AsRef { let config = std::fs::read_to_string(config)?; let app_config: AppConfig = toml::from_str(&config)?; Ok(app_config) } - fn generate, R: BufRead>(dir: P, reader: &mut R) -> Result> { + pub fn generate, R: BufRead>(dir: P, reader: &mut R) -> Result> { let docpaths = DocPaths::new(&dir)?; let blog = Blog::new_from_input(reader)?; let creds = Credentials::new_from_input(&dir, reader)?; diff --git a/src/server/routes.rs b/src/server/routes.rs index d8b1834..b6a8228 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -1,11 +1,112 @@ +use std::path::Path; +use std::sync::Arc; use warp::{Filter, Reply, filters::BoxedFilter}; +use crate::config::AppConfig; + // Placeholder for Warp routes +/* +fn root(app: Arc) -> BoxedFilter<(impl Reply,)> { + let app = &app.clone(); + let main_path = Path::new(&app.docpaths.webroot).join("main"); + + + +} +*/ + +fn static_assets(app: Arc) -> BoxedFilter<(impl Reply,)> { + let app = &app.clone(); + let static_path = Path::new(&app.docpaths.webroot).join("static"); + + warp::path("static") + .and(warp::fs::dir(static_path)) + .boxed() +} + +fn topic_assets(app: Arc, topic: &'static str) -> BoxedFilter<(impl Reply,)> { + let app = &app.clone(); + let topic_asset_path = Path::new(&app.docpaths.webroot).join(topic).join("ext"); + warp::path(topic) + .and(warp::path("ext")) + .and(warp::fs::dir(topic_asset_path)) + .boxed() +} #[cfg(test)] mod tests { + use super::*; + use crate::config::AppConfig; + use tempfile; + + /* #[tokio::test] async fn main_page() { - //let filter = + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let config = AppConfig::generate(&dir, &mut src).unwrap(); + let config = Arc::new(config); + let filter = root(config); + + assert!(!warp::test::request() + .path("/") + .matches(&filter) + .await); + + } + */ + + #[tokio::test] + async fn static_content() { + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let config = AppConfig::generate(&dir, &mut src).unwrap(); + let config = Arc::new(config); + let filter = static_assets(config); + + assert!(!warp::test::request() + .path("/static/style.css") + .matches(&filter) + .await); + + } + + /* + #[tokio::test] + async fn topic_page() { + let filter = topic(); + } + */ + + #[tokio::test] + async fn topic_content() { + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let config = AppConfig::generate(&dir, &mut src).unwrap(); + let config = Arc::new(config); + let filter = topic_assets(config.clone(), "one"); + let filter2 = topic_assets(config.clone(), "two"); + let filter3 = topic_assets(config.clone(), "three"); + let filter_and_more = topic_assets(config.clone(), "and-more"); + + assert!(!warp::test::request() + .path("/one/ext/example.png") + .matches(&filter) + .await); + + assert!(!warp::test::request() + .path("/two/ext/example.png") + .matches(&filter2) + .await); + + assert!(!warp::test::request() + .path("/three/ext/example.png") + .matches(&filter3) + .await); + + assert!(!warp::test::request() + .path("/and-more/ext/example.png") + .matches(&filter_and_more) + .await); } } From 7af4d8d42f9e17bc701e12f737488d1f689ff7e8 Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Sun, 4 Apr 2021 21:03:22 +0200 Subject: [PATCH 10/13] Added rendering, removed route module, renamed io module --- Cargo.lock | 7 +++ Cargo.toml | 1 + src/bin/caty-blog.rs | 7 ++- src/{io.rs => common.rs} | 23 +++++++- src/config.rs | 11 ++-- src/lib.rs | 4 +- src/render.rs | 110 ++++++++++++++++++++++++++++++++++++++ src/render/default.rs | 32 +++++++++++ src/server.rs | 2 - src/server/render.rs | 11 ---- src/server/routes.rs | 112 --------------------------------------- 11 files changed, 184 insertions(+), 136 deletions(-) rename src/{io.rs => common.rs} (65%) create mode 100644 src/render.rs create mode 100644 src/render/default.rs delete mode 100644 src/server.rs delete mode 100644 src/server/render.rs delete mode 100644 src/server/routes.rs diff --git a/Cargo.lock b/Cargo.lock index 6f2fa7e..0057bd4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -148,6 +148,7 @@ dependencies = [ "argon2", "clap", "data-encoding", + "glob", "log", "pulldown-cmark", "rand 0.8.3", @@ -386,6 +387,12 @@ dependencies = [ "wasi 0.10.2+wasi-snapshot-preview1", ] +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" + [[package]] name = "globset" version = "0.4.6" diff --git a/Cargo.toml b/Cargo.toml index 8cc7bb9..636caaf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" argon2 = "0.1" clap = "2.33.3" data-encoding = "2.3.2" +glob = "0.3.0" log = "0.4.14" pulldown-cmark = { version = "0.8", default-features = false, features = ["simd"] } rand = "0.8" diff --git a/src/bin/caty-blog.rs b/src/bin/caty-blog.rs index f55e064..a02a35d 100644 --- a/src/bin/caty-blog.rs +++ b/src/bin/caty-blog.rs @@ -1,9 +1,12 @@ +use std::sync::Arc; use caty_blog::*; +use warp::Filter; + #[tokio::main] async fn main() -> Result<(), Box> { let config = config::load()?; - println!("{:?}", config); + let app = Arc::new(config); // Configure logging @@ -11,6 +14,6 @@ async fn main() -> Result<(), Box> { // - Either generate the base directory structure for a blog // - Or load an existing site and serve its routes based on the loaded config // warp::serve(routes).run(([127,0,0,1], 3030)).await; - + Ok(()) } diff --git a/src/io.rs b/src/common.rs similarity index 65% rename from src/io.rs rename to src/common.rs index 6581898..7c79b45 100644 --- a/src/io.rs +++ b/src/common.rs @@ -1,6 +1,8 @@ use std::fs::OpenOptions; use std::io::prelude::*; -use std::path::Path; +use std::path::{Path, PathBuf}; + +use glob::glob; #[cfg(target_family = "unix")] pub fn str_to_ro_file>(content: &str, dest: P) -> Result<(), Box> { @@ -29,3 +31,22 @@ pub fn str_to_ro_file>(content: &str, dest: P) -> Result<(), Box< perms.set_readonly(true); Ok(()) } + +pub fn path_matches(pat: &str) -> Result, Box> { + let mut path_vec: Vec = Vec::new(); + let entries = glob(pat)?; + for entry in entries.filter_map(Result::ok) { + path_vec.push(entry); + } + + path_vec.reverse(); + Ok(path_vec) +} + +pub fn slugify(topic: &str) -> String { + let topic = topic + .to_ascii_lowercase() + .replace(char::is_whitespace, "-"); + topic +} + diff --git a/src/config.rs b/src/config.rs index db7128c..6007613 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,6 +8,7 @@ use serde::{Serialize, Deserialize}; use toml; use crate::auth; +use crate::common; fn args() -> App<'static, 'static> { App::new("Caty's Blog") @@ -112,7 +113,7 @@ impl Credentials { const PASSWORD_LEN: usize = 32; let password = auth::generate_secret(PASSWORD_LEN)?; let password_file = dir.as_ref().join("admin.pass"); - crate::io::str_to_ro_file(&password, password_file)?; + common::str_to_ro_file(&password, password_file)?; let password = auth::generate_argon2_phc(&password)?; const TOKEN_LEN: usize = 34; @@ -120,7 +121,7 @@ impl Credentials { let token = token.as_bytes(); let token = auth::BASE32_NOPAD.encode(token); let token_file = dir.as_ref().join("admin.totp"); - crate::io::str_to_ro_file(&token, token_file)?; + common::str_to_ro_file(&token, token_file)?; let creds = Credentials { user, password, token }; @@ -196,9 +197,7 @@ impl AppConfig { create_dir_all(format!("{}/main/posts", &self.docpaths.webroot))?; for topic in &self.blog.topics { - let topic = topic - .to_ascii_lowercase() - .replace(char::is_whitespace, "-"); + let topic = common::slugify(&topic); create_dir_all(format!("{}/{}/ext", &self.docpaths.webroot, &topic))?; create_dir_all(format!("{}/{}/posts", &self.docpaths.webroot, &topic))?; @@ -209,7 +208,7 @@ impl AppConfig { fn write>(&self, dir: P) -> Result<(), Box> { let config = toml::to_string_pretty(&self)?; let conf_path = &dir.as_ref().join("config.toml"); - crate::io::str_to_ro_file(&config, &conf_path)?; + common::str_to_ro_file(&config, &conf_path)?; Ok(()) } } diff --git a/src/lib.rs b/src/lib.rs index e4f8bc5..43daa5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ pub mod auth; pub mod config; -pub mod io; -pub mod server; +pub mod common; +pub mod render; diff --git a/src/render.rs b/src/render.rs new file mode 100644 index 0000000..019681f --- /dev/null +++ b/src/render.rs @@ -0,0 +1,110 @@ +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use crate::config::AppConfig; +use crate::common; + +pub use pulldown_cmark::{Parser, html}; +pub use tera::{Tera, Context}; + +mod default; + +pub struct Engine { + pub app: Arc, + pub topic_slug: String, + pub template: String, + pub instance: Tera +} + +impl Engine { + pub fn new(a: Arc, ts: &str, tmpl: &str, inst: Tera) -> Engine { + Engine { + app: a.clone(), + topic_slug: ts.to_string(), + template: tmpl.to_string(), + instance: inst + } + } +} + +pub fn load_default_template() -> Result> { + let mut tera = Tera::default(); + tera.add_raw_template("default.tmpl", default::TEMPLATE)?; + Ok(tera) +} + +pub fn render_topic(engine: Engine) -> Result> { + let blog = &engine.app.blog; + let topic_data = load_topic(&engine)?; + let mut context = Context::new(); + context.insert("blog", blog); + context.insert("posts", &topic_data); + let output = engine.instance.render(&engine.template, &context)?; + + Ok(output) +} + +fn load_topic(engine: &Engine) -> Result, Box> { + let topic_path = Path::new(&engine.app.docpaths.webroot).join(&engine.topic_slug); + let pat = format!("{}/*.md", topic_path.display()); + let paths = common::path_matches(&pat)?; + read_to_html(paths) +} + +fn read_to_html(paths: Vec) -> Result, Box> { + let mut contents: Vec = Vec::new(); + for path in paths { + let buf = std::fs::read_to_string(path)?; + let parser = Parser::new(&buf); + let mut html_output = String::new(); + html::push_html(&mut html_output, parser); + contents.push(html_output); + } + + Ok(contents) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::File; + use std::io::prelude::*; + use tempfile; + + #[test] + fn check_default_template() { + let tera = load_default_template(); + assert!(tera.is_ok()) + } + + #[test] + fn check_render_topic() { + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let instance = load_default_template().unwrap(); + let config = AppConfig::generate(&dir, &mut src).unwrap(); + let config = Arc::new(config); + let engine = Engine::new(config, "one", "default.tmpl", instance); + + let post = r#" +### Something + +Very cool, but maybe not super useful +"#; + let post2 = r#" +### Something Again + +Super Wow! +"#; + + let mut f = File::create(&dir.path().join("blog").join("webroot").join("one").join("post1.md")).unwrap(); + f.write_all(&post.as_bytes()).unwrap(); + + let mut f = File::create(&dir.path().join("blog").join("webroot").join("one").join("post2.md")).unwrap(); + f.write_all(&post2.as_bytes()).unwrap(); + + let page = render_topic(engine); + + assert!(page.is_ok()) + } +} diff --git a/src/render/default.rs b/src/render/default.rs new file mode 100644 index 0000000..6abc540 --- /dev/null +++ b/src/render/default.rs @@ -0,0 +1,32 @@ +pub const TEMPLATE: &str = r#" + + + + + + +{{ blog.name }} + + +
+
+

{{ blog.name }}

+ +
+
+
+{%- for post in posts %} +{{ post }} +{%- endfor -%} +
+
+

© {{ blog.author }}

+
+ + +"#; diff --git a/src/server.rs b/src/server.rs deleted file mode 100644 index fe8f58c..0000000 --- a/src/server.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod routes; -pub mod render; diff --git a/src/server/render.rs b/src/server/render.rs deleted file mode 100644 index e7c0468..0000000 --- a/src/server/render.rs +++ /dev/null @@ -1,11 +0,0 @@ -use std::fs::File; -use std::io::prelude::*; -use std::path::PathBuf; -use pulldown_cmark::{Parser, html}; -use tera::{Tera, Context}; - -// Placeholder for template and markdown rendering - -#[cfg(test)] -mod tests { -} diff --git a/src/server/routes.rs b/src/server/routes.rs deleted file mode 100644 index b6a8228..0000000 --- a/src/server/routes.rs +++ /dev/null @@ -1,112 +0,0 @@ -use std::path::Path; -use std::sync::Arc; -use warp::{Filter, Reply, filters::BoxedFilter}; - -use crate::config::AppConfig; - -// Placeholder for Warp routes -/* -fn root(app: Arc) -> BoxedFilter<(impl Reply,)> { - let app = &app.clone(); - let main_path = Path::new(&app.docpaths.webroot).join("main"); - - - -} -*/ - -fn static_assets(app: Arc) -> BoxedFilter<(impl Reply,)> { - let app = &app.clone(); - let static_path = Path::new(&app.docpaths.webroot).join("static"); - - warp::path("static") - .and(warp::fs::dir(static_path)) - .boxed() -} - -fn topic_assets(app: Arc, topic: &'static str) -> BoxedFilter<(impl Reply,)> { - let app = &app.clone(); - let topic_asset_path = Path::new(&app.docpaths.webroot).join(topic).join("ext"); - warp::path(topic) - .and(warp::path("ext")) - .and(warp::fs::dir(topic_asset_path)) - .boxed() -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::config::AppConfig; - use tempfile; - - /* - #[tokio::test] - async fn main_page() { - let dir = tempfile::tempdir().unwrap(); - let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; - let config = AppConfig::generate(&dir, &mut src).unwrap(); - let config = Arc::new(config); - let filter = root(config); - - assert!(!warp::test::request() - .path("/") - .matches(&filter) - .await); - - } - */ - - #[tokio::test] - async fn static_content() { - let dir = tempfile::tempdir().unwrap(); - let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; - let config = AppConfig::generate(&dir, &mut src).unwrap(); - let config = Arc::new(config); - let filter = static_assets(config); - - assert!(!warp::test::request() - .path("/static/style.css") - .matches(&filter) - .await); - - } - - /* - #[tokio::test] - async fn topic_page() { - let filter = topic(); - } - */ - - #[tokio::test] - async fn topic_content() { - let dir = tempfile::tempdir().unwrap(); - let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; - let config = AppConfig::generate(&dir, &mut src).unwrap(); - let config = Arc::new(config); - let filter = topic_assets(config.clone(), "one"); - let filter2 = topic_assets(config.clone(), "two"); - let filter3 = topic_assets(config.clone(), "three"); - let filter_and_more = topic_assets(config.clone(), "and-more"); - - assert!(!warp::test::request() - .path("/one/ext/example.png") - .matches(&filter) - .await); - - assert!(!warp::test::request() - .path("/two/ext/example.png") - .matches(&filter2) - .await); - - assert!(!warp::test::request() - .path("/three/ext/example.png") - .matches(&filter3) - .await); - - assert!(!warp::test::request() - .path("/and-more/ext/example.png") - .matches(&filter_and_more) - .await); - } -} From 9ea016dff50949a30bbd98ebf6f150602b091360 Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Mon, 5 Apr 2021 14:01:51 +0200 Subject: [PATCH 11/13] Update cargo, remove warp, branch for routes --- Cargo.lock | 689 ++----------------------------------------- Cargo.toml | 1 - src/bin/caty-blog.rs | 8 +- src/config.rs | 4 +- src/lib.rs | 1 + src/render.rs | 4 +- src/routes.rs | 105 +++++++ 7 files changed, 142 insertions(+), 670 deletions(-) create mode 100644 src/routes.rs diff --git a/Cargo.lock b/Cargo.lock index 0057bd4..3674bb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -45,12 +45,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" -[[package]] -name = "base64" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" - [[package]] name = "base64ct" version = "1.0.0" @@ -86,15 +80,6 @@ dependencies = [ "generic-array 0.12.4", ] -[[package]] -name = "block-buffer" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" -dependencies = [ - "generic-array 0.14.4", -] - [[package]] name = "block-padding" version = "0.1.5" @@ -113,16 +98,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "buf_redux" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b953a6887648bb07a535631f2bc00fbdb2a2216f135552cb3f534ed136b9c07f" -dependencies = [ - "memchr", - "safemem", -] - [[package]] name = "byte-tools" version = "0.3.1" @@ -151,14 +126,13 @@ dependencies = [ "glob", "log", "pulldown-cmark", - "rand 0.8.3", + "rand", "serde", "simplelog", "tempfile", "tera", "tokio", "toml", - "warp", ] [[package]] @@ -205,12 +179,6 @@ dependencies = [ "vec_map", ] -[[package]] -name = "cpuid-bool" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" - [[package]] name = "crossbeam-utils" version = "0.8.3" @@ -274,78 +242,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "form_urlencoded" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" -dependencies = [ - "matches", - "percent-encoding", -] - -[[package]] -name = "futures" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f55667319111d593ba876406af7c409c0ebb44dc4be6132a783ccf163ea14c1" -dependencies = [ - "futures-channel", - "futures-core", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-channel" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939" -dependencies = [ - "futures-core", - "futures-sink", -] - -[[package]] -name = "futures-core" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94" - -[[package]] -name = "futures-io" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71c2c65c57704c32f5241c1223167c2c3294fd34ac020c807ddbe6db287ba59" - -[[package]] -name = "futures-sink" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85754d98985841b7d4f5e8e6fbfa4a4ac847916893ec511a2917ccd8525b8bb3" - -[[package]] -name = "futures-task" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa189ef211c15ee602667a6fcfe1c1fd9e07d42250d2156382820fba33c9df80" - -[[package]] -name = "futures-util" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1" -dependencies = [ - "futures-core", - "futures-sink", - "futures-task", - "pin-project-lite", - "pin-utils", - "slab", -] - [[package]] name = "generic-array" version = "0.12.4" @@ -365,17 +261,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "getrandom" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" -dependencies = [ - "cfg-if", - "libc", - "wasi 0.9.0+wasi-snapshot-preview1", -] - [[package]] name = "getrandom" version = "0.2.2" @@ -384,7 +269,7 @@ checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ "cfg-if", "libc", - "wasi 0.10.2+wasi-snapshot-preview1", + "wasi", ] [[package]] @@ -417,56 +302,6 @@ dependencies = [ "walkdir", ] -[[package]] -name = "h2" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d832b01df74254fe364568d6ddc294443f61cbec82816b60904303af87efae78" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http", - "indexmap", - "slab", - "tokio", - "tokio-util", - "tracing", -] - -[[package]] -name = "hashbrown" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" - -[[package]] -name = "headers" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0b7591fb62902706ae8e7aaff416b1b0fa2c0fd0878b46dc13baa3712d8a855" -dependencies = [ - "base64", - "bitflags", - "bytes", - "headers-core", - "http", - "mime", - "sha-1 0.9.4", - "time", -] - -[[package]] -name = "headers-core" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" -dependencies = [ - "http", -] - [[package]] name = "hermit-abi" version = "0.1.18" @@ -476,81 +311,12 @@ dependencies = [ "libc", ] -[[package]] -name = "http" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7245cd7449cc792608c3c8a9eaf69bd4eabbabf802713748fd739c98b82f0747" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "http-body" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dfb77c123b4e2f72a2069aeae0b4b4949cc7e966df277813fc16347e7549737" -dependencies = [ - "bytes", - "http", - "pin-project-lite", -] - -[[package]] -name = "httparse" -version = "1.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691" - -[[package]] -name = "httpdate" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" - [[package]] name = "humansize" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" -[[package]] -name = "hyper" -version = "0.14.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8e946c2b1349055e0b72ae281b238baf1a3ea7307c7e9f9d64673bdd9c26ac7" -dependencies = [ - "bytes", - "futures-channel", - "futures-core", - "futures-util", - "h2", - "http", - "http-body", - "httparse", - "httpdate", - "itoa", - "pin-project", - "socket2", - "tokio", - "tower-service", - "tracing", - "want", -] - -[[package]] -name = "idna" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89829a5d69c23d348314a7ac337fe39173b61149a9864deabd260983aed48c21" -dependencies = [ - "matches", - "unicode-bidi", - "unicode-normalization", -] - [[package]] name = "ignore" version = "0.4.17" @@ -569,25 +335,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "indexmap" -version = "1.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" -dependencies = [ - "autocfg", - "hashbrown", -] - -[[package]] -name = "input_buffer" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f97967975f448f1a7ddb12b0bc41069d09ed6a1c161a92687e057325db35d413" -dependencies = [ - "bytes", -] - [[package]] name = "instant" version = "0.1.9" @@ -611,15 +358,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.90" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba4aede83fc3617411dc6993bc8c70919750c1c257c6ca6a502aed6e0e2394ae" +checksum = "56d855069fafbb9b344c0f962150cd2c1187975cb1c22c1522c240d8c4986714" [[package]] name = "lock_api" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312" +checksum = "5a3c91c24eae6777794bb1997ad98bbb87daf92890acab859f7eaa4320333176" dependencies = [ "scopeguard", ] @@ -639,39 +386,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" -[[package]] -name = "matches" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" - [[package]] name = "memchr" version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" -[[package]] -name = "mime" -version = "0.3.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" - -[[package]] -name = "mime_guess" -version = "2.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212" -dependencies = [ - "mime", - "unicase", -] - [[package]] name = "mio" -version = "0.7.10" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2182a122f3b7f3f5329cb1972cee089ba2459a0a80a56935e6e674f096f8d839" +checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956" dependencies = [ "libc", "log", @@ -682,32 +407,13 @@ dependencies = [ [[package]] name = "miow" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" dependencies = [ - "socket2", "winapi", ] -[[package]] -name = "multipart" -version = "0.17.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d050aeedc89243f5347c3e237e3e13dc76fbe4ae3742a57b94dc14f69acf76d4" -dependencies = [ - "buf_redux", - "httparse", - "log", - "mime", - "mime_guess", - "quick-error", - "rand 0.7.3", - "safemem", - "tempfile", - "twoway", -] - [[package]] name = "ntapi" version = "0.3.6" @@ -805,7 +511,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85d8faea6c018131952a192ee55bd9394c51fc6f63294b668d97636e6f842d40" dependencies = [ "base64ct", - "rand_core 0.6.2", + "rand_core", ] [[package]] @@ -854,27 +560,7 @@ checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d" dependencies = [ "maplit", "pest", - "sha-1 0.8.2", -] - -[[package]] -name = "pin-project" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96fa8ebb90271c4477f144354485b8068bd8f6b78b428b01ba892ca26caf0b63" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "758669ae3558c6f74bd2a18b41f7ac0b5a195aea6639d6a9b5e5d1ad5ba24c0b" -dependencies = [ - "proc-macro2", - "quote", - "syn", + "sha-1", ] [[package]] @@ -883,12 +569,6 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - [[package]] name = "ppv-lite86" version = "0.2.10" @@ -897,9 +577,9 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" [[package]] name = "proc-macro2" -version = "1.0.24" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" dependencies = [ "unicode-xid", ] @@ -915,12 +595,6 @@ dependencies = [ "unicase", ] -[[package]] -name = "quick-error" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" - [[package]] name = "quote" version = "1.0.9" @@ -930,19 +604,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" -dependencies = [ - "getrandom 0.1.16", - "libc", - "rand_chacha 0.2.2", - "rand_core 0.5.1", - "rand_hc 0.2.0", -] - [[package]] name = "rand" version = "0.8.3" @@ -950,19 +611,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ "libc", - "rand_chacha 0.3.0", - "rand_core 0.6.2", - "rand_hc 0.3.0", -] - -[[package]] -name = "rand_chacha" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" -dependencies = [ - "ppv-lite86", - "rand_core 0.5.1", + "rand_chacha", + "rand_core", + "rand_hc", ] [[package]] @@ -972,16 +623,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", - "rand_core 0.6.2", -] - -[[package]] -name = "rand_core" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" -dependencies = [ - "getrandom 0.1.16", + "rand_core", ] [[package]] @@ -990,16 +632,7 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" dependencies = [ - "getrandom 0.2.2", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core 0.5.1", + "getrandom", ] [[package]] @@ -1008,7 +641,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" dependencies = [ - "rand_core 0.6.2", + "rand_core", ] [[package]] @@ -1052,12 +685,6 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" -[[package]] -name = "safemem" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" - [[package]] name = "same-file" version = "1.0.6" @@ -1067,12 +694,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "scoped-tls" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" - [[package]] name = "scopeguard" version = "1.1.0" @@ -1081,18 +702,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.124" +version = "1.0.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f" +checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.124" +version = "1.0.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1800f7693e94e186f5e25a28291ae1570da908aff7d97a095dec1e56ff99069b" +checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" dependencies = [ "proc-macro2", "quote", @@ -1110,43 +731,18 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_urlencoded" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" -dependencies = [ - "form_urlencoded", - "itoa", - "ryu", - "serde", -] - [[package]] name = "sha-1" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df" dependencies = [ - "block-buffer 0.7.3", + "block-buffer", "digest 0.8.1", "fake-simd", "opaque-debug 0.2.3", ] -[[package]] -name = "sha-1" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfebf75d25bd900fd1e7d11501efab59bc846dbc76196839663e6637bba9f25f" -dependencies = [ - "block-buffer 0.9.0", - "cfg-if", - "cpuid-bool", - "digest 0.9.0", - "opaque-debug 0.3.0", -] - [[package]] name = "signal-hook-registry" version = "1.3.0" @@ -1167,12 +763,6 @@ dependencies = [ "termcolor", ] -[[package]] -name = "slab" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" - [[package]] name = "slug" version = "0.1.4" @@ -1188,17 +778,6 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" -[[package]] -name = "socket2" -version = "0.3.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" -dependencies = [ - "cfg-if", - "libc", - "winapi", -] - [[package]] name = "strsim" version = "0.8.0" @@ -1213,9 +792,9 @@ checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" [[package]] name = "syn" -version = "1.0.64" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fd9d1e9976102a03c542daa2eff1b43f9d72306342f3f8b3ed5fb8908195d6f" +checksum = "3ce15dd3ed8aa2f8eeac4716d6ef5ab58b6b9256db41d7e1a0224c2788e8fd87" dependencies = [ "proc-macro2", "quote", @@ -1230,7 +809,7 @@ checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" dependencies = [ "cfg-if", "libc", - "rand 0.8.3", + "rand", "redox_syscall", "remove_dir_all", "winapi", @@ -1250,7 +829,7 @@ dependencies = [ "percent-encoding", "pest", "pest_derive", - "rand 0.8.3", + "rand", "regex", "serde", "serde_json", @@ -1295,21 +874,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "tinyvec" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" - [[package]] name = "tokio" version = "1.4.0" @@ -1341,44 +905,6 @@ dependencies = [ "syn", ] -[[package]] -name = "tokio-stream" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e177a5d8c3bf36de9ebe6d58537d8879e964332f93fb3339e43f618c81361af0" -dependencies = [ - "futures-core", - "pin-project-lite", - "tokio", -] - -[[package]] -name = "tokio-tungstenite" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1a5f475f1b9d077ea1017ecbc60890fda8e54942d680ca0b1d2b47cfa2d861b" -dependencies = [ - "futures-util", - "log", - "pin-project", - "tokio", - "tungstenite", -] - -[[package]] -name = "tokio-util" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5143d049e85af7fbc36f5454d990e62c2df705b3589f123b71f441b6b59f443f" -dependencies = [ - "bytes", - "futures-core", - "futures-sink", - "log", - "pin-project-lite", - "tokio", -] - [[package]] name = "toml" version = "0.5.8" @@ -1388,77 +914,6 @@ dependencies = [ "serde", ] -[[package]] -name = "tower-service" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" - -[[package]] -name = "tracing" -version = "0.1.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f" -dependencies = [ - "cfg-if", - "log", - "pin-project-lite", - "tracing-core", -] - -[[package]] -name = "tracing-core" -version = "0.1.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" -dependencies = [ - "lazy_static", -] - -[[package]] -name = "tracing-futures" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2" -dependencies = [ - "pin-project", - "tracing", -] - -[[package]] -name = "try-lock" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" - -[[package]] -name = "tungstenite" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ada8297e8d70872fa9a551d93250a9f407beb9f37ef86494eb20012a2ff7c24" -dependencies = [ - "base64", - "byteorder", - "bytes", - "http", - "httparse", - "input_buffer", - "log", - "rand 0.8.3", - "sha-1 0.9.4", - "url", - "utf-8", -] - -[[package]] -name = "twoway" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59b11b2b5241ba34be09c3cc85a36e56e48f9888862e19cedf23336d35316ed1" -dependencies = [ - "memchr", -] - [[package]] name = "typenum" version = "1.13.0" @@ -1530,24 +985,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "unicode-bidi" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" -dependencies = [ - "matches", -] - -[[package]] -name = "unicode-normalization" -version = "0.1.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07fbfce1c8a97d547e8b5334978438d9d6ec8c20e38f56d4a4374d181493eaef" -dependencies = [ - "tinyvec", -] - [[package]] name = "unicode-width" version = "0.1.8" @@ -1560,24 +997,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" -[[package]] -name = "url" -version = "2.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ccd964113622c8e9322cfac19eb1004a07e636c545f325da085d5cdde6f1f8b" -dependencies = [ - "form_urlencoded", - "idna", - "matches", - "percent-encoding", -] - -[[package]] -name = "utf-8" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7" - [[package]] name = "vec_map" version = "0.8.2" @@ -1592,61 +1011,15 @@ checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" [[package]] name = "walkdir" -version = "2.3.1" +version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" dependencies = [ "same-file", "winapi", "winapi-util", ] -[[package]] -name = "want" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" -dependencies = [ - "log", - "try-lock", -] - -[[package]] -name = "warp" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dafd0aac2818a94a34df0df1100a7356c493d8ede4393875fd0b5c51bb6bc80" -dependencies = [ - "bytes", - "futures", - "headers", - "http", - "hyper", - "log", - "mime", - "mime_guess", - "multipart", - "percent-encoding", - "pin-project", - "scoped-tls", - "serde", - "serde_json", - "serde_urlencoded", - "tokio", - "tokio-stream", - "tokio-tungstenite", - "tokio-util", - "tower-service", - "tracing", - "tracing-futures", -] - -[[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 636caaf..282df06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,6 @@ simplelog = "0.10.0" tera = "1.7.0" tokio = { version = "1", features = ["full"] } toml = "0.5" -warp = "0.3" [dev-dependencies] tempfile = "3" diff --git a/src/bin/caty-blog.rs b/src/bin/caty-blog.rs index a02a35d..ec0d722 100644 --- a/src/bin/caty-blog.rs +++ b/src/bin/caty-blog.rs @@ -1,7 +1,6 @@ use std::sync::Arc; -use caty_blog::*; -use warp::Filter; +use caty_blog::*; #[tokio::main] async fn main() -> Result<(), Box> { @@ -10,10 +9,5 @@ async fn main() -> Result<(), Box> { // Configure logging - // Do shit based on arguments - // - Either generate the base directory structure for a blog - // - Or load an existing site and serve its routes based on the loaded config - // warp::serve(routes).run(([127,0,0,1], 3030)).await; - Ok(()) } diff --git a/src/config.rs b/src/config.rs index 6007613..766d024 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,8 +7,8 @@ use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; use serde::{Serialize, Deserialize}; use toml; -use crate::auth; -use crate::common; +use super::auth; +use super::common; fn args() -> App<'static, 'static> { App::new("Caty's Blog") diff --git a/src/lib.rs b/src/lib.rs index 43daa5e..b59d317 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,3 +2,4 @@ pub mod auth; pub mod config; pub mod common; pub mod render; +pub mod routes; diff --git a/src/render.rs b/src/render.rs index 019681f..595b020 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,8 +1,8 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; -use crate::config::AppConfig; -use crate::common; +use super::config::AppConfig; +use super::common; pub use pulldown_cmark::{Parser, html}; pub use tera::{Tera, Context}; diff --git a/src/routes.rs b/src/routes.rs new file mode 100644 index 0000000..e4e29aa --- /dev/null +++ b/src/routes.rs @@ -0,0 +1,105 @@ +use std::path::Path; +use std::sync::Arc; + +use super::config::AppConfig; +use super::render; + +pub fn topic_posts(app: Arc, topic: String) { + let app = app.clone(); + let t = topic.clone(); + let handler = move || { + let mut body = String::new(); + if let Ok(instance) = render::load_default_template() { + let engine = render::Engine::new(app.clone(), &t, "default.tmpl", instance); + if let Ok(output) = render::render_topic(engine) { + body = output; + } + } + + }; + + if &topic == "main" { + // Iron route + } else { + // Iron route + } +} + +pub fn static_assets(app: Arc) { + let app = &app.clone(); + let static_path = Path::new(&app.docpaths.webroot).join("static"); + // Iron route +} + +pub fn topic_assets(app: Arc, topic: String) { + let app = &app.clone(); + let topic_asset_path = Path::new(&app.docpaths.webroot).join(&topic).join("ext"); + // Iron route +} + +#[cfg(test)] +mod tests { + use std::fs::File; + use std::io::prelude::*; + + use super::*; + use crate::config::AppConfig; + use tempfile; + + #[tokio::test] + async fn main_page() { + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let config = AppConfig::generate(&dir, &mut src).unwrap(); + let config = Arc::new(config); + let filter = topic_posts(config, "main".to_owned()); + + let test_file = b"### Some title\n\nsome bytes here\n"; + let mut f = File::create(dir.path().join("blog/webroot/main/posts/1.md")).unwrap(); + f.write_all(test_file).unwrap(); + + } + + #[tokio::test] + async fn topic_page() { + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let config = AppConfig::generate(&dir, &mut src).unwrap(); + let config = Arc::new(config); + let filter = topic_posts(config, "and-more".to_owned()); + + let test_file = b"### Some title\n\nsome bytes here\n"; + let mut f = File::create(dir.path().join("blog/webroot/and-more/posts/1.md")).unwrap(); + f.write_all(test_file).unwrap(); + + } + + #[tokio::test] + async fn static_content() { + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let config = AppConfig::generate(&dir, &mut src).unwrap(); + let config = Arc::new(config); + + let test_file = b"some bytes here\n"; + let mut f = File::create(dir.path().join("blog/webroot/static/example")).unwrap(); + f.write_all(test_file).unwrap(); + let filter = static_assets(config); + + } + + #[tokio::test] + async fn topic_static() { + let dir = tempfile::tempdir().unwrap(); + let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; + let config = AppConfig::generate(&dir, &mut src).unwrap(); + let config = Arc::new(config); + + let test_file = b"some bytes here\n"; + let mut f = File::create(dir.path().join("blog/webroot/one/ext/example")).unwrap(); + f.write_all(test_file).unwrap(); + + let filter = topic_assets(config.clone(), "one".to_owned()); + + } +} From f5e680322aebf18a8374a154feb0841e2382fcd9 Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Thu, 8 Apr 2021 20:22:54 +0200 Subject: [PATCH 12/13] Create routes with routerify, reach base MVP - Closes #1 directory structure is generated by `new` subcommand - Closes #2 dynamic routes created and verified - Closes #4 generated config loads and runs the site as defined --- Cargo.lock | 245 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/bin/caty-blog.rs | 14 ++- src/render.rs | 15 +-- src/routes.rs | 142 ++++++++++++------------- 5 files changed, 335 insertions(+), 83 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3674bb1..e5ba1e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,9 +124,11 @@ dependencies = [ "clap", "data-encoding", "glob", + "hyper", "log", "pulldown-cmark", "rand", + "routerify", "serde", "simplelog", "tempfile", @@ -242,6 +244,45 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "futures-channel" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94" + +[[package]] +name = "futures-sink" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85754d98985841b7d4f5e8e6fbfa4a4ac847916893ec511a2917ccd8525b8bb3" + +[[package]] +name = "futures-task" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa189ef211c15ee602667a6fcfe1c1fd9e07d42250d2156382820fba33c9df80" + +[[package]] +name = "futures-util" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + [[package]] name = "generic-array" version = "0.12.4" @@ -302,6 +343,31 @@ dependencies = [ "walkdir", ] +[[package]] +name = "h2" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc018e188373e2777d0ef2467ebff62a08e66c3f5857b23c8fbec3018210dc00" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" + [[package]] name = "hermit-abi" version = "0.1.18" @@ -311,12 +377,70 @@ dependencies = [ "libc", ] +[[package]] +name = "http" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7245cd7449cc792608c3c8a9eaf69bd4eabbabf802713748fd739c98b82f0747" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfb77c123b4e2f72a2069aeae0b4b4949cc7e966df277813fc16347e7549737" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691" + +[[package]] +name = "httpdate" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" + [[package]] name = "humansize" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" +[[package]] +name = "hyper" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bf09f61b52cfcf4c00de50df88ae423d6c02354e385a86341133b5338630ad1" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + [[package]] name = "ignore" version = "0.4.17" @@ -335,6 +459,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "indexmap" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "instant" version = "0.1.9" @@ -563,12 +697,38 @@ dependencies = [ "sha-1", ] +[[package]] +name = "pin-project" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc174859768806e91ae575187ada95c91a29e96a98dc5d2cd9a1fed039501ba6" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a490329918e856ed1b083f244e3bfe2d8c4f336407e4ea9e1a9f479ff09049e5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "ppv-lite86" version = "0.2.10" @@ -679,6 +839,19 @@ dependencies = [ "winapi", ] +[[package]] +name = "routerify" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c18f54182904acfbd8082adb04985c59eb037027105631e3a68a5a51de31294d" +dependencies = [ + "http", + "hyper", + "lazy_static", + "percent-encoding", + "regex", +] + [[package]] name = "ryu" version = "1.0.5" @@ -763,6 +936,12 @@ dependencies = [ "termcolor", ] +[[package]] +name = "slab" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" + [[package]] name = "slug" version = "0.1.4" @@ -778,6 +957,16 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" +[[package]] +name = "socket2" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e3dfc207c526015c632472a77be09cf1b6e46866581aecae5cc38fb4235dea2" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "strsim" version = "0.8.0" @@ -905,6 +1094,20 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-util" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5143d049e85af7fbc36f5454d990e62c2df705b3589f123b71f441b6b59f443f" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio", +] + [[package]] name = "toml" version = "0.5.8" @@ -914,6 +1117,38 @@ dependencies = [ "serde", ] +[[package]] +name = "tower-service" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" + +[[package]] +name = "tracing" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "try-lock" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" + [[package]] name = "typenum" version = "1.13.0" @@ -1020,6 +1255,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 282df06..1671743 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,9 +11,11 @@ argon2 = "0.1" clap = "2.33.3" data-encoding = "2.3.2" glob = "0.3.0" +hyper = "0.14" log = "0.4.14" pulldown-cmark = { version = "0.8", default-features = false, features = ["simd"] } rand = "0.8" +routerify = "2.0.0" serde = { version = "1", features = ["derive"] } simplelog = "0.10.0" tera = "1.7.0" diff --git a/src/bin/caty-blog.rs b/src/bin/caty-blog.rs index ec0d722..c4914be 100644 --- a/src/bin/caty-blog.rs +++ b/src/bin/caty-blog.rs @@ -1,13 +1,25 @@ +use std::net::SocketAddr; use std::sync::Arc; use caty_blog::*; +use hyper::Server; +use routerify::RouterService; #[tokio::main] async fn main() -> Result<(), Box> { let config = config::load()?; let app = Arc::new(config); + + // TODO: Configure logging - // Configure logging + let router = routes::router(app.clone()); + let service = RouterService::new(router).unwrap(); + let addr: SocketAddr = "0.0.0.0:9090".parse().unwrap(); + let server = Server::bind(&addr).serve(service); + + if let Err(err) = server.await { + eprintln!("Server error: {}", err) + } Ok(()) } diff --git a/src/render.rs b/src/render.rs index 595b020..9590de6 100644 --- a/src/render.rs +++ b/src/render.rs @@ -20,8 +20,8 @@ impl Engine { pub fn new(a: Arc, ts: &str, tmpl: &str, inst: Tera) -> Engine { Engine { app: a.clone(), - topic_slug: ts.to_string(), - template: tmpl.to_string(), + topic_slug: ts.to_owned(), + template: tmpl.to_owned(), instance: inst } } @@ -45,7 +45,7 @@ pub fn render_topic(engine: Engine) -> Result } fn load_topic(engine: &Engine) -> Result, Box> { - let topic_path = Path::new(&engine.app.docpaths.webroot).join(&engine.topic_slug); + let topic_path = Path::new(&engine.app.docpaths.webroot).join(&engine.topic_slug).join("posts"); let pat = format!("{}/*.md", topic_path.display()); let paths = common::path_matches(&pat)?; read_to_html(paths) @@ -97,14 +97,15 @@ Very cool, but maybe not super useful Super Wow! "#; - let mut f = File::create(&dir.path().join("blog").join("webroot").join("one").join("post1.md")).unwrap(); + let mut f = File::create(&dir.path().join("blog/webroot/one/posts/post1.md")).unwrap(); f.write_all(&post.as_bytes()).unwrap(); - let mut f = File::create(&dir.path().join("blog").join("webroot").join("one").join("post2.md")).unwrap(); + let mut f = File::create(&dir.path().join("blog/webroot/one/posts/post2.md")).unwrap(); f.write_all(&post2.as_bytes()).unwrap(); - let page = render_topic(engine); + let page = render_topic(engine).unwrap(); - assert!(page.is_ok()) + assert!(page.contains("super useful")); + assert!(page.contains("Super Wow!")); } } diff --git a/src/routes.rs b/src/routes.rs index e4e29aa..966fcb6 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,105 +1,97 @@ +use std::convert::Infallible; +use std::fs::File; +use std::io::prelude::*; use std::path::Path; use std::sync::Arc; +use hyper::{Body, Request, Response};//, StatusCode}; +use routerify::{prelude::*, Router}; + use super::config::AppConfig; use super::render; -pub fn topic_posts(app: Arc, topic: String) { - let app = app.clone(); - let t = topic.clone(); - let handler = move || { - let mut body = String::new(); - if let Ok(instance) = render::load_default_template() { - let engine = render::Engine::new(app.clone(), &t, "default.tmpl", instance); - if let Ok(output) = render::render_topic(engine) { - body = output; - } - } - }; +pub fn router(app: Arc) -> Router { + Router::builder() + .data(app.clone()) + .get("/", index_handler) + .get("/:topic", topic_handler) + .get("/:topic/ext/:fname", topic_assets) + .get("/static/:fname", static_assets) + .build() + .unwrap() +} - if &topic == "main" { - // Iron route - } else { - // Iron route - } +/// Handler for "/" +async fn index_handler(req: Request) -> Result, Infallible> { + let app = req.data::>().unwrap(); + topic_posts(app.clone(), "main".to_owned()).await +} + +/// Handler for "/:topic" +async fn topic_handler(req: Request) -> Result, Infallible> { + let app = req.data::>().unwrap(); + let topic = req.param("topic").unwrap(); + topic_posts(app.clone(), topic.to_owned()).await +} + +/// Called by topic_handler to dynamically generate topic pages +async fn topic_posts(app: Arc, topic: String) -> Result, Infallible> { + let instance = render::load_default_template().unwrap(); + let engine = render::Engine::new(app.clone(), &topic, "default.tmpl", instance); + let output = render::render_topic(engine).unwrap(); + Ok(Response::new(Body::from(output))) } -pub fn static_assets(app: Arc) { - let app = &app.clone(); - let static_path = Path::new(&app.docpaths.webroot).join("static"); - // Iron route +/// Handler for "/static/:fname" +async fn static_assets(req: Request) -> Result, Infallible> { + let app = req.data::>().unwrap(); + let resource = req.param("fname").unwrap(); + let static_path = Path::new(&app.docpaths.webroot).join("static").join(resource); + let mut f = File::open(static_path).unwrap(); + let mut buf = Vec::new(); + f.read_to_end(&mut buf).unwrap(); + Ok(Response::new(Body::from(buf))) } -pub fn topic_assets(app: Arc, topic: String) { - let app = &app.clone(); - let topic_asset_path = Path::new(&app.docpaths.webroot).join(&topic).join("ext"); - // Iron route +/// Handler for "/:topic/ext/:fname" +async fn topic_assets(req: Request) -> Result, Infallible> { + let app = req.data::>().unwrap(); + let topic = req.param("topic").unwrap(); + let resource = req.param("fname").unwrap(); + let topic_asset_path = Path::new(&app.docpaths.webroot).join(topic).join("ext").join(resource); + let mut f = File::open(topic_asset_path).unwrap(); + let mut buf = Vec::new(); + f.read_to_end(&mut buf).unwrap(); + Ok(Response::new(Body::from(buf))) } + #[cfg(test)] mod tests { + /* use std::fs::File; use std::io::prelude::*; + use std::net::SocketAddr; use super::*; use crate::config::AppConfig; + use routerify::RouterService; use tempfile; + // TODO: figure out how to actually test this.... + #[tokio::test] - async fn main_page() { - let dir = tempfile::tempdir().unwrap(); - let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; - let config = AppConfig::generate(&dir, &mut src).unwrap(); - let config = Arc::new(config); - let filter = topic_posts(config, "main".to_owned()); - - let test_file = b"### Some title\n\nsome bytes here\n"; - let mut f = File::create(dir.path().join("blog/webroot/main/posts/1.md")).unwrap(); - f.write_all(test_file).unwrap(); - - } - - #[tokio::test] - async fn topic_page() { + async fn router_test() { let dir = tempfile::tempdir().unwrap(); let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; - let config = AppConfig::generate(&dir, &mut src).unwrap(); - let config = Arc::new(config); - let filter = topic_posts(config, "and-more".to_owned()); - - let test_file = b"### Some title\n\nsome bytes here\n"; - let mut f = File::create(dir.path().join("blog/webroot/and-more/posts/1.md")).unwrap(); - f.write_all(test_file).unwrap(); - - } - - #[tokio::test] - async fn static_content() { - let dir = tempfile::tempdir().unwrap(); - let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; - let config = AppConfig::generate(&dir, &mut src).unwrap(); - let config = Arc::new(config); - - let test_file = b"some bytes here\n"; - let mut f = File::create(dir.path().join("blog/webroot/static/example")).unwrap(); - f.write_all(test_file).unwrap(); - let filter = static_assets(config); + let app = AppConfig::generate(&dir, &mut src).unwrap(); + let app = Arc::new(app); + let router = router(app.clone()); + let service = RouterService::new(router).unwrap(); + let addr: SocketAddr = "0.0.0.0:9999".parse().unwrap(); } + */ - #[tokio::test] - async fn topic_static() { - let dir = tempfile::tempdir().unwrap(); - let mut src: &[u8] = b"Blog Name\nAuthor Name\nOne, Two, Three, And More\nadmin\n"; - let config = AppConfig::generate(&dir, &mut src).unwrap(); - let config = Arc::new(config); - - let test_file = b"some bytes here\n"; - let mut f = File::create(dir.path().join("blog/webroot/one/ext/example")).unwrap(); - f.write_all(test_file).unwrap(); - - let filter = topic_assets(config.clone(), "one".to_owned()); - - } } From 13c279b3ff476d384cccea932b075e7629810faf Mon Sep 17 00:00:00 2001 From: "Anthony J. Martinez" Date: Thu, 8 Apr 2021 20:55:35 +0200 Subject: [PATCH 13/13] Fix the default template - Closes #9 - Closes #10 --- src/render/default.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/render/default.rs b/src/render/default.rs index 6abc540..90d1457 100644 --- a/src/render/default.rs +++ b/src/render/default.rs @@ -12,7 +12,7 @@ pub const TEMPLATE: &str = r#"

{{ blog.name }}

+{% if posts | length == 0 %} +

Coming Soon!

+{% else %} {%- for post in posts %} {{ post }} {%- endfor -%} +{% endif %}