From 4e1c2b9f04073294ecb8402486c20d9c01721598 Mon Sep 17 00:00:00 2001 From: Jokler Date: Wed, 14 Oct 2020 00:19:27 +0200 Subject: Replace channels&locks with actors & log with slog --- src/web_server/api.rs | 17 ++++++------ src/web_server/bot_data.rs | 47 +++++++++++++++++++++++++++++++ src/web_server/bot_executor.rs | 63 ------------------------------------------ src/web_server/default.rs | 17 ++++++------ src/web_server/tmtu.rs | 17 ++++++------ 5 files changed, 72 insertions(+), 89 deletions(-) create mode 100644 src/web_server/bot_data.rs delete mode 100644 src/web_server/bot_executor.rs (limited to 'src/web_server') diff --git a/src/web_server/api.rs b/src/web_server/api.rs index 4deedad..b1d50c4 100644 --- a/src/web_server/api.rs +++ b/src/web_server/api.rs @@ -1,22 +1,23 @@ -use actix::Addr; use actix_web::{get, web, HttpResponse, Responder, ResponseError}; use derive_more::Display; use serde::Serialize; +use xtra::WeakAddress; -use crate::web_server::{BotDataListRequest, BotDataRequest, BotExecutor}; +use crate::web_server::{BotDataListRequest, BotDataRequest}; +use crate::MasterBot; #[get("/bots")] -pub async fn get_bot_list(bot: web::Data>) -> impl Responder { - let bot_datas = match bot.send(BotDataListRequest).await.unwrap() { - Ok(data) => data, - Err(_) => Vec::with_capacity(0), - }; +pub async fn get_bot_list(bot: web::Data>) -> impl Responder { + let bot_datas = bot.send(BotDataListRequest).await.unwrap(); web::Json(bot_datas) } #[get("/bots/{name}")] -pub async fn get_bot(bot: web::Data>, name: web::Path) -> impl Responder { +pub async fn get_bot( + bot: web::Data>, + name: web::Path, +) -> impl Responder { if let Some(bot_data) = bot.send(BotDataRequest(name.into_inner())).await.unwrap() { Ok(web::Json(bot_data)) } else { diff --git a/src/web_server/bot_data.rs b/src/web_server/bot_data.rs new file mode 100644 index 0000000..af0c5e1 --- /dev/null +++ b/src/web_server/bot_data.rs @@ -0,0 +1,47 @@ +use async_trait::async_trait; + +use xtra::{Context, Handler, Message}; + +use crate::bot::MasterBot; +use crate::web_server::BotData; + +pub struct BotNameListRequest; + +impl Message for BotNameListRequest { + type Result = Vec; +} + +#[async_trait] +impl Handler for MasterBot { + async fn handle(&mut self, _: BotNameListRequest, _: &mut Context) -> Vec { + self.bot_names() + } +} + +pub struct BotDataListRequest; + +impl Message for BotDataListRequest { + type Result = Vec; +} + +#[async_trait] +impl Handler for MasterBot { + async fn handle(&mut self, _: BotDataListRequest, _: &mut Context) -> Vec { + self.bot_datas().await + } +} + +pub struct BotDataRequest(pub String); + +impl Message for BotDataRequest { + type Result = Option; +} + +#[async_trait] +impl Handler for MasterBot { + async fn handle(&mut self, r: BotDataRequest, _: &mut Context) -> Option { + let name = r.0; + + self.bot_data(name).await + } +} diff --git a/src/web_server/bot_executor.rs b/src/web_server/bot_executor.rs deleted file mode 100644 index 0d3e7b7..0000000 --- a/src/web_server/bot_executor.rs +++ /dev/null @@ -1,63 +0,0 @@ -use std::sync::Arc; - -use actix::{Actor, Context, Handler, Message}; - -use crate::bot::MasterBot; -use crate::web_server::BotData; - -pub struct BotExecutor(pub Arc); - -impl Actor for BotExecutor { - type Context = Context; -} - -pub struct BotNameListRequest; - -impl Message for BotNameListRequest { - // A plain Vec does not work for some reason - type Result = Result, ()>; -} - -impl Handler for BotExecutor { - type Result = Result, ()>; - - fn handle(&mut self, _: BotNameListRequest, _: &mut Self::Context) -> Self::Result { - let bot = &self.0; - - Ok(bot.bot_names()) - } -} - -pub struct BotDataListRequest; - -impl Message for BotDataListRequest { - // A plain Vec does not work for some reason - type Result = Result, ()>; -} - -impl Handler for BotExecutor { - type Result = Result, ()>; - - fn handle(&mut self, _: BotDataListRequest, _: &mut Self::Context) -> Self::Result { - let bot = &self.0; - - Ok(bot.bot_datas()) - } -} - -pub struct BotDataRequest(pub String); - -impl Message for BotDataRequest { - type Result = Option; -} - -impl Handler for BotExecutor { - type Result = Option; - - fn handle(&mut self, r: BotDataRequest, _: &mut Self::Context) -> Self::Result { - let name = r.0; - let bot = &self.0; - - bot.bot_data(name) - } -} diff --git a/src/web_server/default.rs b/src/web_server/default.rs index 542dade..6b15784 100644 --- a/src/web_server/default.rs +++ b/src/web_server/default.rs @@ -1,9 +1,9 @@ -use actix::Addr; use actix_web::{http::header, web, Error, HttpResponse}; -use askama::Template; -use askama_actix::TemplateIntoResponse; +use askama_actix::{Template, TemplateIntoResponse}; +use xtra::WeakAddress; -use crate::web_server::{filters, BotData, BotDataRequest, BotExecutor, BotNameListRequest}; +use crate::web_server::{filters, BotData, BotDataRequest, BotNameListRequest}; +use crate::MasterBot; #[derive(Template)] #[template(path = "index.htm")] @@ -12,8 +12,8 @@ struct OverviewTemplate<'a> { bot: Option<&'a BotData>, } -pub async fn index(bot: web::Data>) -> Result { - let bot_names = bot.send(BotNameListRequest).await.unwrap().unwrap(); +pub async fn index(bot: web::Data>) -> Result { + let bot_names = bot.send(BotNameListRequest).await.unwrap(); OverviewTemplate { bot_names: &bot_names, @@ -23,10 +23,10 @@ pub async fn index(bot: web::Data>) -> Result>, + bot: web::Data>, name: String, ) -> Result { - let bot_names = bot.send(BotNameListRequest).await.unwrap().unwrap(); + let bot_names = bot.send(BotNameListRequest).await.unwrap(); if let Some(bot) = bot.send(BotDataRequest(name)).await.unwrap() { OverviewTemplate { @@ -35,7 +35,6 @@ pub async fn get_bot( } .into_response() } else { - // TODO to 404 or not to 404 Ok(HttpResponse::Found().header(header::LOCATION, "/").finish()) } } diff --git a/src/web_server/tmtu.rs b/src/web_server/tmtu.rs index 33a14af..e9eea98 100644 --- a/src/web_server/tmtu.rs +++ b/src/web_server/tmtu.rs @@ -1,9 +1,9 @@ -use actix::Addr; use actix_web::{http::header, web, Error, HttpResponse}; -use askama::Template; -use askama_actix::TemplateIntoResponse; +use askama_actix::{Template, TemplateIntoResponse}; +use xtra::WeakAddress; -use crate::web_server::{filters, BotData, BotDataRequest, BotExecutor, BotNameListRequest}; +use crate::web_server::{filters, BotData, BotDataRequest, BotNameListRequest}; +use crate::MasterBot; #[derive(Template)] #[template(path = "tmtu/index.htm")] @@ -12,8 +12,8 @@ struct TmtuTemplate { bot: Option, } -pub async fn index(bot: web::Data>) -> Result { - let bot_names = bot.send(BotNameListRequest).await.unwrap().unwrap(); +pub async fn index(bot: web::Data>) -> Result { + let bot_names = bot.send(BotNameListRequest).await.unwrap(); TmtuTemplate { bot_names, @@ -23,10 +23,10 @@ pub async fn index(bot: web::Data>) -> Result>, + bot: web::Data>, name: String, ) -> Result { - let bot_names = bot.send(BotNameListRequest).await.unwrap().unwrap(); + let bot_names = bot.send(BotNameListRequest).await.unwrap(); if let Some(bot) = bot.send(BotDataRequest(name)).await.unwrap() { TmtuTemplate { @@ -35,7 +35,6 @@ pub async fn get_bot( } .into_response() } else { - // TODO to 404 or not to 404 Ok(HttpResponse::Found().header(header::LOCATION, "/").finish()) } } -- cgit v1.2.3-70-g09d2