From 3057c21f673ac63dfb1da14a1783d7e7974d7d4b Mon Sep 17 00:00:00 2001 From: Yehowshua Immanuel Date: Thu, 2 Jan 2025 23:24:10 -0500 Subject: [PATCH] now using mod landing as its own module --- backend/src/main.rs | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/backend/src/main.rs b/backend/src/main.rs index 929de35..96a2df4 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -7,6 +7,8 @@ use serde::{Deserialize, Serialize}; use std::{fs, time::Duration}; use chrono::Local; +mod landing; + /// Greeting API structures #[derive(Serialize, Deserialize)] @@ -19,20 +21,10 @@ struct GreetingResponse { message: String, } -#[derive(Serialize)] -pub struct Landing { - pub time : String -} #[derive(Serialize)] enum ToFrontend { - Landing(Landing) -} - -async fn greet(req: web::Json) -> impl Responder { - info!("Received request to /api/greet with name: {}", req.name); - let message = format!("Hello, {}!", req.name); - web::Json(GreetingResponse { message }) + Landing(landing::Landing) } /// WebSocket actor @@ -43,20 +35,14 @@ impl Actor for MyWebSocket { fn started(&mut self, ctx: &mut Self::Context) { info!("WebSocket actor started"); - // Send messages every second ctx.run_interval(Duration::from_secs(1), |_, ctx| { - // Format the current time as HH:mm:ss let current_time = Local::now().format("%H:%M:%S").to_string(); - let message = ToFrontend::Landing(Landing { time: current_time }); + let message = ToFrontend::Landing(landing::Landing { time: current_time }); - // Serialize the message to JSON if let Ok(json_message) = serde_json::to_string(&message) { ctx.text(json_message); - } else { - info!("Failed to serialize the message"); } }); - info!("Leaving started"); } } @@ -87,12 +73,10 @@ async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() - .route("/api/greet", web::post().to(greet)) // Greeting API .route("/ws/", web::get().to(websocket_handler)) // WebSocket endpoint .service(Files::new("/assets", "./public/assets")) .service(Files::new("/", "./public").index_file("index.html")) // Serve frontend .default_service(web::route().to(|| async { - // Serve the `index.html` file let index_html = fs::read_to_string("./public/index.html") .unwrap_or_else(|_| "404 Not Found".to_string()); HttpResponse::Ok()