From a9acc17c7926e6a3611c785d6e00555243ab1fa5 Mon Sep 17 00:00:00 2001 From: Leonora Tindall Date: Mon, 18 Oct 2021 19:24:21 -0500 Subject: [PATCH] feat: sigweb basic setup --- .gitignore | 6 ++--- Cargo.lock | 30 ++++++++++++++++++++++++ libsig/.gitignore | 2 -- libsig/Cargo.lock | 7 ++++++ sigweb/Cargo.toml | 3 ++- sigweb/src/main.rs | 51 ++++++++++++++++++++++++++++++++++++----- sigweb/src/templates.rs | 35 ++++++++++++++++++++++++++++ sigweb/static/style.css | 19 +++++++++++++++ 8 files changed, 141 insertions(+), 12 deletions(-) delete mode 100644 libsig/.gitignore create mode 100644 libsig/Cargo.lock create mode 100644 sigweb/src/templates.rs create mode 100644 sigweb/static/style.css diff --git a/.gitignore b/.gitignore index ea5f23f..20e036d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -libsig/target/ -sigweb/target/ -target/ +libsig/target +sigweb/target +target \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 6f4e94d..f698aa7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -585,6 +585,35 @@ dependencies = [ "regex-automata", ] +[[package]] +name = "maud" +version = "0.22.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59d449907de7d1ae5b290cbaf9ea34a8a7df3fa5db027664bb55bb2b0fc1407c" +dependencies = [ + "maud_htmlescape", + "maud_macros", +] + +[[package]] +name = "maud_htmlescape" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0555daa37f94b5ebb206faf8cdc7b260c2aa371b509e929de9a1e37416d97fa6" + +[[package]] +name = "maud_macros" +version = "0.22.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6896f8e8cdcea80b99ac0f1f7a233708e640737a8517448f50500e401bb8d76" +dependencies = [ + "maud_htmlescape", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "memchr" version = "2.4.1" @@ -1162,6 +1191,7 @@ name = "sigweb" version = "0.1.0" dependencies = [ "libsig", + "maud", "rocket", "serde", "serde_json", diff --git a/libsig/.gitignore b/libsig/.gitignore deleted file mode 100644 index 96ef6c0..0000000 --- a/libsig/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target -Cargo.lock diff --git a/libsig/Cargo.lock b/libsig/Cargo.lock new file mode 100644 index 0000000..fcc60ae --- /dev/null +++ b/libsig/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "libsig" +version = "0.1.0" diff --git a/sigweb/Cargo.toml b/sigweb/Cargo.toml index fc7b8c7..44d6e7c 100644 --- a/sigweb/Cargo.toml +++ b/sigweb/Cargo.toml @@ -6,8 +6,9 @@ edition = "2018" [dependencies] serde = "1" serde_json = "1" +maud = "0.22" libsig = { path = "../libsig" } [dependencies.rocket] version = "0.5.0-rc.1" -features = ["json"] \ No newline at end of file +features = ["json"] diff --git a/sigweb/src/main.rs b/sigweb/src/main.rs index 7b9fcec..76f84b4 100644 --- a/sigweb/src/main.rs +++ b/sigweb/src/main.rs @@ -1,9 +1,12 @@ #[macro_use] extern crate rocket; use libsig; -use serde::{Serialize, Deserialize}; +use rocket::fs::NamedFile; +use rocket::response::content::{Css, Html}; use rocket::serde::json::Json; +use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; +mod templates; #[get("/api")] fn api_version_available() -> Json { @@ -26,24 +29,60 @@ struct Output { } #[get("/api/v1/generate")] -fn generate() -> Json { +fn api_v1_generate() -> Json { let idea = libsig::SongIdea::generate(); Json(Output { data: idea.clone(), - text: idea.to_string() + text: idea.to_string(), }) } #[get("/api/v1/generate_ambient")] -fn generate_ambient() -> Json { +fn api_v1_generate_ambient() -> Json { let idea = libsig::SongIdea::generate_ambient(); Json(Output { data: idea.clone(), - text: idea.to_string() + text: idea.to_string(), }) } +#[get("/")] +fn index() -> Html { + Html(templates::main_page_template(None)) +} + +#[get("/generate")] +fn generate() -> Html { + Html(templates::main_page_template(Some( + libsig::SongIdea::generate(), + ))) +} + +#[get("/generate_ambient")] +fn generate_ambient() -> Html { + Html(templates::main_page_template(Some( + libsig::SongIdea::generate_ambient(), + ))) +} + +#[get("/style.css")] +async fn style_css() -> Css> { + Css(NamedFile::open("static/style.css").await.ok()) +} + #[launch] fn rocket() -> _ { - rocket::build().mount("/", routes![api_version_available, api_v1_index, generate, generate_ambient]) + rocket::build().mount( + "/", + routes![ + api_version_available, + api_v1_index, + api_v1_generate, + api_v1_generate_ambient, + generate, + generate_ambient, + index, + style_css, + ], + ) } diff --git a/sigweb/src/templates.rs b/sigweb/src/templates.rs new file mode 100644 index 0000000..cfcdf0a --- /dev/null +++ b/sigweb/src/templates.rs @@ -0,0 +1,35 @@ +use maud::{html, DOCTYPE}; +use libsig::SongIdea; + +pub fn main_page_template(idea: Option) -> String { + let content = match idea { + Some(idea) => { idea.to_string() }, + None => { "Generate a new idea with the links below!".to_string() } + }; + let html = html! { + (DOCTYPE) + head { + link rel="stylesheet" href="style.css"; + } + body { + h1 { "Song Idea Generator" } + p{ "Why don't you make a song like this: " } + p.content { + (content) + } + footer { + p { + a href="./generate" { "generate new" } + " | " + a href="./generate_ambient" { "generate new (ambient)" } + } + p { + a href="http://nora.codes" { "my blog" } + " | " + a href="https://git.nora.codes/nora/song-idea-generator" { "code" } + } + } + } + }; + html.into() +} \ No newline at end of file diff --git a/sigweb/static/style.css b/sigweb/static/style.css new file mode 100644 index 0000000..0a3c695 --- /dev/null +++ b/sigweb/static/style.css @@ -0,0 +1,19 @@ +body { + background-color: bisque; + color: darkblue; + font-size: 12pt; + font-family: sans-serif; + max-width: 300pt; + margin-left: auto; + margin-right: auto; +} + +p.content { + padding-top: auto; + min-height: 100pt; +} + +footer { + font-size: 10pt; + text-align: center; +} \ No newline at end of file