2022-02-18 01:44:53 +00:00
|
|
|
use std::{
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
time::Instant,
|
2020-11-30 18:11:33 +05:30
|
|
|
};
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
use actix::*;
|
2022-02-18 01:44:53 +00:00
|
|
|
use actix_files::{Files, NamedFile};
|
|
|
|
use actix_web::{
|
2022-02-18 02:44:02 +00:00
|
|
|
middleware::Logger, web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
|
2022-02-18 01:44:53 +00:00
|
|
|
};
|
2019-03-29 15:08:35 -07:00
|
|
|
use actix_web_actors::ws;
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
mod server;
|
2022-02-18 01:44:53 +00:00
|
|
|
mod session;
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2022-02-18 01:44:53 +00:00
|
|
|
async fn index() -> impl Responder {
|
|
|
|
NamedFile::open_async("./static/index.html").await.unwrap()
|
|
|
|
}
|
2018-09-27 22:37:19 +03:00
|
|
|
|
2020-11-30 18:11:33 +05:30
|
|
|
/// Entry point for our websocket route
|
2019-12-15 22:55:54 +06:00
|
|
|
async fn chat_route(
|
2019-03-29 15:08:35 -07:00
|
|
|
req: HttpRequest,
|
|
|
|
stream: web::Payload,
|
|
|
|
srv: web::Data<Addr<server::ChatServer>>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2018-04-13 09:18:42 +08:00
|
|
|
ws::start(
|
2022-02-18 01:44:53 +00:00
|
|
|
session::WsChatSession {
|
2018-04-13 09:18:42 +08:00
|
|
|
id: 0,
|
|
|
|
hb: Instant::now(),
|
|
|
|
room: "Main".to_owned(),
|
2018-05-08 11:08:43 -07:00
|
|
|
name: None,
|
2019-03-29 15:08:35 -07:00
|
|
|
addr: srv.get_ref().clone(),
|
2018-05-08 11:08:43 -07:00
|
|
|
},
|
2019-03-29 15:08:35 -07:00
|
|
|
&req,
|
|
|
|
stream,
|
2018-05-08 11:08:43 -07:00
|
|
|
)
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
2022-02-18 01:44:53 +00:00
|
|
|
/// Displays state
|
|
|
|
async fn get_count(count: web::Data<AtomicUsize>) -> impl Responder {
|
|
|
|
let current_count = count.load(Ordering::SeqCst);
|
2022-06-07 22:53:38 -04:00
|
|
|
format!("Visitors: {current_count}")
|
2020-11-30 18:11:33 +05:30
|
|
|
}
|
|
|
|
|
2020-09-12 16:49:45 +01:00
|
|
|
#[actix_web::main]
|
2019-12-15 22:55:54 +06: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-04-13 09:18:42 +08:00
|
|
|
|
2022-02-18 01:44:53 +00:00
|
|
|
// set up applications state
|
|
|
|
// keep a count of the number of visitors
|
2020-11-30 18:11:33 +05:30
|
|
|
let app_state = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
2022-02-18 01:44:53 +00:00
|
|
|
// start chat server actor
|
2020-11-30 18:11:33 +05:30
|
|
|
let server = server::ChatServer::new(app_state.clone()).start();
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2022-02-18 01:44:53 +00:00
|
|
|
log::info!("starting HTTP server at http://localhost:8080");
|
|
|
|
|
2018-05-08 11:08:43 -07:00
|
|
|
HttpServer::new(move || {
|
2019-03-29 15:08:35 -07:00
|
|
|
App::new()
|
2022-02-18 01:44:53 +00:00
|
|
|
.app_data(web::Data::from(app_state.clone()))
|
|
|
|
.app_data(web::Data::new(server.clone()))
|
|
|
|
.service(web::resource("/").to(index))
|
|
|
|
.route("/count", web::get().to(get_count))
|
|
|
|
.route("/ws", web::get().to(chat_route))
|
|
|
|
.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)
|
2022-02-17 20:22:36 +00:00
|
|
|
.bind(("127.0.0.1", 8080))?
|
2019-12-25 20:48:33 +04:00
|
|
|
.run()
|
2019-12-15 22:55:54 +06:00
|
|
|
.await
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|