now using mod landing as its own module

This commit is contained in:
Yehowshua Immanuel 2025-01-02 23:24:10 -05:00
parent a6fd15c1b1
commit 3057c21f67

View file

@ -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<GreetingRequest>) -> 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()