From 10f67a8632ae53a98dc560d7ade98e47b9ec0151 Mon Sep 17 00:00:00 2001 From: thevickypedia Date: Sat, 17 Feb 2024 21:55:15 -0600 Subject: [PATCH] Convert `.html` files into `String` to execute as binary Update docstrings and README.md --- .github/workflows/rust.yml | 4 -- README.md | 20 ++++++---- src/jinja.rs | 40 ++++--------------- src/lib.rs | 2 + src/templates/{index.html => index.rs} | 14 ++++++- src/templates/{landing.html => landing.rs} | 14 ++++++- src/templates/{listing.html => listing.rs} | 14 ++++++- src/templates/{logout.html => logout.rs} | 14 ++++++- src/templates/mod.rs | 12 ++++++ src/templates/{session.html => session.rs} | 15 ++++++- .../{unauthorized.html => unauthorized.rs} | 15 ++++++- 11 files changed, 114 insertions(+), 50 deletions(-) rename src/templates/{index.html => index.rs} (95%) rename src/templates/{landing.html => landing.rs} (95%) rename src/templates/{listing.html => listing.rs} (94%) rename src/templates/{logout.html => logout.rs} (87%) create mode 100644 src/templates/mod.rs rename src/templates/{session.html => session.rs} (84%) rename src/templates/{unauthorized.html => unauthorized.rs} (84%) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5270782..708a56d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -134,10 +134,6 @@ jobs: steps: - name: Checkout Repo uses: actions/checkout@v4 - - name: Setup Python - uses: actions/setup-python@v5 - with: - python-version: 3.9 - name: Update Rust # print it with style run: | diff --git a/README.md b/README.md index 18c7154..8b4328a 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,15 @@ [![made-with-rust][rust-logo]][rust-src-page] [![build][gh-logo]][build] -[![crates.io][crates-logo]][crates] -[![docs.rs][docs-logo]][docs] + +[![crates.io][crates-logo]][crate] + +**Platform Supported** + +[![platform][platform]][cross-platform] #### Summary -[`RuStream`][repo] is an application written in Rust to stream videos using Actix API via authenticated sessions. +[`RuStream`][repo] is a self-hosted streaming engine, that can render videos via authenticated sessions. ## Usage @@ -75,14 +79,14 @@ async fn main() { "authorization": {"rustic": "S0m3rAn0mP@ssW0rD"}, "video_source": "/Users/hannibal/Downloads/stream", "video_port": 5883, - "file_formats": [".mov", ".mp4", ".mkv"], + "file_formats": ["mov", "mp4", "mkv"], "workers": 10 } ``` ## Crate -https://crates.io/crates/RuStream +[https://crates.io/crates/RuStream][crate] ## Linting ### Requirement @@ -106,8 +110,8 @@ Licensed under the [MIT License][license] [rust-src-page]: https://www.rust-lang.org/ [rust-logo]: https://img.shields.io/badge/Made%20with-Rust-black?style=for-the-badge&logo=Rust [gh-logo]: https://github.com/thevickypedia/RuStream/actions/workflows/rust.yml/badge.svg -[docs-logo]: https://docs.rs/RuStream/badge.svg -[docs]: https://docs.rs/RuStream -[crates]: https://crates.io/crates/RuStream +[crate]: https://crates.io/crates/RuStream [gh-checks]: https://github.com/thevickypedia/RuStream/actions/workflows/rust.yml [crates-logo]: https://img.shields.io/crates/v/RuStream.svg +[platform]: https://img.shields.io/badge/Platform-Linux_|_macOS_|_Windows-blue?style=for-the-badge&logo=ubuntu +[cross-platform]: https://www.computerlanguage.com/results.php?definition=cross+platform diff --git a/src/jinja.rs b/src/jinja.rs index 1ffbd7a..cd0ebfc 100644 --- a/src/jinja.rs +++ b/src/jinja.rs @@ -1,40 +1,16 @@ -use std::path::Path; use std::sync::{Arc, Mutex}; -/// Read the content of each file and return it as a String. -/// -/// # Arguments -/// -/// * `filename` - Filename that has to be read. -/// -/// # Returns -/// -/// String representation of the file content. -fn get_content(filename: &str) -> String { - let filepath = Path::new(env!("CARGO_MANIFEST_DIR")) - .join("src") - .join("templates") - .join(format!("{}.html", filename)) - .to_string_lossy() - .to_string(); - std::fs::read_to_string(filepath).unwrap_or_else(|_| String::new()) -} +use crate::templates; -/// Reads all the HTML files in templates directory and loads the content into a Jinja Environment -/// -/// # Rendered files -/// - Index page template that is served as HTML response for the root endpoint. -/// - Landing page template that is served as HTML response while streaming videos. -/// - Listing page template that is served as HTML response after successful authentication. -/// - Logout page template that is served as HTML response when the user decides to end the session. -/// - Session page template that is served as HTML response when invalid/expired session tokens are received. -/// - Unauthorized page template that is served as HTML response after failed authentication. +/// Loads all the HTML content into a Jinja Environment pub fn environment() -> Arc>> { let mut env = minijinja::Environment::new(); - for html in ["index", "landing", "listing", "logout", "session", "unauthorized"] { - let content = get_content(html); - env.add_template_owned(html, content).unwrap(); - } + env.add_template_owned("index", templates::index::get_content()).unwrap(); + env.add_template_owned("landing", templates::landing::get_content()).unwrap(); + env.add_template_owned("listing", templates::listing::get_content()).unwrap(); + env.add_template_owned("logout", templates::logout::get_content()).unwrap(); + env.add_template_owned("session", templates::session::get_content()).unwrap(); + env.add_template_owned("unauthorized", templates::unauthorized::get_content()).unwrap(); let mutex = Mutex::new(env.to_owned()); Arc::new(mutex) } diff --git a/src/lib.rs b/src/lib.rs index c4dc69d..64a6ba2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,8 @@ mod jinja; mod routes; /// Module to store all the helper functions. mod squire; +/// Module to load all the templates for the UI. +mod templates; /// Contains entrypoint and initializer settings to trigger the asynchronous HTTPServer /// diff --git a/src/templates/index.html b/src/templates/index.rs similarity index 95% rename from src/templates/index.html rename to src/templates/index.rs index 78e1e4f..990d535 100644 --- a/src/templates/index.html +++ b/src/templates/index.rs @@ -1,4 +1,14 @@ - +/// Get the HTML content to render the index/root page. +/// +/// # See Also +/// +/// - This page is served as a response for the `/` entry point. +/// +/// # Returns +/// +/// A `String` version of the HTML, CSS and JS content. +pub fn get_content() -> String { + r###" @@ -196,3 +206,5 @@

