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

38 lines
1.1 KiB
Rust
Raw Normal View History

2022-02-18 01:44:53 +00:00
use actix_files::{Files, NamedFile};
2022-02-18 02:44:02 +00:00
use actix_web::{middleware::Logger, web, App, Error, HttpRequest, HttpServer, Responder};
2019-07-11 15:02:25 +06:00
use actix_web_actors::ws;
2020-04-09 01:54:28 +01:00
mod message;
mod server;
2020-04-09 01:54:28 +01:00
mod session;
2020-04-09 01:54:28 +01:00
use session::WsChatSession;
2022-02-18 01:44:53 +00:00
async fn index() -> impl Responder {
NamedFile::open_async("./static/index.html").await.unwrap()
}
2022-02-18 02:44:02 +00:00
async fn chat_ws(req: HttpRequest, stream: web::Payload) -> Result<impl Responder, Error> {
2020-04-09 01:54:28 +01:00
ws::start(WsChatSession::default(), &req, stream)
}
2024-07-21 10:04:59 +01:00
// the actor-based WebSocket examples REQUIRE `actix_web::main` for actor support
#[actix_web::main]
2020-04-09 01:54:28 +01:00
async fn main() -> std::io::Result<()> {
2022-02-18 01:44:53 +00:00
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
2022-02-18 01:44:53 +00:00
log::info!("starting HTTP server at http://localhost:8080");
2022-02-18 01:44:53 +00:00
HttpServer::new(move || {
App::new()
2022-02-18 01:44:53 +00:00
.service(web::resource("/").to(index))
.service(web::resource("/ws").to(chat_ws))
.service(Files::new("/static", "./static"))
.wrap(Logger::default())
2019-03-09 18:03:09 -08:00
})
2022-02-18 01:44:53 +00:00
.workers(2)
.bind(("127.0.0.1", 8080))?
.run()
.await
}