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;
|
2018-09-18 11:19:45 +01:00
|
|
|
|
2020-04-09 01:54:28 +01:00
|
|
|
mod message;
|
2018-09-18 11:19:45 +01:00
|
|
|
mod server;
|
2020-04-09 01:54:28 +01:00
|
|
|
mod session;
|
2018-09-18 11:19:45 +01:00
|
|
|
|
2020-04-09 01:54:28 +01:00
|
|
|
use session::WsChatSession;
|
2018-09-18 11:19:45 +01:00
|
|
|
|
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)
|
2018-09-18 11:19:45 +01:00
|
|
|
}
|
|
|
|
|
2024-07-21 10:04:59 +01:00
|
|
|
// the actor-based WebSocket examples REQUIRE `actix_web::main` for actor support
|
2020-11-05 21:11:14 +02:00
|
|
|
#[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"));
|
2018-09-18 11:19:45 +01:00
|
|
|
|
2022-02-18 01:44:53 +00:00
|
|
|
log::info!("starting HTTP server at http://localhost:8080");
|
2018-09-18 11:19:45 +01:00
|
|
|
|
2022-02-18 01:44:53 +00:00
|
|
|
HttpServer::new(move || {
|
2018-09-18 11:19:45 +01:00
|
|
|
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
|
2018-09-18 11:19:45 +01:00
|
|
|
}
|