aboutsummaryrefslogtreecommitdiffstats
path: root/src/web_server
diff options
context:
space:
mode:
authorJokler <jokler@protonmail.com>2020-06-04 21:51:49 +0200
committerJokler <jokler@protonmail.com>2020-06-04 21:51:49 +0200
commit3575ac579cdcc5a9fa3ffec84952a634d1d7deaf (patch)
tree93666bf5d3ba01e2529f07cbedc049bda1f5079b /src/web_server
parent654f6b349ab20ce4b19afdd5d37f9a569165d1f2 (diff)
downloadpokebot-3575ac579cdcc5a9fa3ffec84952a634d1d7deaf.tar.gz
pokebot-3575ac579cdcc5a9fa3ffec84952a634d1d7deaf.zip
Improve the default website somewhat
Diffstat (limited to 'src/web_server')
-rw-r--r--src/web_server/default.rs33
1 files changed, 25 insertions, 8 deletions
diff --git a/src/web_server/default.rs b/src/web_server/default.rs
index b3c8291..ec86182 100644
--- a/src/web_server/default.rs
+++ b/src/web_server/default.rs
@@ -1,24 +1,41 @@
use actix::Addr;
-use actix_web::{web, Error, HttpResponse};
+use actix_web::{http::header, web, Error, HttpResponse};
use askama::actix_web::TemplateIntoResponse;
use askama::Template;
-use crate::web_server::{filters, BotData, BotDataListRequest, BotExecutor};
+use crate::web_server::{filters, BotData, BotDataRequest, BotExecutor, BotNameListRequest};
#[derive(Template)]
#[template(path = "index.htm")]
struct OverviewTemplate<'a> {
- bots: &'a [BotData],
+ bot_names: &'a [String],
+ bot: Option<&'a BotData>,
}
pub async fn index(bot: web::Data<Addr<BotExecutor>>) -> Result<HttpResponse, Error> {
- let bot_datas = match bot.send(BotDataListRequest).await.unwrap() {
- Ok(data) => data,
- Err(_) => Vec::with_capacity(0),
- };
+ let bot_names = bot.send(BotNameListRequest).await.unwrap().unwrap();
OverviewTemplate {
- bots: &bot_datas[..],
+ bot_names: &bot_names,
+ bot: None,
}
.into_response()
}
+
+pub async fn get_bot(
+ bot: web::Data<Addr<BotExecutor>>,
+ name: String,
+) -> Result<HttpResponse, Error> {
+ let bot_names = bot.send(BotNameListRequest).await.unwrap().unwrap();
+
+ if let Some(bot) = bot.send(BotDataRequest(name)).await.unwrap() {
+ OverviewTemplate {
+ bot_names: &bot_names,
+ bot: Some(&bot),
+ }
+ .into_response()
+ } else {
+ // TODO to 404 or not to 404
+ Ok(HttpResponse::Found().header(header::LOCATION, "/").finish())
+ }
+}