summaryrefslogtreecommitdiffstats
path: root/src/routes/dashboard.rs
blob: 7d5d3e40a84ae17df806b7ad6702fbf3b63e1655 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use askama::Template;
use axum::{
    Router,
    extract::State,
    response::{Html, IntoResponse},
    routing::get,
};

use crate::auth::CurrentUser;
use crate::routes::AppState;

#[derive(Template)]
#[template(path = "dashboard.html")]
struct DashboardTemplate {
    username: String,
    is_superuser: bool,
    password_change_required: bool,
    active_instance: Option<ActiveInstanceInfo>,
    container_state: String,
}

#[derive(Clone)]
struct ActiveInstanceInfo {
    name: String,
    version: String,
    server_type: String,
    max_players: i32,
}

pub fn router() -> Router<AppState> {
    Router::new().route("/", get(dashboard))
}

async fn dashboard(
    State(state): State<AppState>,
    CurrentUser(account): CurrentUser,
) -> Result<impl IntoResponse, axum::response::Redirect> {
    let (active_info, container_state) = {
        let conn = state.db.conn();
        let result = conn.query_row(
            "SELECT name, version, server_type, max_players, container_state
             FROM instances WHERE is_active = 1",
            [],
            |row| {
                Ok((
                    Some(ActiveInstanceInfo {
                        name: row.get(0)?,
                        version: row.get(1)?,
                        server_type: row.get(2)?,
                        max_players: row.get(3)?,
                    }),
                    row.get::<_, String>(4)?,
                ))
            },
        );
        result.unwrap_or((None, "stopped".into()))
    };

    let template = DashboardTemplate {
        username: account.username,
        is_superuser: account.is_superuser,
        password_change_required: account.password_change_required,
        active_instance: active_info,
        container_state,
    };

    Ok(Html(template.render().unwrap_or_default()))
}