diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-01 20:34:22 -0500 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-01 20:34:22 -0500 |
commit | 2026a8f4579b1db0f6e5e7b11ac33c13969adb6c (patch) | |
tree | daa13419b7227462775e325a4f5f60f2ed33c1da /src/admin.rs | |
parent | remove b64 imgs (diff) | |
download | xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.tar.gz xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.tar.bz2 xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.zip |
static serve refactor
Diffstat (limited to 'src/admin.rs')
-rw-r--r-- | src/admin.rs | 129 |
1 files changed, 0 insertions, 129 deletions
diff --git a/src/admin.rs b/src/admin.rs deleted file mode 100644 index 1da2f1e..0000000 --- a/src/admin.rs +++ /dev/null @@ -1,129 +0,0 @@ -use axum::response::Response; -use lazy_static::lazy_static; -use rand::{distributions::Alphanumeric, Rng}; -use tokio::sync::Mutex; - -use crate::{ - console::{self, sanatize}, - types::{http::ResponseCode, post::Post, session::Session, user::User}, -}; - -lazy_static! { - static ref SECRET: Mutex<String> = Mutex::new(String::new()); -} - -pub fn new_secret() -> String { - rand::thread_rng() - .sample_iter(&Alphanumeric) - .take(32) - .map(char::from) - .collect() -} - -pub async fn get_secret() -> String { - let mut secret = SECRET.lock().await; - if secret.is_empty() { - *secret = new_secret(); - } - secret.clone() -} - -pub async fn regen_secret() -> String { - let mut secret = SECRET.lock().await; - *secret = new_secret(); - secret.clone() -} - -pub fn generate_users() -> Response { - let users = match User::reterieve_all() { - Ok(users) => users, - Err(err) => return err, - }; - - let mut html = r#" - <tr> - <th>User ID</th> - <th>First Name</th> - <th>Last Name</th> - <th>Email</th> - <th>Password</th> - <th>Gender</th> - <th>Date</th> - <th>Day</th> - <th>Month</th> - <th>Year</th> - </tr> - "# - .to_string(); - - for user in users { - html.push_str( - &format!("<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>", - user.user_id, sanatize(&user.firstname), sanatize(&user.lastname), sanatize(&user.email), sanatize(&user.password), - sanatize(&user.gender), user.date, user.day, user.month, user.year - ) - ); - } - - ResponseCode::Success.text(&html) -} - -pub fn generate_posts() -> Response { - let posts = match Post::reterieve_all() { - Ok(posts) => posts, - Err(err) => return err, - }; - - let mut html = r#" - <tr> - <th>Post ID</th> - <th>User ID</th> - <th>Content</th> - <th>Likes</th> - <th>Comments</th> - <th>Date</th> - </tr> - "# - .to_string(); - - for post in posts { - let Ok(likes) = serde_json::to_string(&post.likes) else { continue }; - let Ok(comments) = serde_json::to_string(&post.comments) else { continue }; - - html.push_str(&format!( - "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>", - post.post_id, - post.user_id, - sanatize(&post.content), - console::beautify(&likes), - console::beautify(&comments), - post.date - )); - } - - ResponseCode::Success.text(&html) -} - -pub fn generate_sessions() -> Response { - let sessions = match Session::reterieve_all() { - Ok(sessions) => sessions, - Err(err) => return err, - }; - - let mut html = r#" - <tr> - <th>User ID</th> - <th>Token</th> - </tr> - "# - .to_string(); - - for session in sessions { - html.push_str(&format!( - "<tr><td>{}</td><td>{}</td></tr>", - session.user_id, session.token - )); - } - - ResponseCode::Success.text(&html) -} |