convert_to_actix #2
|
@ -7,6 +7,8 @@ use serde::{Deserialize, Serialize};
|
||||||
use std::{fs, time::Duration};
|
use std::{fs, time::Duration};
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
|
|
||||||
|
mod landing;
|
||||||
|
|
||||||
|
|
||||||
/// Greeting API structures
|
/// Greeting API structures
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
|
@ -19,20 +21,10 @@ struct GreetingResponse {
|
||||||
message: String,
|
message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
pub struct Landing {
|
|
||||||
pub time : String
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
enum ToFrontend {
|
enum ToFrontend {
|
||||||
Landing(Landing)
|
Landing(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 })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// WebSocket actor
|
/// WebSocket actor
|
||||||
|
@ -43,20 +35,14 @@ impl Actor for MyWebSocket {
|
||||||
|
|
||||||
fn started(&mut self, ctx: &mut Self::Context) {
|
fn started(&mut self, ctx: &mut Self::Context) {
|
||||||
info!("WebSocket actor started");
|
info!("WebSocket actor started");
|
||||||
// Send messages every second
|
|
||||||
ctx.run_interval(Duration::from_secs(1), |_, ctx| {
|
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 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) {
|
if let Ok(json_message) = serde_json::to_string(&message) {
|
||||||
ctx.text(json_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(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.route("/api/greet", web::post().to(greet)) // Greeting API
|
|
||||||
.route("/ws/", web::get().to(websocket_handler)) // WebSocket endpoint
|
.route("/ws/", web::get().to(websocket_handler)) // WebSocket endpoint
|
||||||
.service(Files::new("/assets", "./public/assets"))
|
.service(Files::new("/assets", "./public/assets"))
|
||||||
.service(Files::new("/", "./public").index_file("index.html")) // Serve frontend
|
.service(Files::new("/", "./public").index_file("index.html")) // Serve frontend
|
||||||
.default_service(web::route().to(|| async {
|
.default_service(web::route().to(|| async {
|
||||||
// Serve the `index.html` file
|
|
||||||
let index_html = fs::read_to_string("./public/index.html")
|
let index_html = fs::read_to_string("./public/index.html")
|
||||||
.unwrap_or_else(|_| "404 Not Found".to_string());
|
.unwrap_or_else(|_| "404 Not Found".to_string());
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
|
|
Loading…
Reference in a new issue