aboutsummaryrefslogtreecommitdiffstats
path: root/src/web_server/tmtu.rs
blob: 0645ee40f662b1b26c9f2077b67c082b386ed225 (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
41
use actix::Addr;
use actix_web::{http::header, web, Error, HttpResponse};
use askama::actix_web::TemplateIntoResponse;
use askama::Template;

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

#[derive(Template)]
#[template(path = "tmtu/index.htm")]
struct TmtuTemplate {
    bot_names: Vec<String>,
    bot: Option<BotData>,
}

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

    TmtuTemplate {
        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() {
        TmtuTemplate {
            bot_names,
            bot: Some(bot),
        }
        .into_response()
    } else {
        // TODO to 404 or not to 404
        Ok(HttpResponse::Found().header(header::LOCATION, "/").finish())
    }
}