-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
PathBuf
to load all HTML files in Jinja environment
Pass Jinja template as Mutex via app data to all endpoints
- Loading branch information
1 parent
a0ce049
commit 467f7c5
Showing
10 changed files
with
68 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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. | ||
pub 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()) | ||
} | ||
|
||
/// 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. | ||
pub fn environment() -> Arc<Mutex<minijinja::Environment<'static>>> { | ||
let mut env = minijinja::Environment::new(); | ||
for html in ["landing", "listing", "logout", "session"] { | ||
let content = get_content(&html); | ||
env.add_template_owned(html, content).unwrap(); | ||
} | ||
let mutex = Mutex::new(env.to_owned()); | ||
Arc::new(mutex) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.