1
0
mirror of https://github.com/actix/examples synced 2025-02-02 09:39:03 +01:00
2024-07-07 00:23:03 +01:00

49 lines
1.2 KiB
Rust

use std::{io, sync::Arc};
use actix_web::{get, middleware::Logger, post, web, App, HttpResponse, HttpServer, Responder};
use actix_web_lab::extract::Path;
mod broadcast;
use self::broadcast::Broadcaster;
#[actix_web::main]
async fn main() -> io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let data = Broadcaster::create();
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(move || {
App::new()
.app_data(web::Data::from(Arc::clone(&data)))
.service(index)
.service(event_stream)
.service(broadcast_msg)
.wrap(Logger::default())
})
.bind(("127.0.0.1", 8080))?
.workers(2)
.run()
.await
}
#[get("/")]
async fn index() -> impl Responder {
web::Html::new(include_str!("index.html").to_owned())
}
#[get("/events")]
async fn event_stream(broadcaster: web::Data<Broadcaster>) -> impl Responder {
broadcaster.new_client().await
}
#[post("/broadcast/{msg}")]
async fn broadcast_msg(
broadcaster: web::Data<Broadcaster>,
Path((msg,)): Path<(String,)>,
) -> impl Responder {
broadcaster.broadcast(&msg).await;
HttpResponse::Ok().body("msg sent")
}