From 8f4ad4ca41b29d276028d97ed62b3eea56f288e5 Mon Sep 17 00:00:00 2001
From: RMcTn <rmctiernan@protonmail.com>
Date: Mon, 16 Oct 2023 18:13:00 +0100
Subject: [PATCH] Link pages on root page, and add run now page

---
 server/src/main.rs | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/server/src/main.rs b/server/src/main.rs
index 3c17093..c4a35de 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -70,6 +70,8 @@ struct AppState {
 }
 
 const USERS_ROUTE: &'static str = "/users";
+const CREATE_USER_ROUTE: &'static str = "/create_user";
+const RUN_SCRAPER_NOW_ROUTE: &'static str = "/run";
 
 #[tokio::main]
 async fn main() -> Result<(), Box<dyn Error>> {
@@ -134,9 +136,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
         .route("/", get(root_page))
         .route(USERS_ROUTE, get(show_all_users_page))
         .route(
-            "/create_user",
+            CREATE_USER_ROUTE,
             get(show_create_user_form).post(submit_user_form),
         )
+        .route(RUN_SCRAPER_NOW_ROUTE, get(run_scraper_and_email_handler)) // Probably shouldn't be a get request,
+        // but :shrug:
         .layer(axum::middleware::from_fn_with_state(
             app_state.clone(),
             auth_middleware,
@@ -297,11 +301,21 @@ async fn auth_middleware<B>(
 
 #[debug_handler]
 async fn root_page(TypedHeader(cookie): TypedHeader<axum::headers::Cookie>) -> impl IntoResponse {
-    if let Some(session_id) = cookie.get("session_id") {
+    if let Some(_session_id) = cookie.get("session_id") {
         // TODO: Please stick a "retry" button in for the annoying failures. Until we move to some
         // job system anyway
-        let users_page_link = format!("<a href='{}'>Users</a>", USERS_ROUTE);
-        return Html(users_page_link).into_response();
+        let mut html = "<ul>".to_string();
+        let users_page_link = format!("<li><a href='{}'>Users</a></li>", USERS_ROUTE);
+        let create_user_link = format!("<li><a href='{}'>Create User</a></li>", CREATE_USER_ROUTE);
+        let run_link = format!(
+            "<li><a href='{}'>Run scraper and emails now</a></li>",
+            RUN_SCRAPER_NOW_ROUTE
+        );
+        html.push_str(&users_page_link);
+        html.push_str(&create_user_link);
+        html.push_str(&run_link);
+        html.push_str("</ul>");
+        return Html(html).into_response();
     } else {
         let redirect = Redirect::to("/signin").into_response();
         return redirect.into_response();
@@ -336,6 +350,12 @@ async fn sign_in_handler(
     }
 }
 
+async fn run_scraper_and_email_handler(State(app_state): State<AppState>) -> impl IntoResponse {
+    scrape_and_email_stuff(app_state.clone()).await;
+    let redirect = Redirect::to("/").into_response();
+    return redirect;
+}
+
 async fn sign_in_page() -> Html<&'static str> {
     Html(
         r#"