aboutsummaryrefslogtreecommitdiffstats
path: root/src/web_server/default.rs
blob: 6b15784b77282411c50b9b576b9e4bf5efa7b5bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use actix_web::{http::header, web, Error, HttpResponse};
use askama_actix::{Template, TemplateIntoResponse};
use xtra::WeakAddress;

use crate::web_server::{filters, BotData, BotDataRequest, BotNameListRequest};
use crate::MasterBot;

#[derive(Template)]
#[template(path = "index.htm")]
struct OverviewTemplate<'a> {
    bot_names: &'a [String],
    bot: Option<&'a BotData>,
}

pub async fn index(bot: web::Data<WeakAddress<MasterBot>>) -> Result<HttpResponse, Error> {
    let bot_names = bot.send(BotNameListRequest).await.unwrap();

    OverviewTemplate {
        bot_names: &bot_names,
        bot: None,
    }
    .into_response()
}

pub async fn get_bot(
    bot: web::Data<WeakAddress<MasterBot>>,
    name: String,
) -> Result<HttpResponse, Error> {
    let bot_names = bot.send(BotNameListRequest).await.unwrap();

    if let Some(bot) = bot.send(BotDataRequest(name)).await.unwrap() {
        OverviewTemplate {
            bot_names: &bot_names,
            bot: Some(&bot),
        }
        .into_response()
    } else {
        Ok(HttpResponse::Found().header(header::LOCATION, "/").finish())
    }
}