This page requires JavaScript +"###.to_string() +} diff --git a/src/templates/landing.html b/src/templates/landing.rs similarity index 95% rename from src/templates/landing.html rename to src/templates/landing.rs index ba54953..924c677 100644 --- a/src/templates/landing.html +++ b/src/templates/landing.rs @@ -1,4 +1,14 @@ - +/// Get the HTML content to render the streaming/landing page. +/// +/// # See Also +/// +/// - This page is served as a response for the `/stream` entry point. +/// +/// # Returns +/// +/// A `String` version of the HTML, CSS and JS content. +pub fn get_content() -> String { + r###" @@ -192,3 +202,5 @@

{{ video_title }}

+"###.to_string() +} diff --git a/src/templates/listing.html b/src/templates/listing.rs similarity index 94% rename from src/templates/listing.html rename to src/templates/listing.rs index 7c7fffd..fe3978e 100644 --- a/src/templates/listing.html +++ b/src/templates/listing.rs @@ -1,4 +1,14 @@ - +/// Get the HTML content to render the home/listing page. +/// +/// # See Also +/// +/// - This page is served as a response for the `/home` entry point. +/// +/// # Returns +/// +/// A `String` version of the HTML, CSS and JS content. +pub fn get_content() -> String { + r###" @@ -143,3 +153,5 @@

No content was rendered by the server

+"###.to_string() +} diff --git a/src/templates/logout.html b/src/templates/logout.rs similarity index 87% rename from src/templates/logout.html rename to src/templates/logout.rs index cffbe88..57d01b9 100644 --- a/src/templates/logout.html +++ b/src/templates/logout.rs @@ -1,4 +1,14 @@ - +/// Get the HTML content to render the logout page. +/// +/// # See Also +/// +/// - This page is served as a response for the `/logout` entry point. +/// +/// # Returns +/// +/// A `String` version of the HTML, CSS and JS content. +pub fn get_content() -> String { + r###" Rustic video streaming @@ -67,3 +77,5 @@

Click ME to reach out.

}); +"###.to_string() +} diff --git a/src/templates/mod.rs b/src/templates/mod.rs new file mode 100644 index 0000000..70ab863 --- /dev/null +++ b/src/templates/mod.rs @@ -0,0 +1,12 @@ +/// Index page template that is served as HTML response for the root endpoint. +pub mod index; +/// Landing page template that is served as HTML response while streaming videos. +pub mod landing; +/// Listing page template that is served as HTML response after successful authentication. +pub mod listing; +/// Logout page template that is served as HTML response when the user decides to end the session. +pub mod logout; +/// Session page template that is served as HTML response when invalid/expired session tokens are received. +pub mod session; +/// Unauthorized page template that is served as HTML response after failed authentication. +pub mod unauthorized; diff --git a/src/templates/session.html b/src/templates/session.rs similarity index 84% rename from src/templates/session.html rename to src/templates/session.rs index 144ced6..8ae0b9e 100644 --- a/src/templates/session.html +++ b/src/templates/session.rs @@ -1,4 +1,15 @@ - +/// Get the HTML content to render the session expired/invalid page. +/// +/// # See Also +/// +/// - This page is served as a response for all the content delivery entry points, +/// when the user session-token is invalid or expired. +/// +/// # Returns +/// +/// A `String` version of the HTML, CSS and JS content. +pub fn get_content() -> String { + r###" Rustic video streaming @@ -66,3 +77,5 @@

Click HERE to reach out.

}); +"###.to_string() +} diff --git a/src/templates/unauthorized.html b/src/templates/unauthorized.rs similarity index 84% rename from src/templates/unauthorized.html rename to src/templates/unauthorized.rs index 3ed3850..128c927 100644 --- a/src/templates/unauthorized.html +++ b/src/templates/unauthorized.rs @@ -1,4 +1,15 @@ - +/// Get the HTML content to render the session unauthorized page. +/// +/// # See Also +/// +/// - This page is served as a response for all the entry points, +/// when the user tries to access a page without valid authentication. +/// +/// # Returns +/// +/// A `String` version of the HTML, CSS and JS content. +pub fn get_content() -> String { + r###" Rustic video streaming @@ -64,3 +75,5 @@

Click HERE to reach out.

}); +"###.to_string() +}